Search code examples
mysqllaravel-8laravel-sail

Connect to mySQL DB in a laravel project with sail


I've set up a laravel project with sail and I'm trying to connect to my database with a db management tool. Yet I can't figure out what address and what username I should use.

In my .env file :

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=app-name
DB_USERNAME=sail
DB_PASSWORD=password

The set-up is the default one. I've tried to connect through localhost:3306, but it doesn't work. Do you have any Idea on how I should proceed?

note that my project is running correctly in localhost.


Solution

  • As Laravel sail is basically just a docker-compose wrapper, you should check your docker-compose.yml file in the root directory for port forwardings of the mysql container. There should be something like this:

    ...
    mysql:
      image: 'mysql/mysql-server:8.0'
      ports:
        - '${FORWARD_DB_PORT:-3306}:3306'
    ...
    

    You could also check docker after putting sail up with sail up -d by running docker ps -a | grep mysql. If your output contains 0.0.0.0:3306->3306/tcp, your mysql server should be reachable at localhost:3306.