Search code examples
mysqllaravellaradock

Laravel does not accept new database user in the .env file


I'm setting up my laravel dev environment using a different username. I don't want to use the root user to access my database.

By the way, I'm using Laradock. So I'm not using the normal homestead credentials.

If I use the new user, and run php artisan migrate, it keeps giving me the following error:

PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'newuser'@'172.25.0.4' (using password: YES)")

PDO::__construct("mysql:host=mysql;port=3306;dbname=db_name", "db_user", "db_password!", [])

I have done the following steps already:

  1. Restarted the server (docker-compose down, then up, then restarted docker)
  2. php artisan config:clear
  3. php artisan cache:clear
  4. php artisan config:cache

TAKE NOTE: It only throws this error if I'm using the new user but it works totally fine with the root user. The new user has GRANT ALL privilege as well.

My .env file contains:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_user
DB_PASSWORD=db_password

I expect to be able to run php artisan migrate using the new user credentials instead of the default root user.


Solution

  • You need set correct value for DB_HOST. If Your DB in local network, then you can set DB_HOST=localhost, or you may use external IP addres, for example DB_HOST=172.25.0.4

    After it run

    php artisan config:cache
    php artisan migrate