Home » Php » php – Laravel Access denied for user 'root'@'localhost' (using password: YES) in laravel 4.2

php – Laravel Access denied for user 'root'@'localhost' (using password: YES) in laravel 4.2

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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.

Answer:

DB_HOST=localhost worked for me on Laravel 5.7

Answer:

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

Answer:

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

Answer:

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 🙂

Answer:

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)

Answer:

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

Answer:

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

Answer:

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

Answer:

Laravel 6 just set secret instead of empty space.

DB_PASSWORD=secret

Answer:

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!

Answer:

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”