Showing posts with label docker remote connection. Show all posts
Showing posts with label docker remote connection. Show all posts

Thursday, January 9, 2020

Connect to Docker Mysql remotely using public IP

You want to connect to mysql server which is installed and configured inside docker container, from host or from remote server. To achieve this you need to make sure your host system has public IP. Docker container is a virtual machine. The system where docker is installed known as Host Machine. You can connect from Host Machine to Docker container Mysql server using private IP and public IP both.

Here I am going to show how to connect docker container mysql server using public IP.

1. Your docker container mysql server port (default 3306) should be mapped with one of the port of host system. If no mysql server is running on host system then you can map the same port i.e. 3306 but if a mysql server is running on host system, you need to map docker container mysql server with another port of host system. Suppose it is 3500. When you launch the container using docker run command, you can pass attribute -p with it to map ports between container and host system.


2. Now next thing is, your docker container mysql server config should not be binded to local host or 127.0.0.1 only. It means, this mysql server can be connected from localhost only. As we are connecting mysql server remotely, this option should be commented.

#bind-address = 127.0.0.1
Once you comment it in mysql config, restart the mysql server.

3. Now you need to give global access permission to mysql user. It is your mysql user, you are going to connect into mysql server using this username.
To give global access to mysql user, Host should be % in user table and db table both for the mysql user.
Make sure you have updated Host as % for same combination of user and database in db table.


4. Now make sure port 3500 is opened in your firewall and it can be accessed remotely

5. Now try to connect docker mysql using public ip

mysql -u mysqlusername -p -h 209.45.xx.xx -P 3500
As 3500 is the port where we mapped docker mysql port to Host system port, we will pass this in command to connect to docker mysql server through host system.

Now you should be able to conenct to docker mysql server remotely.