Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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=
like image 737
harshit Avatar asked Jan 10 '19 08:01

harshit


2 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
like image 71
Udhav Sarvaiya Avatar answered Oct 19 '22 21:10

Udhav Sarvaiya


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

like image 2
BibekStha Avatar answered Oct 19 '22 21:10

BibekStha