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

本文介绍了当遇到MySQL服务器无法从远程主机进行连接的问题时的排查步骤及解决方案。具体包括检查MySQL服务器是否允许远程连接、验证网络连通性、确认MySQL监听端口设置,并最终通过修改配置文件中的bind-address参数来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.MySQL本地可以登录,但是不能远程登录。

[root@nb0 ~]# mysql -h192.168.1.157 -uroot -p
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.157' (111)

2. MySQL服务器是可以ping通的

[root@nb0 ~]# ping 192.168.1.157
PING 192.168.1.157 (192.168.1.157) 56(84) bytes of data.
64 bytes from 192.168.1.157: icmp_seq=1 ttl=64 time=0.242 ms
64 bytes from 192.168.1.157: icmp_seq=2 ttl=64 time=0.262 ms
^C
--- 192.168.1.157 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.242/0.252/0.262/0.010 ms

3. MySQL的端口是默认的

[root@my ~]# netstat -nlp|grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      24061/mysqld    

4.解决办法

将mysql配置文件中的bind-address = 127.0.0.1注释掉

[root@my ~]# vi /etc/mysql/mysql.conf.d/mysqld.cnf 

#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1

如果安装的是Mariadb10,对应目录文件会发生一些改变

[root@my ~]# cd /etc/mysql/mariadb.conf.d
[root@my ~]/etc/mysql/mariadb.conf.d# ls
50-client.cnf  50-mysql-clients.cnf  50-mysqld_safe.cnf  50-server.cnf

然后编辑50-server.cnf文件,同样是注释掉bind-address

vi 50-server.cnf

重启MySQL

[root@my ~]:~# sudo service mysql restart

重新远程连接

[root@nb0 ~]# mysql -h192.168.1.157 -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.11-0kord6 (Ubuntu)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值