How to connect mysql workbench to running mysql inside docker?

How to connect mysql workbench to running mysql inside docker?

To configure your docker container with MySQL Workbench please follow the steps below.


  1. In your docker-compose.yml you will have the following MySQL configuration block, if not add it. Here is an example of mine under the services object in my docker-compose.yml file.

            image: mysql
                - "./.data/db:/var/lib/mysql"
                MYSQL_ROOT_PASSWORD: root
                MYSQL_DATABASE: mydatabase
                MYSQL_USER: user
                MYSQL_PASSWORD: secret
  2. Restart docker container with docker-compose down followed by docker-compose up and run following commands to get to the bash shell in the MySQL container. The name will appear under the name heading.

    docker ps
    docker exec -it <mysql container name> /bin/bash 

    Inside the container, to connect to mysql command line type,

    mysql -u root -p

    Use MYSQL_ROOT_PASSWORD as specified in the docker-compose.yml . Execute following commands to create new user. You are creating the user 'user' here. Change it to what you require but remeber to change it in docker and restart.

    create user 'user'@'%' identified by 'pass';
    grant all privileges on *.* to 'user'@'%' with grant option;
    flush privileges;

    The percent sign (%) means all ip's. Restart the docker container.

  3. In your MySQL Workbench provide the connection details. Use MYSQL_PASSWORD as specified in your docker-compose.yml file.

MySQL Workbench setup

Categories: Posts