docker container Mysql Port outside access issue - mysql

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.

Related

How to connect to mysql container?

I am running official mysql docker image on Linux:
docker run --name somemysql -e MYSQL_ROOT_PASSWORD=pass -p 3306:3306 -d mysql
How do I connect to the database from outside of the container?
You must have to specify the port to container with something like -p 3306:3306, which means you redirect your host 3306 port to the 3306 container port and then you just need connect by exec to the container like:
$ docker exec -it somemysql bash and you can access it easily, otherwise if you want to connect via mysql client use:
$ mysql -h 127.0.0.1 -u root -p
It's important to specify the localhost IP. If you don't, you wont be able to connect. The port 3306 can't be in use when you assign it.
First you have to open the Ports via -p expose is Not enough, since it is used in a network for multiple container to find each other without connection to the outside. Then you can connect to your container with exec and then via SSL

MySQL: How to access MySQL DB running inside a docker instance on a Ubuntu machine

I have a Ubuntu machine with IP = 172.16.12.134. On this Ubuntu machine, I have a docker instance of MySQL image running. The IP address of docker instance is like 172.18.0.8.
I am able to access db from ubuntu machine terminal with command like mysql -h 172.18.0.8 -P 3306 -u root -p.
Is there a way to access the db from outside(any other machine)the ubuntu machine?
like mysql -h 172.16.12.134 -P 3306 -u root -p.
I exported the port of docker in yml file as -port 3306:3306.
I don't see the problem here. Your first step would be to expose the MySQL service to your host machine, which you have done using -port 3306:3306.
You verified this to be working which means MySQL 'appears' to be running at 172.16.12.134:3306. Therefore the only thing left to do is allowing connections on port 3306 in your firewall. This way people can connect using your Ubuntu machine host IP and the respective port 3306.

Localhost connect MySQL Docker confusion

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.

Accessing a Docker MySQL server from the host machine, without needing the container's IP address

I'm writing setup instructions for an application needing a MySQL database, and I'd like it to be easy even for people that don't have a MySQL installation.
Therefore, I'd like to run the MySQL server in a container:
docker run -p 3306:3306 \
--name mysql \
-e MYSQL_ROOT_PASSWORD=test \
-e MYSQL_DATABASE=my_db \
-d mysql
And to be able to connect to it this way:
mysql -u root -ptest -D my_db
Which doesn't work because the MySQL server only listens locally on the container.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
So I see three options, none of which fitting my needs:
1) Using the container IP everytime
mysql -u root -ptest -D my_db -h 172.17.0.4
Annoying, the IP will often change and I don't want people to have to update their configuration this much.
2) Changing the configuration of MySQL inside the container
Requires to run a docker exec ... each time the container is run, so it's annoying as well.
3) Making a custom image where the configuration suits my needs
Seems a bit overkill, I'm pretty sure there is a better solution.
Thoughts?
When the mysql client is invoked as you did:
mysql -u root -ptest -D my_db
(i.e. without the hostname) or with localhost as hostname it tries to connect to the local server using Unix pipes. A pipe is a special file type and the client communicate with the server through it.
Your MySQL server is not local, it runs on a separate machine. You cannot connect to it using pipes.
As you already noticed, it works if you use the IP address of the container as argument for the -h command line option of mysql.
You also map the port 3306 of the container to port 3306 of the host machine. This means any TCP connection to port 3306 of the local machine goes through this mapping to the port 3306 of the container, where the MySQL server is listening.
Combining the two above, all you have to do is to put 127.0.0.1 as host name in the mysql command line:
mysql -u root -ptest -D my_db -h 127.0.0.1

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?
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
First, check netstat -an to make sure the port is open in Windows. If it is, also check the Windows firewall to make sure nothing is blocking connections to the port.
Most of my Docker experience is in CoreOS, so I'm not exactly sure how Windows handles routing traffic into the container. In CoreOS, it uses a proxy. If there is a proxy in Windows, make sure nothing is interfering with it.
Changing the port in which I was running the image worked.
I checked if this port was used by something else, but it was not used. so I just -desperately- run a new container in a different port '3309'. and it worked fine!
Make sure you have entered the port with -P.