【linux错误解决】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)错误。
 
[mysql@vvm vcs0 ~]$ 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.
 
为何出现这么低级的错误呢?
由于我一直在用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.
再次说明基础细节很重要啊。

版权声明:本文为博主原创文章,未经博主容许不得转载。java