Abe Miessler
Abe Miessler

Reputation: 85126

How to connect to MySQL instance running in container on local machine?

I have setup a MySQL instance in a docker container using the instructions I found here. I'm able to connect to the instance by bashing into the container and then running the MySQL client like so:

docker exec -it mySQLContainer bash

and then

mysql -uroot -p

But I can't see to connect to the instance from the host machine. So far I've tried the following:

mysql localhost
mysql -h localhost -P 3306

which returns

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

and

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

which returns

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (61)

I've found a few other questions about this same problem but the solutions don't seem to work. Can anyone see what I'm doing wrong here or what I need to do in order to connect?

Note: Here is the container I spun up for MySQL - let me know if I need to include any other information:

docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
9f1fc20d66c0        mysql:latest        "docker-entrypoint.s…"   2 days ago          Up 2 days           3306/tcp, 33060/tcp   plugins

UPDATE: These are the last two lines from my container log - not sure if it's relevant or not:

2020-02-07T20:02:22.887174Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2020-02-07T20:02:22.982168Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

Upvotes: 1

Views: 1356

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562891

When I do this, I launch the container with an external port mapped to 3306.

For example, here's how I launch a container to run Percona Server 8.0:

docker run -d --name ps8 -e MYSQL_ROOT_PASSWORD=XXX -p 6603:3306 percona/percona-server:8.0

Then I can connect to the external port, and Docker will send it to 3306 inside the container:

mysql -h 127.0.0.1 -P 6603 -uroot -pXXX

I use port 6603 because I also have a MySQL instance running on my laptop using 3306. But if you want to use the default port 3306 and it doesn't conflict, that should work too.


Re your comment:

Unless you specify the port to use from outside the container, the ports are not exposed. You can't contact a port inside a docker container by default.

This is why the -p option is important. It tells docker you want it to allow connections from outside the container, and it tells docker what port it should listen on, to proxy to the mysqld process inside the container.

When I view my running container, I see there is a mapping from 6603->3306.

$ docker ps

CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                               NAMES
b1c949e0a8ae        percona/percona-server:8.0   "/docker-entrypoint.…"   44 seconds ago      Up 43 seconds       33060/tcp, 0.0.0.0:6603->3306/tcp   ps8

MySQL 8.0 also listens on 33060, which is the XDev Protocol port. I don't currently configure a mapping for this port, but I could.

Upvotes: 2

Related Questions