27

I build 2 docker container with docker-compose. I use Docker on Mac, no boot2docker.

version: '2'
    services:
        drupal-web:
            image: drupal:latest
        ports:
            - "8080:80"
    depends_on:
            - mysql-server
    links:
            - mysql-server:mysql
    mysql-server:
        image: mysql
    environment:
        MYSQL_DATABASE: drupal
        MYSQL_ROOT_PASSWORD: root
        MYSQL_USER: drupal
        MYSQL_PASSWORD: drupal

Everything works fine. I install Drupal successfully.

The problem is: I would like to connect SequelPro to my DB, but i dont get a connection to the mysql container. I'm a docker beginner.

Martijn
  • 15,791
  • 4
  • 36
  • 68
freemindghost
  • 381
  • 1
  • 3
  • 10

6 Answers6

55

You forgot to expose your DB port to the host, so simply add

mysql-server:
    image: mysql
    ports: 
      - "3306:3306"
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: drupal
        MYSQL_USER: drupal
        MYSQL_PASSWORD: drupal

And then connect to the database in Sequel Pro using:

user: root
password: root
host: localhost
port: 3306

If you already have a local mysql database running on your host, change the port

ports: 
  - "4306:3306"

and then connect to port 4306 instead of 3306. Be aware, from the Drupal container, you will still use 3306

Eugen Mayer
  • 8,942
  • 4
  • 33
  • 57
  • 1
    Explicitly exposing port 3306 allowed me to access it with sequel pro from my mac. I was a bit surprised that it was not already exposed by default – DDS Mar 13 '17 at 01:31
  • 4
    I'm getting "MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/lib/plugin/caching_sha2_password.so, 2): image not found" when trying to connect – Jonny Mar 15 '19 at 02:46
  • 1
    I spent hours to figure out that the issue was indeed another db running on the same 3306 port locally therefore not letting my sequel pro connect to docker's db. I changed the port as described above and my problem was solved. Thanks man! – Rox Nov 28 '21 at 23:18
14

Map host port 4306 (or any other available port) to docker mysql 3306 like:

mysql-server:
        image: mysql
    environment:
        MYSQL_DATABASE: drupal
        MYSQL_ROOT_PASSWORD: root
        MYSQL_USER: drupal
        MYSQL_PASSWORD: drupal
    ports:
      - "4306:3306"

You should be able to connect to docker mysql with 127.0.0.1:4306

mysql -u drupal -h 127.0.0.1 -P 4306 -p
Khaldoon Masud
  • 300
  • 2
  • 4
3

In my case i has problem with mysql:8, change to any 5 and all is ok

1

If you are running MySQL in docker, you might run into an error that says Host '172.18.0.1' is not allowed to connect to this MySQL server. The reason is that, in MySQL each database user is defined with an IP address that the user can connect from. By default, the IP address is set to 127.0.0.1 or localhost for 'root' user. But since we are running the MySQL server on docker, we won't be connecting to the server from that IP address. You can read more about it at https://github.com/docker-library/mysql/issues/275

The solution that worked for me:

docker exec -it mysql5.7.16 mysql -uroot -p123456

update mysql.user set host='%' where user='root';

flush privileges;
Anushri HV
  • 51
  • 3
0
version: "3.8"
services:
          db-1:
            container_name: db-1
            image: mysql:5.7
            restart: always
            environment:
              MYSQL_DATABASE: 'db'
              # So you don't have to use root, but you can if you like
              MYSQL_USER: 'user'
              # You can use whatever password you like
              MYSQL_PASSWORD: 'password'
              # Password for root access
              MYSQL_ROOT_PASSWORD: 'password'
            ports:
              # <Port exposed> : < MySQL Port running inside container>
              - '3306:3306'
            expose:
              # Opens port 3306 on the container
              - '3306'
              # Where our data will be persisted
            volumes:
              - my-db:/var/lib/mysql
volumes:
  my-db:

This works just fine today. However it didn't work when I used image: mysql:latest

George Mylonas
  • 704
  • 6
  • 16
-1

Should look a bit more like this:

version: '2'
services:
    mysql-server:
        image: mysql
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: drupal
            MYSQL_USER: drupal
            MYSQL_PASSWORD: drupal

    drupal-web:
        image: drupal:latest
        ports:
            - "8080:80"
        depends_on:
            - mysql-server
        links:
            - mysql-server:mysql-server
        environment:
            MYSQL_DATABASE: drupal
            MYSQL_USER: drupal
            MYSQL_PASSWORD: drupal

(Im not sure if defining the environment variables on a global level work, maybe someone who knows can correct my answer here and simplify it)

As you can see, you have 2 services, mysql-server and drupal-web. Drupal-web links your database service. Both services have their own environment variables.

chickahoona
  • 1,914
  • 14
  • 23