khalil jridi
khalil jridi

Reputation: 21

Laravel DataBase Connection: Access denied for user

I install laravel on Ampps (windows 10) with .

it works.

But Now I want connect to mysql.

I create a 'blog' DB and change these two files: .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD='mysql'

and /config/database.php :

  'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'blog'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', 'mysql'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

but still I get the below message when I want to migrate:

C:\Program Files (x86)\Ampps\www\blog>php artisan migrate

[Illuminate\Database\QueryException]
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)

[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

Upvotes: 2

Views: 3718

Answers (4)

Mehran Zamani
Mehran Zamani

Reputation: 831

First change your password in .env file from DB_PASSWORD='mysql' to DB_PASSWORD=mysql without single quotation. Second you should set more secure passwords for your databases.

You should give root user access to your database.

GRANT ALL PRIVILEGES ON blog.* TO 'root'@'localhost' identified by 'mysql';
FLUSH PRIVILEGES;

Upvotes: 1

Jahid Mahmud
Jahid Mahmud

Reputation: 1136

You should write this

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

and your env file is

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

Also restart your server.

Upvotes: 1

Rahman Qaiser
Rahman Qaiser

Reputation: 682

Password should be this

DB_PASSWORD=mysql

Not this

DB_PASSWORD='mysql'

Upvotes: 1

Marcin
Marcin

Reputation: 1494

Try running below command

php artisan cache:clear

This should update the settings saved in the cache.

Upvotes: 0

Related Questions