5ar5kovic
5ar5kovic

Reputation: 35

Connecting to dockerized MySQL database on Laravel App

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=

Upvotes: 1

Views: 1985

Answers (2)

George Orlov
George Orlov

Reputation: 36

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

Upvotes: 0

paccamicio
paccamicio

Reputation: 76

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}'

Upvotes: 2

Related Questions