问题描述:
环境布置:centos7、docker、mysql
mysql允许远程访问,防火墙也允许mysql端口访问,但是在打开防火墙的情况下还是无法远程连接mysql
mysql容器开放的是 33066 端口
docker run --name mysql -p 33066:3306 \
-v /opt/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
-v /opt/mysql/conf/mysql.cnf:/etc/mysql/conf.d/mysql.cnf \
-v /opt/mysql/data:/var/lib/mysql \
-v /opt/mysql/logs:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8746fa00333 mysql:5.7 "docker-entrypoint.s…" 12 days ago Up 5 hours 33060/tcp, 0.0.0.0:33066->3306/tcp mysql
beca917a5a01 redis "docker-entrypoint.s…" 13 days ago Up 12 days 0.0.0.0:63799->6379/tcp redis
防火墙已打开33066端口
[root@localhost ~]# firewall-cmd --list-ports
33066/tcp 63799/tcp 33066/udp 3306/tcp 3306/udp
mysql已允许远程访问
mysql> select host, user, authentication_string, plugin from mysql.user;
+-----------+---------------+-------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+---------------+-------------------------------------------+-----------------------+
| localhost | root | *2776F491CCF772B093027A82D6559664AA4D6C10 | mysql_native_password |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password |
| % | root | *2776F491CCF772B093027A82D6559664AA4D6C10 | mysql_native_password |
| % | cxrcb | *2776F491CCF772B093027A82D6559664AA4D6C10 | mysql_native_password |
+-----------+---------------+-------------------------------------------+-----------------------+
5 rows in set (0.01 sec)
但是在开启防火墙的时候仍然无法远程连接
关闭防火墙就可以
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# firewall-cmd --state
not running
问题解决:需要在关闭防火墙的情况下,重启docker服务,然后再打开防火墙就可以了
具体步骤:
- 查看当前docker容器
docker ps
- 将正在运行的docker容器先停掉
docker stop mysql
- 关闭docker服务
systemctl stop docker
- 关闭防火墙
systemctl stop firewalld
- 开启docker服务和容器
systemctl start docker
docker start mysql
- 开启防火墙
systemctl start firewalld
注意:以上步骤要在配置好mysql远程访问和开放防火墙端口的前提下