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
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.
Related
I've a mysql inside docker container, which is running on the server.
And I can run it normally inside the container
using
mysql -u root -h localhost --port=3306 -p
I've mapped the container 3306 to host 33069 port.
netstat -tnlp | grep :33069
the above command runed on the server shows me the following result
tcp6 0 0 :::33069 :::* LISTEN 120562/docker-proxy
from which it's clear that mysql is available not only for localhost.
However when I try to connect to the mysql from my local machine using the command
mysql -u root -h myAddress.com --port=33069 -p
I get the error
Can't connect to MySQL server on ...
after timeout.
What I've missed?
if you are using the docker run command, the container's port 3306:33069 needs to be exposed, and the docker MySql container host's server port, if the firewall is enabled for the port 33069.
Afterwards on your local pc, the port 33069 needs to be open, to communicate to MySql docker container.
Following the guide to create a mysql NDB cluster on https://hub.docker.com/r/mysql/mysql-cluster/ after initializing docker server exposing 3306 and 33060 I'm still unable to connect it using MySQL Workbench but I'm able to access it through CLI. Workbench throws an error saying as if there wasn't a database to be connected.
docker run -d --net=cluster --name=mysql1 --ip=192.168.0.10 -e MYSQL_RANDOM_ROOT_PASSWORD=true -p 3306:3306 -p 33060:33060 mysql/mysql-cluster mysqld
Besides trying to login with root I've also created a new user to try to login with the same outcome.
Bulding a mysql based image as follows (Dockerfile):
FROM mysql:5.7
COPY somescripts* /docker-entrypoint-initdb.d/
and then exec-ing into it:
docker run --env="MYSQL_ROOT_PASSWORD=mypassword" -it theimagejustbuilt bash
but ...
root#73857bf5744e:/# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Solution 1. Specify "127.0.0.1" as the host instead of localhost, i.e. mysql -h 127.0.0.1 -u root instead of mysql. Note that if you omit the host (mysql -u root) the MySQL client will implicitly use localhost.
Solution 2. In /etc/mysql/my.cnf you should see this near the top of the file:
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
Change socket to the location of your MemSQL socket file. By default, this is /var/lib/memsql/data/memsql.sock.
Turns out I was accidentaly overriding the entrypoint via the following command:
docker run --env="MYSQL_ROOT_PASSWORD=mypassword" -it theimagejustbuilt bash
the bash keyword at the end is not needed since it overrides the default entrypoint and connection to mysql service is therefore not possible.
I'm trying to start Vault docker container with mysql storage using this command:
docker run --cap-add=IPC_LOCK -e 'VAULT_LOCAL_CONFIG={"storage": {"mysql": {"username":"root", "password":"hello", "database":"vault", "address":"127.0.0.1:3306"}}, "listener": {"tcp":{"address":"127.0.0.1:8200", "tls_disable":"1"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h"}' -e VAULT_SKIP_VERIFY=true vault server
This is the error I'm getting:
Error initializing storage of type mysql: failed to check mysql schema
exist: dial tcp 127.0.0.1:3306: connect: connection refused
I can connect to mysql using the username and password I am supplying to the previous command.
I also made sure that the mysql is running on the 3306 port
[root#jwahba]# netstat -tlpn | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 39552/mysqld
I checked out the vault official document (here) but it's not obvious what is wrong in my configuration. Any suggestions please ?
You are trying to connect to a db on localhost from a Docker container, but they are on different network stacks. Use --net="host" in your docker run command; 127.0.0.1 in your docker container will now point to your docker host.
Source: From inside of a Docker container, how do I connect to the localhost of the machine?
I created a test_mysql using the following command:
docker run -d -p 3306:3306 --name=test_mysql --env="MYSQL_ROOT_PASSWORD=123" mysql
I got the IP address using docker inspect test_mysql. The IP is 172.17.0.2.
The strange thing is that when I tried to connect to mysql server on my local using
mysql -uroot -p123 -h 172.17.0.2 -P 3306
An error raised:
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.2' (51)
However, if I use the localhost IP address instead it did work:
mysql -uroot -p123 -h 127.0.0.1 -P 3306
My question is why I can't connect to the container use docker inspect result while localhost IP works?
1) while localhost IP works?
Let's see again your command to start container:
docker run -d -p 3306:3306 --name=test_mysql --env="MYSQL_ROOT_PASSWORD=123" mysql
The -p 3306:3306 will "bind" the port 3306 of host to the port 3306 of the container. As a result, we can see that if there is any connections come to port 3306, they will be forwarded to the port of the container.
So, your connections on local IP will work:
mysql -uroot -p123 -h 127.0.0.1 -P 3306
See more detail on Docker page
2) why I can't connect to the container use docker inspect result
It seems your container is connected to the default bridge network(docker0 may have IP:172.17.0.1 in your case ) which is often created by default when you install Docker.
You can find more detail in Docker network page.Therefore, inside your container, you may "see" the bridge (you can try to use ping command", but from your local host, it may not know how to find/resolve the 172.17.0.2 and you got that error.