CentOS的MySQL遠程連接ERROR 2003 (HY000):Can't connect to MySQL server on'XXXXX'(111) 的問題

   今天在CentOS7虛擬機上安裝了mysql,想要在宿主機上遠程連接,結果一直報錯,就百度了原因,這裏分享一篇博客以及自己的一點知識。

   引用:

問題描述:

從一臺linux遠程連接另一臺linux上的MySQL, 出現ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.85'(111)錯誤。

[mysql@vvmvcs0 ~]$ mysql -hxxx.xxx.xxx.85 -uroot -p
Enter password: www.2cto.com
ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.85' (111)

[mysql@vvmvcs0 ~]$ perror 111
OS error code 111: Connection refused
查看errorCode
[mysql@vvmvcs0 ~]$ perror 111
OS error code 111: Connection refused

問題分析:
1,可能網絡連接問,遠程ping xxx.xxx.xxx.85 ,能ping通,排除此情況

[mysql@vvmvcs0 ~]$ ping xxx.xxx.xxx.85
PING xxx.xxx.xxx.85 (xxx.xxx.xxx.85) 56(84) bytes of data.
64 bytes from xxx.xxx.xxx.85: icmp_seq=1 ttl=63 time=0.230 ms

2,排查可能由於85上my.cnf裏配置了skip_networking或者bind_address,只允許本地socket連接

2.1 在[mysqld]下設置skip_networking,
知識說明: 這使用MySQL只能通過本機Socket連接(socket連接也是本地連接的默認方式),放棄對TCP/IP的監聽 www.2cto.com
當然也不讓本地java程序連接MySQL(Connector/J只能通過TCP/IP來連接)。

2.2 可能使用了bind_address=127.0.0.1(當然也可以是其他ip)
[mysqld]
bind_address=127.0.0.1
知識說明:這種情況可以TCP/IP連接
通過查看了my.cnf文件,以上兩個都是沒設置的,排除掉這兩種情況

3,排查DNS解析問題,檢查是否設置了: skip_name_resolve。 這個情況肯定不可能,因爲我用的是ip,不是主機名。
[mysqld]
skip_name_resolve
知識說明:這個參數加上後,不支持主機名的連接方式。
4, 排查用戶和密碼問題, 其實用戶和密碼的錯誤,不會出現111的,所以排除用戶密碼問題
ERROR 1045 (28000): Access denied for user 'root'@'XXXX' (using password: YES)

5,排查--port問題,有可能85的MySQL port不是默認3306, 這樣我遠程連接時,沒有指定--port,用的是3306, 而85上沒有對3306進行監聽。
ps -ef | grep mysqld
果然是: 85上的MySQL使用的是3308 port.
最終連接方式:加上--port=3308
[mysql@vvmvcs0 ~]$ mysql -hxxx.xxx.xxx.85 -uroot -p --port=3308
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
其實根本原因是:
1. MySQL本地連接,如果不指mysql --protocol=tcp, 連接默認是socket方式連接的。這點大家都知道。
2, MySQL socket連接是根據sokect文件來的,與--port不相關的,如果是一機多實例,則用-S(或者--socket=name )來指定連接哪個實例。
就是這個socket連接對--port無識別效果,導致排查這個問題這麼久。
見下面: 其實85上只有一個port爲3308的MySQL實例,但是用3306仍然是連接上此實例,說明socket連接方式忽略--port參數。
-bash-3.2$ mysql -uroot --port=3308
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql -uroot --port=3306
Welcome to the MySQL monitor. Commands end with ; or \g.
再次說明基礎細節很重要啊。


   以上是博主分享的各種問題,我經過了上邊所有排查,結果依然連接不上。突然靈機一動就想到了系統防火牆,這麼重要的細節自己竟然忘掉了,centOS上的防火牆還開着呢,怎麼能遠程連接。然後關閉防火牆就實現了完美連接。

    這裏附上centos有關防火牆的操作:

CentOS6.5查看防火牆的狀態:

1
[linuxidc@localhost ~]$service iptable status

  顯示結果:

1
2
3
4
5
[linuxidc@localhost ~]$service iptable status
Redirecting to /bin/systemctl status  iptable.service
● iptable.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)  --表示防火牆已經關閉

    CentOS 6.5關閉防火牆

1
2
[root@localhost ~]#servcie iptables stop                    --臨時關閉防火牆
[root@localhost ~]#chkconfig iptables off                    --永久關閉防火牆

    CentOS 7.2關閉防火牆

CentOS 7.0默認使用的是firewall作爲防火牆,這裏改爲iptables防火牆步驟。


firewall-cmd --state #查看默認防火牆狀態(關閉後顯示notrunning,開啓後顯示running)

1
2
[root@localhost ~]#firewall-cmd --state
not running

   檢查防火牆的狀態:

從centos7開始使用systemctl來管理服務和程序,包括了service和chkconfig。

1
2
[root@localhost ~]#systemctl list-unit-files|grep firewalld.service            --防火牆處於關閉狀態
firewalld.service                          disabled

  或者

1
2
3
4
[root@localhost ~]#systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

   關閉防火牆:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啓動

1
2
[root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#systemctl disable firewalld.service
1
2
3
4
5
6
7
8
啓動一個服務:systemctl start firewalld.service
關閉一個服務:systemctl stop firewalld.service
重啓一個服務:systemctl restart firewalld.service
顯示一個服務的狀態:systemctl status firewalld.service
在開機時啓用一個服務:systemctl enable firewalld.service
在開機時禁用一個服務:systemctl disable firewalld.service
查看服務是否開機啓動:systemctl is-enabled firewalld.service;echo $?
查看已啓動的服務列表:systemctl list-unit-files|grep enabled




CentOS6.5查看防火牆的狀態:

1
[linuxidc@localhost ~]$service iptable status

  顯示結果:

1
2
3
4
5
[linuxidc@localhost ~]$service iptable status
Redirecting to /bin/systemctl status  iptable.service
● iptable.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)  --表示防火牆已經關閉

    CentOS 6.5關閉防火牆

1
2
[root@localhost ~]#servcie iptables stop                    --臨時關閉防火牆
[root@localhost ~]#chkconfig iptables off                    --永久關閉防火牆

    CentOS 7.2關閉防火牆

CentOS 7.0默認使用的是firewall作爲防火牆,這裏改爲iptables防火牆步驟。


firewall-cmd --state #查看默認防火牆狀態(關閉後顯示notrunning,開啓後顯示running)

1
2
[root@localhost ~]#firewall-cmd --state
not running

   檢查防火牆的狀態:

從centos7開始使用systemctl來管理服務和程序,包括了service和chkconfig。

1
2
[root@localhost ~]#systemctl list-unit-files|grep firewalld.service            --防火牆處於關閉狀態
firewalld.service                          disabled

  或者

1
2
3
4
[root@localhost ~]#systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

   關閉防火牆:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啓動

1
2
[root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#systemctl disable firewalld.service
1
2
3
4
5
6
7
8
啓動一個服務:systemctl start firewalld.service
關閉一個服務:systemctl stop firewalld.service
重啓一個服務:systemctl restart firewalld.service
顯示一個服務的狀態:systemctl status firewalld.service
在開機時啓用一個服務:systemctl enable firewalld.service
在開機時禁用一個服務:systemctl disable firewalld.service
查看服務是否開機啓動:systemctl is-enabled firewalld.service;echo $?
查看已啓動的服務列表:systemctl list-unit-files|grep enabled

CentOS6.5查看防火牆的狀態:

1
[linuxidc@localhost ~]$service iptable status

  顯示結果:

1
2
3
4
5
[linuxidc@localhost ~]$service iptable status
Redirecting to /bin/systemctl status  iptable.service
● iptable.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)  --表示防火牆已經關閉

    CentOS 6.5關閉防火牆

1
2
[root@localhost ~]#servcie iptables stop                    --臨時關閉防火牆
[root@localhost ~]#chkconfig iptables off                    --永久關閉防火牆

    CentOS 7.2關閉防火牆

CentOS 7.0默認使用的是firewall作爲防火牆,這裏改爲iptables防火牆步驟。


firewall-cmd --state #查看默認防火牆狀態(關閉後顯示notrunning,開啓後顯示running)

1
2
[root@localhost ~]#firewall-cmd --state
not running

   檢查防火牆的狀態:

從centos7開始使用systemctl來管理服務和程序,包括了service和chkconfig。

1
2
[root@localhost ~]#systemctl list-unit-files|grep firewalld.service            --防火牆處於關閉狀態
firewalld.service                          disabled

  或者

1
2
3
4
[root@localhost ~]#systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

   關閉防火牆:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啓動

1
2
[root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#systemctl disable firewalld.service
1
2
3
4
5
6
7
8
啓動一個服務:systemctl start firewalld.service
關閉一個服務:systemctl stop firewalld.service
重啓一個服務:systemctl restart firewalld.service
顯示一個服務的狀態:systemctl status firewalld.service
在開機時啓用一個服務:systemctl enable firewalld.service
在開機時禁用一個服務:systemctl disable firewalld.service
查看服務是否開機啓動:systemctl is-enabled firewalld.service;echo $?
查看已啓動的服務列表:systemctl list-unit-files|grep enabled
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章