远程连接docker中的mysql容器 报错10060 unknown error解决办法详解。

引言:我在docker里搭建mysql容器里通过navicat 远程连接docker里的mysql结果发现连接不了出现了一个10060 unknow error,结果搞了一下午才解决掉,找了大半天,最终还是防火墙上的问题,希望写此博客给那些还在迷茫的人们指一条明路。


环境:centOs-7,在docker里部署mysql容器。


总结原因:要么是网络问题,要么就是权限问题,要么就是防火墙的问题(iptables),要么就是端口号不正确。


1、如果你到了下面这一步,说明你的网络是没问题的

[root@localhost ~]# docker exec -it mysql bash//进入容器的命令总会吧。

root@216c535c46c7:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)


2、权限问题,如果权限问题的话,你就尝试更改一下权限,mysql账户是否不允许远程连接。如果无法连接可以尝试以下方法

 mysql -u root -p //登录MySQL

 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION; //任何远程主机都可以访问数据库

 mysql> FLUSH PRIVILEGES; //需要输入次命令使修改生效


3、防火墙问题:很多时候在liunx系统上安装了web服务应用后(如tomcat、apache等),需要让其它电脑能访问到该应用,而linux系统(centos-7、redhat等)的防火墙是默认只对外开放了22端口,可能没有对mysql的3306开放端口。

而centOs宿主机的端口设置在/etc/sysconfig/iptables文件中配置。

vi编辑器的用法:esc:命令行模式/插入模式;i:插入,wq:保存推出,q:退出,q!:不保存退出

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT 放在下面的位置,不要乱放,这就开放了mysql远程连接的端口,然后重启一下iptables的服务就ok了。

[root@localhost ~]# vi /etc/sysconfig/iptables

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT//centos-7默认的端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT//tomcat端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT //mysql端口

-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
~

[root@localhost ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@localhost ~]#


4、以下是效果图。



结束语:希望能帮到大家。
 

猜你喜欢

转载自blog.csdn.net/liqz666/article/details/82225575
今日推荐