skip to Main Content

My docker cmds:

docker volume create SQL-DB

docker run       
  --name MySQL 
  -p 3306:3306 
  -v SQL-DB:/var/lib/mysql 
  -e MYSQL_ROOT_PASSWORD=password 
  -d mysql:latest

Able to connect via TablePLUS & other MySQL Clients. But Failed to connect using .env file of Laravel.

.env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=chrip
DB_USERNAME=root
DB_PASSWORD=password

chirp DB has been created using TablePlus MySQL client.

2

Answers


  1. The issue is due to the using of localhost ip in the .env file dbhost value. The db server is running in docker. so it behave as a separate network. So it will not be working in localhost ip. Please find the ip of the docker container. using docker inspect command and update .env file with the ip.

    Login or Signup to reply.
  2. Since you said you run the php-laravel on local machine using php artisan serve and mysql runs on docker, either you add the IP of mysql container or add flag --network host when you run mysql container instead of port mapping. This should solve your issue

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search