Laravel doesn't detect my local (development) database config file - php

My Laravel application is running on my local computer. When I run my app, Laravel uses my production database configuration and throws an Exception:
SQLSTATE[HY000] [1045] Access denied for user 'production_admin'#'localhost' (using password: YES)
This is strange because when I check current environment using artisan it says :
Current application environment: development
and my development database config file has a different username and password.
also I have stated in bootstrap/start.php that :
$env = $app->detectEnvironment(array(
'development' => array('*.dev', gethostname()),
'production' => array('*.com', '*.net', '*.org', '*.ir')
));
I don't know why Laravel insist on using production configuration although I'm in development environment.
Thanks for your help.

The best way to avoid this issue is to do the following:
in /bootstrap/start.php
Replace the default environment detection with:
$env = $app->detectEnvironment(function()
{
return getenv('APP_ENV') ?: 'local';
});
This will check for the presence of an environmental variable which is set by apache/nginx assuming you are running Homestead or similar.

Related

Error SQLSTATE [HY000] [1045]

I'm currently working on a Laravel project. I did the deploy on a shared server, but I get the error message
"SQLSTATE [HY000] [1045] Access denied for user 'root' # 'localhost' (using password: NO)"
The project in Local works me correctly, but on the server it's limited to error only. I configured the environment variables in my .env
I think that the server probably does not read the .env file and that is why the problem arises.
Clear your cache on live server by adding and running this route:
Route::get('/clear-cache', function() {
$exitCode = Artisan::call('cache:clear');
// return what you want
});
You have to add your proper database credentials to .env file .. you can find the database credentials in cPanel
Actually the problem was with my Hosting.
My best option was to start using DigitalOcean.
But at that moment what I did was:
Create a new database.
Create a new user.
Add the new data in my .env file as specified by Laravel.
Create a route to clear the cache as Junaid Ahmad specified but by changing some things.
Route::get('/cache', function() {
$clear = Artisan::call('cache:clear');
return "Cache cleared";
});
I take this opportunity to thank those people who helped me at that time.
And if you are going through this: Avoid at all costs modifying the database.php file. This isn't a good practice... And change your hosting for a DigitalOcean VPS.

Getting laravel app Database running on local

Hey guys I'm new to PHP/Laravel, from a rails background. I pulled down an app via ssh that has certain database configurations and I cant for the life of me get this app running locally these are the errors I keep getting.
SQLSTATE[HY000] [1045] Access denied for user
'fatkikisManiak'#'localhost' (using password: YES)
For this error I noticed that it was trying to run the production configurations so I thought it best to change to a development config. I went into bootstrap/start.php and
$env = $app->detectEnvironment(array(
\\'local' => array('homestead', 'Username', 'password'),
'local' => array('MacBook-Pro'),
));
This gives me:
SQLSTATE[HY000] [1049] Unknown database 'fatkikis_local'
Any help on getting this resolved would be apprciated
Please check the .env file in the root folder and modify the database informations.
This is a hidden file.

Laravel Unable to Detect Application Environment

I have three environment folders in the config directory in Laravel: development, testing, and local. When I change the environment and hostname within the start.php file, only the production environment is recognized. Any thoughts on what I might be doing wrong here?
app/routes.php
//using this as a test to confirm that the correct environment is being used
Route::get('/', function()
{
var_dump(App::environment());
});
bootstrap/start.php:
$env = $app->detectEnvironment(array(
'development' => array('localhost'),
));
Here, the result should be "string(11) "development". Instead, it's "string(10) "production".
localhost probably isn't the real hostname of your machine.
You can find out your hostname by echoing gethostname(). Then use that instead of localhost

Laravel 4.2 says my application is in production. How do I turn this off?

I have a fresh install of Laravel. When running php artisan migrate:refresh I get a message saying Application In Production! Do you really wish to run this command?'
I know this is an update in 4.2, however I can't figure out how to turn it off.
I found in the source that it comes from Illuminate\Console\ConfirmableTrait and runs if this if test passes : if ($this->getLaravel()->environment() == 'production')
I'm not sure why it thinks I'm in production. I never setup any environments. This is the default environment detection, which I'm still currently using.
$env = $app->detectEnvironment(array(
'local' => array('homestead')
));
Also, if I set a production environment to a hostname that isn't my machine, I still have the same problem.
Just specify a machine name for the host that matches a given environment, then laravel will automatically detect the environment (default is production), for example:
$env = $app->detectEnvironment(array(
//'local' => array('homestead'),
'local' => array('*.dev', gethostname()),
'production' => array('*.com', '*.net', 'www.somedomain.com')
));
Read the documentation and this answer as well.
Setting your environment to something other than production is The Right Way. See the accepted answer.
But, if you're looking for A Quick Fix you can use (in UNIXoid environments):
yes | php artisan migrate:refresh
All this does is send a stream of "y" to the program, which acts like you pressed "y" when prompted.
I find this to be a little better than --force, as not all the artisan commands support force.
In case if anyone stumbled upon this question while searching for similar problem in a lumen installation I'd suggest to check the .env file and add APP_ENV=local if its not already there. It solved my problem.
Hopefully this will help someone else. I suddenly had an issue where my dev site I was building stopped connecting to the DB saying:
PDOException SQLSTATE[HY000] [1049] Unknown database 'forge' failed
I was also receiving errors such as the OP when trying to run artisan migrate:refresh etc, the error was stating that i was in production etc etc.
After much head scratching (!) I found that my hostname value set inside the /bootstrap/start.php was wrong, because my hostname had changed on my macbook pro!? I have no idea how but it changed from something like RobMacbookPro2.local to RobMacbookPro.local. This meant it fell back to production thus loading the incorrect database.php file with the standard DB=forge (which was wrong)
Check this guide:
http://laravel.com/docs/4.2/configuration
Pay particular attention to the code:
<?php
$env = $app->detectEnvironment(array(
'local' => array('your-machine-name'),
));
On a mac and probably linux? you can determine your hostname by typing # hostname in terminal.
Hope that saves someone some time!

Laravel 4.1 to Heroku: SQLSTATE[HY000] [2002] No such file or directory

I deployed my Laravel 4.1 app in Heroku by following this article http://phpartisan.tumblr.com/post/71580870739/running-laravel-4-on-heroku
The static HTML homepage loaded okay but when I am trying to log-in, I am getting the error
SQLSTATE[HY000] [2002] No such file or directory
My local setup is Laravel 4.1 & MySQL.
I don't think there was a database created when I deployed.
Do I need to do it manually? How and what will be my settings then?
thanks!
The problem could be your environment might still point to local. in cli, type heroku run php artisan env to find out.
else, you might wanto run php artisan migrate --env=production to set it to the correct production environment.
you might then encounter another issue like this:
PHP Fatal error: Call to undefined function Symfony\Component\Console\mb_convert_variables() in /var/www/mysite/vendor/symfony/console/Symfony/Component/Console/Application.php on line 1154
Trust me, tt has nothing to do with mb_string module like what this post stated.
Laravel: Call to undefined function Symfony\Component\Console\mb_convert_variables()?
The problem is at environment setting.
First, make sure you have .env.php file at same dir level with composer.json and it is exists in your heroku instance. Since you cant FTP inside, you need to push to repo. But laravel tell you this is git ignored, so you will need to temporary allow it to push to your heroku repo.
Next, you will need to edit the bootstrap\start.php
replace the $env = $app->detectEnvironment(function(){}) to:
$env = $app->detectEnvironment(function() {
return file_exists(dirname(__FILE__) . '/../.env.php')
?
'production'
:
'local';
});
Once you done all these, rerun the migrate command and it will works.
You may follow this link for reference.
Laravel environment config not loading, migration fails - SQLSTATE[HY000] [2002] No such file or directory

Categories