Connect to mysql in a docker container from the host

docker mysql
docker-compose mysql
docker connect to mysql from another container
docker connect to mysql outside container
connect to local mysql from docker container
connect to remote mysql db from docker container
cannot connect to mysql on docker
docker mysql connection refused

(It's probably a dumb question due to my limited knowledge with Docker or mysql administration, but since I spent a whole evening on this issue, I dare to ask it.)

In a nutshell

I want to run mysql in a docker container and connect to it from my host. So far, the best I have achieved is:

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

More details

I'm using the following Dockerfile:

FROM ubuntu:14.04.3
RUN apt-get update && apt-get install -y mysql-server

# Ensure we won't bind to localhost only
RUN grep -v bind-address /etc/mysql/my.cnf > temp.txt \
  && mv temp.txt /etc/mysql/my.cnf

# It doesn't seem needed since I'll use -p, but it can't hurt

CMD /etc/init.d/mysql start && tail -F /var/log/mysql.log

In the directory where there is this file, I can succesfully build the image and run it with:

> docker build -t my-image .
> docker run -d -p 12345:3306 my-image

When I attach to the image, it seems to work just fine:

# from the host
> docker exec -it <my_image_name> bash

#inside of the container now
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.

However I don't have that much success from the host:

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

Even more details

  • I've seen that there's a question which looks like mine. However, it isn't the same (and it doesn't have any answers anyway)
    • I've seen that there are images dedicated to mysql, but I didn't have more success with them
    • My grep -v may feel weird. Admittedly, there may be cleaner way to do it. But when I attach my image, I can observe it actually worked as expected (ie: removed the bind-address). And I can see in the container /var/log/mysql/error.log:

Server hostname (bind-address): ''; port: 3306 - '' resolves to ''; Server socket created on IP: ''.

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

Change 3306 to port number you have forwarded from Docker container (in your case it will be 12345).

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.

Connect to MySQL running in Docker container from a local machine, Create shared folder for host machine eg: mkdir /host. Run docker container with volume mount option docker run -it -v /host:/shared <mysql image> . Then change mysql configuration file /etc/my. Restart MySQL service service mysql restart in docker. MySQL is a widely used, open-source relational database management system (RDBMS).

If you use "" instead of localhost mysql will use tcp method and you should be able to connect container with:

mysql -h -P 3306 -u root

Connect to mysql in a docker container from the host, You could create as many container as you want on the host machine, Run below command to create a docker container with mysql image. Inside a docker container, localhost refers to the container itself, unless you use --network host, which has other consequences. Rather, you want to refer to the host by using its IP address, The reason why the mysql command is not working in the container is because you don't haven't it installed. By default in docker

Connect docker and mysql in right way | by Raufur Rahman, Connecting to MySQL Server from within the Container. Once the server is ready, you can run the mysql client within the MySQL Server container you just started,� We’ll use the official MySQL image: docker container run --name my_mysql -d mysql. This will create a container named “my_mysql”. To execute a command inside the container run the following command: docker container exec -it my_mysql ls /var. The -i option stands for interactive, and -t tells Docker to allocate a pseudo TTY device.

2.6.1 Basic Steps for MySQL Server Deployment with Docker, The documentation from the official repo does not explain how to connect from outside the docker host !! I've created a container using the official� The following command will pull the MySQL server version 8.0.20 from the Docker registry and then instantiate a Docker container with the name “mk-mysql.” It will also attach the previously created volume “mysql-volume” with the Database and will expose the port 3306 so that you can reach the MySQL database outside the container:

if you running docker under docker-machine?

execute to get ip:

docker-machine ip <machine>

returns the ip for the machine and try connect mysql:

mysql -h<docker-machine-ip>

Connecting to a mysql running on a Docker container � Issue #95 , Hi, I did the following to start a MySQL docker container: docker run --name= my_mysql_instance -d mysql/mysql-server:latest I verified that its� 1. Before you can connect the MySQL server container with the host, you need to make sure the MySQL client package is installed: apt-get install mysql-client. 2. Then, start a MySQL client inside the container by typing: docker exec -it [container_name] mysql -uroot -p. 3. Provide the root password, when prompted.

Can't connect with mysql-cli from host to mysql docker container , Once in a while you may want to connect a container to a database or service running on your Docker host. Here's how to do it. Fortunately you can easily have a container connect to any service that’s installed on your Docker host. This means you could install your database / service directly on your Docker host and then connect to it from a running Docker container. You can simply connect to your local network IP address.

Docker Tip #35: Connect to a Database Running on Your Docker Host, There are many MySQL container images available in the Docker Hub registry From the physical host, we can now access the MySQL server. Restart docker container and run following commands to get to the bash shell in the mysql container. 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.

MySQL Docker Containers: Understanding the Basics, I cannot figure out how to connect to my container from the host. I have tried localhost and 127.0 an hour ago Up About an hour 3306/tcp� The documentation from the official repo does not explain how to connect from outside the docker host !! I've created a container using the official repository as follows: $ docker pull mysql $ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest -p 3306:3306 $ docker-machine ip default $ docker

  • Probably not so dumb. I stumbled upon this for the 10th time now and finally got the time to try it out at home.
  • Can you clarify why the mysql socket is not available? Your command works, but I'm wondering if there's a way to mount the mysql socket to the host from the container.
  • I am not an expert in Unix communication, but from what I understand socket is a connection represented as a file. Since socket file is not shared between Docker container and host machine MySQL client cannot use one from inside of the Docker container. To connect to the MySQL server inside Docker container from host machine you could: 1. Set MySQL server to put socket in the specified place --socket=/var/run/mysqld/mysqld.sock 2. Mount this file outside of the Docker container 3. Specify path to socket in MySQL client with --socket=/host/mysql.sock
  • @maniekq Great comment! But did you verify all your three possibilities? Do they all really work?
  • After I long struggle I found this golden answer, --protocol=tcp finally made the connection work. Thanks @maniekq for both the nice answer and your explanation on sockets via your comment!
  • It should not be the accepted answer, since he explicitly states that he has no idea of UNIX file communication.
  • I can verify that once i changed localhost to and removed the protocol flag, it worked the same
  • I think this is the short and best answer ;)
  • Might be short but it's not the answer, given that TCP is the slower method to communicate with mysql. (more cpu and higher latency)
  • @John you are right, it's slower but it does exist. Of course you can share /var/run/mysqld/mysqld.sock between host and container.
  • That's what I'd recommend for any higher duty DB. it's as simple as "-v socketfile.sock" and then mysql --socket /path/file.sock without polluting the tcp/ip stack.