Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to MySQL server inside Docker

First I run mysql image:

docker run -e MYSQL_ROOT_PASSWORD=password  -d  -p 127.0.0.1:3308:3306 mysql

Then I use container bash:

docker exec -it my_container_name bash

In Bash I can successfully connect to MySQL server via command:

mysql -uroot -ppassword

But when I try to connect to MySQL container from Windows cmd:

mysql -uroot -ppassword -h127.0.0.1 -P3308

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (10061)

If I connect to 192.168.99.100 instead (this ip is returned by docker-machine ip), then the result is the same.

The question is: How do I correctly expose my MySQL port inside Docker to outside Windows?

like image 949
Yurii Avatar asked Nov 01 '16 15:11

Yurii


People also ask

How do I connect to a MySQL Docker database?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

Can I use MySQL with Docker?

MySQL has an official Docker image available on Docker Hub. First identify the image tag you should use. MySQL versions 5.6, 5.7, and 8.0 are available. The latest tag points to the latest release, currently 8.0.

How do I access MySQL Docker in terminal?

Connecting to MySQL Server from within the Containermysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword'; Substitute newpassword with the password of your choice. Once the password is reset, the server is ready for use.


1 Answers

The error is in your port mapping in the original docker run command, you just need to provide the ports, not the IP address:

docker run -e MYSQL_ROOT_PASSWORD=password -d -p 3308:3306 mysql

You can run docker ps -a to check for the port mapping in the running containers.

You should now be able to connect to MySQL using

mysql -uroot -ppassword -h192.168.99.100 -P3308
like image 90
nwinkler Avatar answered Sep 21 '22 15:09

nwinkler