MySQL遠端連線出現ERROR 2003 (HY000):Can't connect to MySQL server on'XXXXX'的問題

來源:互聯網
上載者:User

標籤:mysql


MySQL遠端連線ERROR 2003 (HY000):Can‘t connect to MySQL server on‘XXXXX‘的問題


問題描述:

 

從一台linux遠端連線另一台linux上的MySQL, 出現ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘xxx.xxx.xxx.85‘(111)錯誤。

 

[[email protected] ~]$ 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)

[[email protected] ~]$ perror 111

OS error code 111: Connection refused

查看errorCode

 

[[email protected] ~]$ perror 111 

OS error code 111: Connection refused

 

問題分析:

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的監聽  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

 

[[email protected] ~]$ mysql -hxxx.xxx.xxx.85 -uroot -p --port=3308 

Enter password: 

Welcome to the MySQL monitor. Commands end with ; or \g.

 

為什麼出現這麼低級的錯誤呢?

因為我一直在用85上的MySQL, 而且每次都是直接用mysql -uroot就串連上了,沒有指定--port,這樣我就一直以為這MySQL的port一直是預設的3306的。

 

其實根本原因是:

 

1. MySQL本地串連,如果不指mysql --protocol=tcp, 串連預設是socket方式串連的。這點大家都知道。  www.2cto.com  

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.

再次說明基礎細節很重要啊。















本文出自 “見” 部落格,請務必保留此出處http://732233048.blog.51cto.com/9323668/1635940

MySQL遠端連線出現ERROR 2003 (HY000):Can't connect to MySQL server on'XXXXX'的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.