how to connect remote docker container mysql server - mysql

is there any way to connect remote docker container mysql server?
I am installing magento web application, now I have a situation like I need to use/point the existing remote docker container database. I have make port forwarding in order to access database from remote machine but it doe not work.
docker run -it -d -p 3002:80 -h tm.gworks.mobi -v /var/www/public --privileged --name database magedev
For testing purpose in remote machine I have tried like mysql -u root -h 192.168.1.21:3002 -p in mysql console but it does not connect, it throws error ERROR 2005 (HY000): Unknown MySQL server host '192.168.1.21:3002' (-2)

Docker run command should be,
docker run -it -d -p 3002:3306 -h tm.gworks.mobi -v /var/www/public --privileged --name database magedev
default mysql port is 3306 but I listen port 80 which is my nginx port so it can't be to allow.
mysql -u root -h 192.168.1.21 -P 3002 -p
now everything works fine

Related

Delphi Firedac connect to MySQL in docker container

I have created a MySQL image on my Windows 10 using the default settings from Docker.
I started the container using this command:
docker run --name local-mysql --network="host" -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d <your-docker-Image>
I used the --network parameter in the hope that I could connect to the container from my host computer.
Then I ran this command to connect to the container from the MySQL shell
docker exec -it mysql bash -l
I was able to connect using this
mysql -h localhost -P 3306 --protocol=tcp -u root -p
Using Delphi and setting FireDac to use DriverId MySQL, I specified host as localhost, port 3306, user as root and the password.
But I get this connection error
[FireDAC][Phys][MySQL] Cannot connect to MySQL server on 'localhost:3306' (10061)
I have tried using 127.0.0.1 and 0.0.0.0 without success and with the same error.
I would appreciate it if anyone has tried it with Delphi FireDac to connect to a MySQL container hosted on the same computer.
Thank you in advance.
I was able to solve the issue by using this command to run the image
docker run --name local-mysql -p 127.0.0.1:3307:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d <your-docker-Image>
I used a different host port (3307) to map to the default 3306
I was able to test it using the bash first
mysql -h 127.0.0.1 -P 3306 -u root -p
And in Delphi FireDac, I used the following to connect
Host=127.0.0.1
Port=3307
User_Name=root
Password=my-secret-pw
And all is good. Hope this helps someone who is trying to the same.

Connect to mysql in a Docker container from another Docker container in a MacOS host

I have been trying to set up several Docker containers for different mysql databases in my MacOS host system.
So, I have 2 different mysql databases in containers built from the mysql/mysql-server:5.7 image.
docker run --name=db-1 -d -p 1200:3306 mysql/mysql-server:5.7
docker run --name=db-2 -d -p 1201:3306 mysql/mysql-server:5.7
Since they are binded to 0.0.0.0:120x, I am able to access the mysql cli using
mysql -uroot -p -h 0.0.0.0 -P 120x (WORKS)
However, I really wanted to try and access the databases using the IP of the container which I found using
docker inspect db-1 | grep IPAddress
I found that the IP of the container is 172.17.0.8
So logically
mysql -uroot -p -h 172.17.0.8 -P 3306
should have worked, but it doesn't.
I get an error that reads out ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.8' (60)
I was trying to access using the container IP so I could access the databases from other containers by linking them. However this doesn't seem to work.
What am I missing?

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

I am trying to connect to mysql server using mysql cli. Image was created using following command:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d mysql
The docker container is running. docker ps prints:
johnd#john-pc:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
598c0f8680dc mysql "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp some-mysql
When i enter the following command:
mysql -h 127.0.0.1 -P 3306 -u root -p it returns:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
I also tried with --protocol=tcp attribute. How do i connect from client to mysql server running on docker using terminal from client machine (not from another docker)
EDIT:
I also tried connecting to mysql using this command:
docker run -it --rm mysql mysql -h127.0.0.1 -uexample-user -p
It returns the same error
mysql -h 127.0.0.1 -P 3306 -u root -p
You are connecting to your localhost's sql server but you didn't map the docker's container port to the host.
Try mapping the port to your host by this:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d -p 3306:3306 mysql
Then retry:
mysql -h 127.0.0.1 -P 3306 -u root -p
same error to me, just remember start mysql service first in your container.
exec in your container
/etc/init.d/mysql start
Using internal docker IP like 127.0.0.1 didn't work for me
I used my POD's IP and it worked
mysql -h 10.10.10.41 -P 3306 -u root -p
I still don't understand why I wasn't able to login and before and after this command I was able to login

How to connect to ProxySQL with default config on Docker

I installed docker, got the most popular box with proxySQL.
docker run -d -p 6032:6032 --name proxysql prima/proxysql:latest
then I tried to connect to it from my local mysql like so:
mysql -u admin -padmin -h 127.0.0.1 -P6032
and I'm getting this error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 "Internal error/check (Not system error)"
I tried this trick with twindb/proxysql:latest and prima/proxysql:latest docker images and the result was the same :(
You cannot connect to proxysql from outside the container in default config. bash into the proxysql container and then execute
mysql -u admin -p<password-here> -h 127.0.0.1 -P 6032 --prompt='proxysql>'
default password will be the admin
You need to map 6033 instead of 6032
docker run -d 6033:6033 --name proxysql prima/proxysql:latest
And then run below
mysql -u admin -padmin -h 127.0.0.1 -P6033
Inside the container mysql listens on 127.0.0.1:6032 and for outside connections it listens on 0.0.0.0:6033. So you need to use 6033 for connections from outside the container

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.