简体   繁体   中英

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

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

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!

DB_HOST="localhost" worked for me.


And to be safe, wrap .env variables in double quotes to avoid ENV errors:

DB_USERNAME="root"
DB_PASSWORD="#Password"

These will give errors: DB_PASSWORD=#Password , DB_PASSWORD=I have spaces

# is a start of a comment and is exactly the same as //

在 configure .env之后运行php artisan serve ,而不是之前。

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

The error says it all, Laravel can't connect to a DB. Checkpriveleges 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.

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 :)

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

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

I'm test in Laravel 5.6.33 and i see this error: , but the username and password is . ,但用户名和密码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)

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

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

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!

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"

请删除位于 bootstrap/cache/config.php 的配置文件,它会在您运行 php artisan config:cache 命令时自动生成它会起作用

I have solved issue .Issue with Prefix .In Postgrey Sql we have Prefixes to database so we have to add Prefix also in connection so it might help some one

database.php

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

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

在此处输入图片说明

我将 .env 中的这一行 DB_HOST=127.0.0.1 更改为 DB_HOST=localhost ,它对我有用。

这听起来很明显,但请检查数据库的凭据。

For me something was wrong in the env file. So I created a fresh installation, copied the env file from there and updated the credentials and it worked.

If you're using docker and still having issues with user access denied issues, you may want to try starting over fresh with the DB, which is was what it took for me.

First, I figured out where my DB data volumes were being stored in my docker-compose.yml file, which ended up being here: [app root dir]/docker/db/data/

    db:
    container_name: ${APP_NAME}_db
    image: mysql:8.0
    ports: 
        - 33060:3306
    volumes: 
        - ./docker/db/data:/var/lib/mysql
    environment:
        - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
        - MYSQL_DATABASE=${DB_DATABASE}

Then I ran this in the app root directory to remove all the docker containers.

docker-compose rm -v

Next I navigated to the DB data files and removed all of them:

cd ~/[app root dir]/API/docker/db/data
sudo rm -rf ./*

Finally, I started up the containers and was then able to connect with my database user credentials.

docker-compose up --build --force-recreate --no-deps

REFERENCE: https://github.com/docker-library/mysql/issues/51

Laravel 6 只是设置了秘密而不是空白空间。

DB_PASSWORD=secret

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM