简体   繁体   中英

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 = user.email@gmail.com)

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

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.

Make sure your database credentials and database host are set correctly:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="your_database_name"
DB_USERNAME="put_db_user_name _here"
DB_PASSWORD="put_db_password_here_if_have_set"

If you have not set any database password then add:

DB_PASSWORD=""

The problem is that the project made a cache file. 'config.php' must be deleted in order to allow the system to restart and you can do so by locate this file and deleting it:

bootstrap/cache/config.php

I was working in the Laravel framework using homestead and had such a problem. In fact, this error was because I did not create the database in Vagrant . So, I created the database. In more details:

  1. run vagrant ssh to get homestead command line.

  2. run mysql to start the MySQL command line client.

  3. run create database database_name; to create your database. Note that the database should be inside two ` ( not ') marks.

  4. Modify the .env file of the Laravel project as below:

     DB_CONNECTION=mysql DB_HOST=192.168.10.10 DB_PORT=3306 DB_DATABASE=askidb2 DB_USERNAME=homestead DB_PASSWORD=secret
  5. (if you previously made your model and configured files in Laravel projects for creation tables), run php artisan migrate in the command line of the project's folder. This causes it to create tables in the askidb2 database.

  6. (perhaps optional for you) since before I set needed files for seeding the question table I just run php artisan db:seed to seed my desired table(s).

If you use Homestead to serve your application, you must connect to the Homestead built-in database. Otherwise, I get an error ( SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' ) when I try to connect to the MySQL database that is installed on my Mac.

Here is how I connect to the Homestead database. I am not sure why the host → 127.0.0.1 from the Laravel documentation doesn't work.

In the.env file

DB_CONNECTION=mysql
DB_HOST=192.168.10.10
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=homestead
DB_PASSWORD=secret

I use Sequel Pro as the GUI to manage the database, and here is how I connect Sequel Pro to the Homestead MySQL database.

Connect with standard mode

Host: 192.168.10.10
User: homestead
Password: secret
Port: 3306

Connect with SSH mode

在此处输入图像描述

So the problem is mine. I apologize for that.

I missed to put my DB_PASSWROD inside "" . So that caused my two hours.

I have the password which contains some special characters at the beginning. So it should be inside the "" to avoid the problems.

For example, if my password is "%helloworld@" , then in the .env file, it should be:

`DB_PASSWORD="%helloworld@" `

Previously, I had

`DB_PASSWORD=%helloworld@

I had this error while my connection with the database was established. The problem was that I was on localhost , not 127.0.0.1.

First

DB_DATABASE="your_database_name"
DB_USERNAME="put_db_user_name _here"
DB_PASSWORD="put_db_password_here_if_have_set"

Then type

php artisan config:clear

This will make your problem clear. Then type

php artisan migrate

I had this error because my XAMPP user Account access for root and password did not match Laravel.env database DB_USERNAME & DB_PASSWORD.

This will Work:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="your_database_name"
DB_USERNAME="put_db_user_name _here"
DB_PASSWORD="put_db_password_here_if_have_set"

This may also be due to the DB_PORT . Go to the PhpMyAdmin Database server and check the server. So I changed from this

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306

to

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307

Change the .env as follows

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="Your DB name"
DB_USERNAME=root
DB_PASSWORD=

and then run php artisan config:cache

and then run php artisan migrate:fresh --seed

and then run php artisan serve

Please make sure to have the same db name in .env file and db name in php myadmin

In my case, if I have Bitnami WordPress. It's a service using an Apache/localhost address, so my XAMPP instance is only accessible via 127.0.0.1 and when accessing localhost I got 'Access denied error...'. Then when I stop the Bitnami service, everything is back to normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM