How to fix "Illuminate\Database\QueryException: SQLSTATE[HY000] [1044] Access denied for user"

Viewed 104107

I tried to run: php artisan migrate

Also to connect to MySQL using Xampp on Windows.

I Got this error:

Illuminate\Database\QueryException  : SQLSTATE[HY000] [1044] Access
denied for user ''@'localhost' to database 'homestead' (SQL: select *
from information_schema.tables where table_schema = homestead and
table_name = migrations)

  at
C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [1044] Access denied for user
''@'localhost' to database 'homestead'")
      C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

  2  
PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead",
"homestead", "", [])
      C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

  Please use the argument -v to see more details.

.env file:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=homestead 
DB_USERNAME=homestead 
DB_PASSWORD=
9 Answers

Open the .env file and edit it. Just set up correct DB credentials:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=            // Your Database Name
DB_USERNAME=           // Yout Database Username
DB_PASSWORD=          // Your Database Password 

The DB_USERNAME should be set to root if you do not have a default username in the installation of MySQL in xampp.

If no password is set on the database, clear it DB_PASSWORD, empty space must also be removed(In the past I've also faceout this problem, window consider blank space as a password)

After completion of .env edit please enter this command in your terminal for clear cache:

php artisan config:cache

For Mac users using MAMP, if the accepted answer was not enough to solve this issue, like me, add socket to the .env file.

DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
//or
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

The variable should match with the setting on config > database.php. Look for connections > mysql > unix_socket

Hi go to xampp phpmyadmin create a database for exemple stack and change your .env to this:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=stack
DB_USERNAME=root 
DB_PASSWORD=

by default phpmyadmin username is root and without password save and try php artisan migrate

You should try this:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=your database name 
DB_USERNAME= your localhost-database username (i.e `root`)
DB_PASSWORD= your localhost-database password (i.e `root`)

then you run below command

php artisan config:cache
php artisan cache:clear 
php artisan migrate

Try this, go to your .env file, change the name of the database. Go to your phpmyadmin, create a new database with the new name in your .env file.

Then run php artisan migrate

I was stuck with the same error until I changed the DB_PORT and set the value from MAMP

Try this in your terminal: To start XMAPP use

sudo /opt/lampp/lampp start

Console output should be

Starting XAMPP for Linux 7.4.8-0...
XAMPP: Starting Apache...fail.
XAMPP: Another web server is already running.
XAMPP: Starting MySQL...ok.
XAMPP: Starting ProFTPD...ok.

When you get the error

error: /opt/lampp/bin/mysql.server: 264: kill: No such process**

try this again in your terminal:

sudo service mysql stop
sudo service apache2 stop

Then open datatbase after migrating the file.

you fix file .env Pay attention to the password section 'password'
 +need to have '
 
 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=            // Your Database Name
 DB_USERNAME=           // Yout Database Username
 DB_PASSWORD=          // Your Database Password 
 

I'm using MacOS and MAMP

I tried the above mentioned suggestions but I couldn't solve it.

What solved it for me was switching from PHP 7.x to 8.0.x in MAMP.

Related