I’m setting up a MySQL server inside a Docker container
sudo docker run --name test-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -e MYSQL_USER=desktop -e MYSQL_PASSWORD=password -e MYSQL_ROOT_HOST=% -d mysql
For some reason I can’t figure out, an external machine cannot connect to it, no matter what I try.
Here is the content of my.cnf
root@f9042fc2a105:/# cat /etc/mysql/my.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
Here is docker.cnf
root@f9042fc2a105:/# cat /etc/mysql/conf.d/docker.cnf
[mysqld]
skip-host-cache
skip-name-resolve
Here is mysql.cnf
# Copyright (c) 2015, 2021, Oracle and/or its affiliates.
[mysql]
Here is the content of the User table
mysql> select user, host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| desktop | % |
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)
Here is the error I get when connecting from an external machine using MySQL shell:
MySQL JS > connect --mysql [email protected]
Creating a Classic session to '[email protected]'
Please provide the password for '[email protected]': ********
MySQL Error 2013 (HY000): Lost connection to MySQL server at 'waiting for initial communication packet', system error: 10060
The docker host machine has no firewall.
2
Answers
You should check the firewall rules.
Maybe you have to allow something like:
Did you manage to ping the instance of MySQL on the port 3306?
By the way keep in mind that allowing any is not safe, you should allow only your IP o the one you are trying to connect from.
It seems that if you have to activate and allow the port that MySQL is listening to, which is
3306
, due to the port mapping-p 3306:3306
in your description. And also check if MySQL remote access is enabled or not.You can read about the default inactive firewall here.
You may try the below steps:
Step 1: Setting up the rules BEFORE activate your ufw firewall.
Make sure you allow ssh port too if the host is a remote machine. If anything happens, at least we can ssh to it and find what is happening.
Step 2: Activate your firewall
Step 3: Check the host machine’s firewall status
It should be somewhat like this
Step 4: Enabling MySQL remote access. By default, MySQL remote access is disabled. Find the
mysqld.cnf
file of your MySQL in the containerand add the configuration below