问题
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' => '',
),
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
回答1:
Laravel 4.x doesn't even support ENV files. You just have to see whether settings in ./config/[env name]/database.php are correct.
回答2:
The error says it all, Laravel can't connect to a DB. Check priveledges and make sure DB exists. Also, try to connect to a DB with 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 Laravel issue.
回答3:
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
回答4:
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 :)
回答5:
DB_HOST=localhost
worked for me on Laravel 5.7
回答6:
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)
回答7:
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
回答8:
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
来源:https://stackoverflow.com/questions/39035201/laravel-access-denied-for-user-rootlocalhost-using-password-yes-in-larav