Connect to Docker MySQL container from localhost?

MysqlMacosDockerDocker Compose

Mysql Problem Overview


I have a docker mysql image running, following is what the docker-compose.yml file looks like:

db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: ""
    MYSQL_ALLOW_EMPTY_PASSWORD: yes
  ports:
    - "3306:3306"

This works fine.

My question is: How can I connect to the MySQL instance running on that container from the command line mysql client on my the host (my macbook)?

To clarify:

  • I have a macbook with Docker installed
  • I have a docker container with mysql
  • I want to connect to the mysql instance running on the aforementioned container from the Terminal on my macbook
  • I do NOT want to user a docker command to make this possible. Rather, I want to use the mysql client directly from the Terminal (without tunneling in through a docker container).

I don't have MySQL running locally, so port 3306 should be open and ready to use.

The command I am using to start the container is: docker-compose run

Mysql Solutions


Solution 1 - Mysql

Using docker-compose up

Since you published port 3306 on your docker host, from that host itself you would connect to 127.0.0.1:3306.

Using docker-compose run

In that case the port mapping section of the docker-compose.yml file is ignored. To have the port mapping section considered, you have to add the --service-ports option:

docker-compose run --service-ports db

Additional note

Beware that by default, the mysql client tries to connect using a unix socket when you tell it to connect to localhost. So do use 127.0.0.1 and not localhost:

 $ mysql -h 127.0.0.1 -P 3306 -u root

> Welcome to the MySQL monitor. Commands end with ; or \g. > Your MySQL connection id is 1 > Server version: 5.6.26 MySQL Community Server (GPL) > > Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. > > Oracle is a registered trademark of Oracle Corporation and/or its > affiliates. Other names may be trademarks of their respective > owners. > > Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. > > mysql>

$ mysql -h localhost -P 3306 -u root

> ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Solution 2 - Mysql

I got it!! The answer is to use the --service-ports option when running docker-compose:

docker-compose run --service-ports db (the original docker-compose.yml file works fine)

Thanks to all for the help!

Solution 3 - Mysql

A simple way to login to MySQL inside a Docker image is:

sudo docker exec -it <CONTAINER_ID> mysql -u root -p

for mySQL's root account by default password is not set, its BLANK, just press enter/return key, unless you have changed root password.

On successful execution, above command gives you mysql prompt.

Cheers!

Solution 4 - Mysql

If your Docker MySQL host is running correctly you can connect to it from local machine, but you should specify host, port and protocol like this:

mysql -h localhost -P 3306 --protocol=tcp -u root

Because you are running MySQL inside Docker container, socket is not available and you need to connect through TCP. Setting "--protocol" in the mysql command will change that.

Solution 5 - Mysql

this worked for me.

/usr/local/mysql/bin/mysql -h 127.0.0.1 -P 3306 -u root -p

Solution 6 - Mysql

You can just Use --network="host" in your docker run command, then 127.0.0.1 or localhost in your docker container will point to your docker host.

docker run --network="host" -p 8080:8080 <your-docker-Image>

Solution 7 - Mysql

Your yml file looks good.

You can directly connect docker container directly as it already mapped with local port 3306. Just goto terminal and run

mysql -h 127.0.0.1 -u root -p

Note: you must have access to mysql command line. If mysql command show an error, check '/usr/local/mysql/bin' other wise you can not connect to mysql server. In other word you must have mysql client on your machine.

Solution 8 - Mysql

Connect to MySQL via {host ip}:3306 since you've exposed the internal port to your host as 3306. If you need to access the MySQL CLI tools you will need to go docker exec -it mycontainer bash this will place you inside the container to access the tools installed with MySQL if you do not have them installed locally on the host o/s.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCalebView Question on Stackoverflow
Solution 1 - MysqlThomasleveilView Answer on Stackoverflow
Solution 2 - MysqlCalebView Answer on Stackoverflow
Solution 3 - Mysqlcross_handleView Answer on Stackoverflow
Solution 4 - MysqljozalaView Answer on Stackoverflow
Solution 5 - MysqlSwannieView Answer on Stackoverflow
Solution 6 - Mysqlnaib khanView Answer on Stackoverflow
Solution 7 - MysqlVirendra JadejaView Answer on Stackoverflow
Solution 8 - MysqlGHETTO.CHiLDView Answer on Stackoverflow