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
I know this question may have answers already on Stack Overflow. But I have different issue here.
Set root user password Login as user root with blank password >mysql -u root mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'abc';
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With