Search code examples
mysqldockerdocker-composedatagrip

I can't connect to mysql in docker from my localhost with DataGrip database client


I have the following docker-compose.yml file:

version: '3.2'

services:

  mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=verysecret
      - MYSQL_DATABASE=yii2advanced
      - MYSQL_USER=yii2advanced
      - MYSQL_PASSWORD=secret

I run it via docker-compose up -d. But I can't connect from SQL client to my database (I use DataGrip from JetBrains). Here is my configuration:

enter image description here

The password of course is secret. I have already tried to check allowed hosts for yii2advanced user:

enter image description here

As you can see for my yii2advanced connection is allowed from any host. I have tried to change mysqld.cnf and set bind-address = 0.0.0.0. Tried to setbind-address to *. Tried to set not 127.0.0.1 but 172.17.0.1 in the settings of my SQL client. Tried to create manually new one user with full privileges. No effect. The problem still exists. I can't connect to mysql in the container from my localhost.

What is wrong?


Solution

  • By default, docker-compose create a bridge network that will isolate your application from outside including the localhost.
    You need to expose 3306 port from isolated network to localhost.
    Try this.

    version: '3.2'
    
    services:
    
      mysql:
        image: mysql:5.7
        environment:
          - MYSQL_ROOT_PASSWORD=verysecret
          - MYSQL_DATABASE=yii2advanced
          - MYSQL_USER=yii2advanced
          - MYSQL_PASSWORD=secret
        ports:
          - 3306:3306 # <host port>:<container port>
    

    Good to read docker network section. https://docs.docker.com/network/