I am a beginner developing an app in Laravel framework. I am facing a problem that only the root user works with my database without the password or the user ''#'host' works. However, when I deploy the app to the server I can not use any of these users
I get the following error, it seems that the Laravel is not reading any user name from .env or database.php file. (as the name of the user is blank here).
SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'itax'
Again it works if I use the user=root and no password or user=''
Here are the configurations of my .env file
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=itax
DB_USERNAME=raees
DB_PASSWORD=12345
and database.php file
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'itax',
'username' => 'raees',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Anyone facing the same issue or found any solutions to these problems please share
Found solution with empty username. The username was always blank.
The issue was with MySQL. The SQLSTATE error message is being passed directly from MySQL.
So then I deleted the database and user. Re-created the database in the Databases tab of PhpMyAdmin. And importantly separately added the user and privileges. This fixed the issue.
I could connect from the MySQL client and run migrations.
Related
I have a Heroku app where I'm hosting my Laravel app. I started the development initially with MySQL, so I wanted to continue doing so using Amazon's RDS service. I create the instance there and managed to successfully connect via my MySQL client, the console etc.
The problem is that the Laravel app can't connect the database after numerous desperate attempts for me to fix it. I have found some articles suggesting the use of DATABASE_URL environment variable is mandatory, so I added it via the Heroku app settings. It looks like so:
mysql://myusername:mypass#myhostnamefromamazon/mydb?sslca=/app/storage/certs/amazon-rds-ca-cert.pem
I found this solution on Heroku's website. I have placed the amazon-rds-ca-cert.pem file on my Laravel's storage folder, like so: /app/storage/certs/amazon-rds-ca-cert.pem
This didn't solve my issue, so then I kept looking and found a Stackoverflow question which had this issue on Lumen. I adjusted my config/database.php according to the answer, but it's still not working for me!
<?php
$credentials = get_db_credentials();
$config = [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', $credentials->host),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', $credentials->database),
'username' => env('DB_USERNAME', $credentials->username),
'password' => env('DB_PASSWORD', $credentials->password),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
];
if (env('APP_ENV') == 'production') {
$config['connections']['mysql']['options'] = [PDO::MYSQL_ATTR_SSL_CA => '../storage/certs/amazon-rds-ca-cert.pem'];
}
return $config;
The get_db_credentials() function simply parses the DATABASE_URL environment variable.
The exact exception that I get is:
[2018-10-25 19:32:16] production.ERROR: SQLSTATE[HY000] [2002] Connection timed out {"exception":"[object] (Doctrine\\DBAL\\Driver\\PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out at /tmp/build_05920c42a6de0a378402b798320d3f04/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:50
I'm totally lost on this and unsure how to proceed.
Your AWS Security Groups must permit traffic from Heroku's IP address range.
https://devcenter.heroku.com/articles/amazon-rds
You must grant Heroku dynos access to your RDS instance. The recommended way to do this is to configure the RDS instance to only accept SSL-encrypted connections from authorized users and configure the security group for your instance to permit ingress from all IPs, eg 0.0.0.0/0.
Sometimes (about every 20 request) I get this error. But the next (next second), the same request, is fine. I dont know why it failed the first one. Sometimes i can get another error :
No supported encrypter found. The cipher and / or key length are invalid.
My .env database parameters are fine.
I have generated a key using php artisan key:generate
This key is in my .env file under a APP_KEY key
My config/app.php has a key 'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC'
does anyone have ANY idea how this can happen?
Just cache your config using
php artisan config:cache
Don't forget to do this every time after setting your .env file.
I had this exact same problem for the past few days and I think I solved it:
The settings in .env are not always used for some reason or other and occasionally Laravel will just use the default settings in config/app.php and config/database.php.
config/app.php:
'key' => env('APP_KEY', 'SomeRandomString'),
'cipher' => 'AES-256-CBC',
Change the 'SomeRandomString' to the generated key from your .env
config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Change localhost, database, username, password to your actual settings from the .env. This example is for MySQL if you use another database, change those variables instead.
There might be a better solution (more secure?) but this is what so far kept the error from showing up.
You have to delete bootstrap/cache/config.php
I just experienced this after merging in changes from git without updating the .env file.
Basically, the person changed the code so that the application required these keys in the env file:
DB_SOME_DATABASE=something
DB_SOME_USERNAME=something
DB_SOME_PASSWORD=something
But I still had the old credentials in there, so it "looked correct", but Laravel was throwing this error that made it look like the env file wasn't being used.
This would indicate that, if you see this, check very closely if anything is spelled wrong in your .env file that would cause Laravel to attempt to use the default values (ie: look in the folder config/database.php where you see 'forge').
It may not be a typo. Someone may have added a database connection and maybe you haven't updated your .env file yet.
This should be really easy, but it's not. Here is the default settings for a MySQL database in Laravel 5.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I ran php artisan env on the production server and got production. I set my environment variables with the correct keys on Forge. But when I run artisan on the production server, I get an error: 'Access denied for user 'forge'#'localhost' (using password: NO)' The reason I think it's saying password: NO is that an empty string is the default for DB_PASSWORD if the environment variable is not set.
I think what is happening is that Forge is not reading my environment variables and is using the defaults specified. If I change the values of my environment variables, I get the same error message, which seems to confirm this. I have restarted the Digital Ocean production server, thinking that would force the server to read the variables. But nada. Do I need to do something else in Forge to get it to read the variables. Jeffrey Way's Laracast doesn't indicate that I need to.
What's going on here?
Laravel 5 is not yet in beta.
Taylor has already mentioned in a recent podcast that he'll need to make changes to Forge to accommodate Laravel 5 environmental settings.
I think from memory he mentioned the ability to just edit the .env file from Forge.
For now - you should just put all your settings into a .env file in the root of your web server and it will work.
In case anyone else finds this and is still experiencing it, the .env on both Laravel 5 and Forge now work. You must however make sure your database credentials are correct. This should work for most of the defaults as of the writing of this post (Laravel 5.2)
DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=forge
DB_PASSWORD=the_provided_password
I need my Laravel project to use a database that is on a virtual machine on another network.
I know there's two database configuration on
app/config
app/config/local
I'm using MySql, how should I configure the database.php file?
Right now is configured to my local:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db_name',
'username' => 'root',
'password' => 'the_password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
You should specify remote MySQL server host IP address in database.php configuration.
Also you need to make sure you have enable remote connection to your MySQL database (normaly most MySQL servers are restricted to connections only from localhost). For this purpose you will need to create new database user with the following command
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'IP' IDENTIFIED BY 'PASSWORD';
Where you put IP of the server where you laravel installation is located.
Also after that you must execute
FLUSH PRIVILEGES;
And the you can connect to remote database.
I moved an application that I'm working on into a new dev server, a Laravel Homestead VagrantBox on a Mac OSX host. Upon doing so, I ran php artisan:migrate to update my database and this went through without a hitch.
I decided to create a new user to continue testing, so I created the route
Route::get('/newuser', function()
{
User::create([
'username' => 'someone',
'email' => 'someone#someone.com',
'password' => Hash::make('password')
]);
return 'Done.';
});
When I visit /newuser, however. I am getting the following message.
SQLSTATE[HY000] [2002] Connection refused
Now, I know that my database config must be correct, as I received no errors when I ran php artisan migrate and my database migrated successfully. However, just to be safe, I checked my database config.
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:33060',
'database' => 'site',
'username' => 'root',
'password' => 'apassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I figured that maybe there was an issue with the way I defined the port I was on, so I added the parameter:
'port' => '33060'
Upon doing this, the error message changed from "Connection refused" to "No such file or directory"
I'm at a loss. Does anyone have any pointers?
Now, I know that my database config must be correct, as I received no errors when I ran php artisan migrate
It's a better than even money bet when you're stuck on something that it's one of your assumptions that's the problem. It's possible your Laravel application is reading different credentials during a command line run, or that the migration had nothing to do, or for some weird PHP reason the errors were suppressed during your migration run. I'd check the credentials Laravel's using during the context your errors are cropping up. Add the following code to your newuser route to see what Laravel's reading.
$default = Config::get('database.default');
var_dump($default);
$config = Config::get('database.connections.'.$default);
var_dump($config);
I had same problem and i saw my database server was unresponsive i restarted and it works... sometime it cause when wrong configuration.
I posted this answer if someone will have same issue and mysql unresponsive :)