Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) . DB_HOST set to localhost

I moved the Laravel project from localhost to server. Which I have done every step on the server.

I am able to view the login page on my server. The problem is I am not able to connect with my MySQL server.

My .env file:

APP_NAME=Transport
APP_ENV=local
APP_KEY=base64:mrakeyidharhaikonsdf
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=transport_db
DB_USERNAME=root
DB_PASSWORD=mypass

I tried to change the host to 127.0.0.1 and also tried to put my server's IP address. It didn't helped me. Am I missing something?

My error:

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) (SQL: select count(*) as aggregate from users where email = [email protected])

I know this question may have answers already on Stack Overflow. But I have different issue here.

like image 281
farooq Avatar asked Oct 04 '19 09:10

farooq


People also ask

How do I fix error 1045 28000 Access denied?

Set root user password Login as user root with blank password >mysql -u root mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'abc';

How do I fix access denied in mysql?

Use the ALTER USER command and change the authentication method to log into MySQL as root: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'insert_password'; This command changes the password for the user root and sets the authentication method to mysql_native_password.


1 Answers

To be honest, while working on Laravel 6, I also faced this issue many times and probably was unable to figure out the solution. I tried commands like php artisan config:cache and php artisan config:clear, but these two commands didn't help.

To come over this issue, you have to execute all the below commands:

php artisan route:cache
php artisan route:clear
php artisan config:cache
php artisan config:clear
php artisan optimize

Note: Make sure in .env you have add db_password and it is not null and also check if your db_password uses any special character. Always enclose them in "".

Example:

DB_PASSWORD="%123456%"

To debug this issue you can also create a test route and then dump the .env variable there to check if they have the correct values or not.

Route::get('/test/env', function () {
    dd(env('DB_DATABASE')); // Dump 'db' variable value one by one
});

Note: Please make sure to restart your server.

like image 194
Salman Zafar Avatar answered Sep 22 '22 04:09

Salman Zafar