11
votes

I have old project which built using Laravel 4.2.I am getting following error

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

I have googled and tried every think but i couldn't able to fix it

.env file

APP_KEY=az9tq5VHQCV9g5m2CsgY89jtijrCMgEA
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234

database.php

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'billing',
            'username'  => 'root',
            'password'  => '1234',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

enter image description here

Can any one guide me where m doing wrong ?

Note: Before asking question i tried by updating composer update as well as most of the stackoverflow answers.

Updated

I have tested this connection by creating php file

<?php
$servername = "localhost";
$username = "root";
$password = "1234";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

i will get Connected successfully message

18
it might feel bit dumb to ask but did you try restarting your server ? and instead of using xampp can you use laravel inbuilt server and test it ? - Aditya Singh
@user3103915. i have restarted server.but i didn't tried laravel inbuilt server - Vision Coderz

18 Answers

4
votes

Laravel 4.x doesn't even support ENV files. You just have to see whether settings in ./config/[env name]/database.php are correct.

17
votes

My error was Laravel Access denied for user 'root'@'localhost' (using password: NO)

and I only got this error at my host site.

In .env I changed:

DB_USERNAME=user
DB_PASSWORD=password

to:

DB_USERNAME='user'
DB_PASSWORD='password' 

Worked for me!

6
votes

DB_HOST=localhost worked for me on Laravel 5.7

6
votes

Run php artisan serve after configure .env, not before.

3
votes

Try In .ENV

APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234 

And Delete Cache Files From Root/boostrap/chache/files and Run The App

3
votes

The error says it all, Laravel can't connect to a DB. Check priveleges and make sure the DB exists. Also, try to connect to a DB with a client, like MySQL Workbench using same login and password. This will give a hint about what you can do to fix this. If you can't do this, it's not a Laravel issue.

2
votes

Try This

.env file

APP_ENV=local

DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=billing
DB_USERNAME=root
DB_PASSWORD=1234

config/database.php

default' => env('DB_CONNECTION', 'mysql'),


'mysql' => array(
            'driver'    => 'mysql',
            'host'      => env('DB_HOST','localhost'),
            'database'  => env('DB_DATABASE','billing'),
            'username'  => env('DB_USERNAME','root'),
            'password'  => env('DB_PASSWORD', '1234'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        )

Else checkout the .env file and check the connection

2
votes

I just ran into the same problem on a new Laravel install. The problem was that I was connecting to my ubuntu localhost mysql server instead of to the vagrant box's mysql server that was on it's own ip address '192.168.10.10'. I changed over to that and all worked a charm :)

2
votes

I'm test in Laravel 5.6.33 and i see this error: "Access denied for user 'root'@'localhost'", but the username and password is correct. In this version i think of this is a BUG, i change DB_HOST, from 127.0.0.1 to localhost and works!

DB_HOST=127.0.0.1 (before)
DB_HOST=localhost (after)

1
votes

I faced the same problem when learning laravel using homestead, the problem happened when I try to open a page. It tried to connect to database but got rejected, although I can connect to the database using mysql command line. The problem was I had wrong assumption that since homestead forward web server request from guest to host, the same thing is also same with myqsl, but apparently it is not. Homestead has it's own mysql service, so my problem was fixed by: - homestead ssh into the guest machine - mysql -u root -p to connect to mysql command line, default password is "secret" - create the database, but you need to change your password first, just change into the same password as in mysql root's password in your host machine for convenience - exit the mysql cli and - php artisan migrate to create the tables - refresh your browser, now it should be able to connect to your db

1
votes

Make sure your .env is using the correct port number. If you're using phpMyAdmin this is how is setup:

MariaDB defaults to 3306

and

MySQL defaults to 3308

So change your .env to use port 3308 for MySQL. I hope this helps!

1
votes

I got the same problem. You should add "" to the DB_USERNAME and DB_PASSWORD So,

  • Step 1: DB_USERNAME="root" DB_PASSWORD="1234"
  • Step 2: Run "php artisan config:clear"
  • Step 3: Run "php artisan cache:clear"
1
votes

please delete config file located at bootstrap/cache/config.php it will be autogenerated when you run php artisan config:cache command it will work

0
votes

Access denied for user 'root'@'localhost' (using password: YES)

The error was coming? The possible reason is your database connection.If the database is not connect then throw the error.

So Check the database releted all things and If all things is correct.

If not solved then change

DB_HOST=127.0.0.1 TO DB_HOST=localhost
0
votes

I had a similar problem, the password is the major issue in such communication, see my picture below, i solved it after changing password, php artisan serve and then reloading my view. It worked.

please login to mysql or phpmyadmin and change your password. enter image description here

enter image description here

0
votes

I changed this line DB_HOST=127.0.0.1 in .env to DB_HOST=localhost and it worked for me.

0
votes

It sounds kind of obvious, but check the credentials of the database.

-1
votes

Laravel 6 just set secret instead of empty space.

DB_PASSWORD=secret