skip to Main Content

For the first time I wanted to use Docker for my Laravel project and I just created the app with newest Laravel 8. I’m using Laravel Sail for starting the Docker. For now, thing goes pretty well, but I don’t know how to connect on MySQL database.

When I start docker with command "sail up", after that how can I make connection on MySQL database with Navicat for example, or even on phpmyadmin?

This is .env content for MySQL:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_example
DB_USERNAME=root 
DB_PASSWORD=

2

Answers


  1. To connect to MySQL that is in the Docker container you need to use your machine’s IP.

    If you are in a MacOS you can get the IP using the following command:

    ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}'
    
    Login or Signup to reply.
  2. First of all you need to set DB_HOST=mysql_container_name

    Also, you can create phpmyadmin container, add container to the same network and connect with mysql DB, like i did in my docker-compose file:

     ...
    phpmyadmin:
                container_name: phpmyadmin
                image: phpmyadmin
                environment:
                  PMA_HOST: db-mysql (your mysql-container name)
                  PMA_USER: phpmyadmin
                  PMA_PASSWORD: PASS
                ports:
                  - 8080:80
                depends_on:
                  - db-mysql (your mysql-container name)
                networks:
                  - backend (here is common network)
     ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search