ERROR 2003 (HY000): Can't connect to MySQL server on 'ip'(111)

來源:互聯網
上載者:User

標籤:ade   tar   允許   order   ini   name   127.0.0.1   linu   input   

問題描述:
 從一台linux遠端連線另一台linux上的MySQL, 出現ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘ip‘(111)錯誤。 [[email protected] ~]$ mysql -hxxx.xxx.xxx.85 -uroot -pEnter password:  123456  ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘xxx.xxx.xxx.85‘ (111)
問題分析:1.可能網路連接問,遠程ping xxx.xxx.xxx.85 ,能ping通,排除此情況 [[email protected] ~]$ 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的監聽  當然也不讓本地java程式串連MySQL(Connector/J只能通過TCP/IP來串連)。2.2 可能使用了bind_address=127.0.0.1(當然也可以是其他ip)[mysqld] bind_address=127.0.0.1請把這行注釋掉 #bind_address=127.0.0.1
 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進行監聽。 netstat -nplt | grep mysql tcp  0  0 0.0.0.0:3306 0.0.0.0:*  LISTEN 11107/mysqld  測試連接:  mysql -u root -p -h xxx.xxx.xxx.85 --port 3306 

6.最坑的防火牆:
1.檢查防火牆狀態 [[email protected] ~]# service iptables  statusRedirecting to /bin/systemctl status  iptables.serviceiptables.service - IPv4 firewall with iptables   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled)   Active: active (exited) since Wed 2016-11-02 23:10:51 CST; 14min ago  Process: 12024 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS)  Process: 12078 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 12078 (code=exited, status=0/SUCCESS)Nov 02 23:10:51 iZ288zn7gymZ iptables.init[12078]: iptables: Applying firewall rules: [  OK  ]Nov 02 23:10:51 iZ288zn7gymZ systemd[1]: Started IPv4 firewall with iptables.正常啟動。

 

查看是否放開mysql連接埠 

 [[email protected] ~]# iptables -L -n   (或者: iptables --list  )

 

 

Chain INPUT (policy DROP)

 

target     prot opt source               destination         ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHEDACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:21ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:3306ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:443ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            limit: avg 100/sec burst 100ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            limit: avg 1/sec burst 10syn-flood  tcp  --  0.0.0.0/0            0.0.0.0/0            tcp flags:0x17/0x02REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

 如果沒有 3306 加入防火牆規則:

 iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

注意這樣只能臨時加入防火牆  需要把規則save到   /etc/sysconfig/iptables 檔案下

[[email protected] ~]# service iptables saveiptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
或者簡單粗暴
直接vim /etc/sysconfig/iptables
增加一行  -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
完成後我的如下:

 

# Generated by iptables-save v1.4.21 on Thu Jan 28 19:16:55 2016*filter:INPUT DROP [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [1:152]:syn-flood - [0:0]-A INPUT -i lo -j ACCEPT-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT-A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood-A INPUT -j REJECT --reject-with icmp-host-prohibited-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN-A syn-flood -j REJECT --reject-with icmp-port-unreachableCOMMIT# Completed on Thu Jan 28 19:16:55 2016

 然後重啟防火牆:

[[email protected] ~]# service iptables restart

 

Redirecting to /bin/systemctl restart  iptables.service

PS. 請保證mysql 進程正常啟動的前提下 逐一排查以上幾點。
參考: MySQL遠端連線ERROR 2003 (HY000):Can‘t connect to MySQL server on‘XXXXX‘的問題
mysql許可權及密碼問題見:http://www.cnblogs.com/wangdaijun/p/5312424.html

 

 

 

ERROR 2003 (HY000): Can't connect to MySQL server on 'ip'(111)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.