且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

连接到mysql服务器时出现问题:ERROR 2003(HY000)

更新时间:2022-03-23 10:18:59

该错误消息是由客户端(而非服务器)生成的,因为已尝试与服务器建立连接,但无法访问服务器.

That error message is generated by the client (not the server) because a connection to the server has been attempted but the server could not be reached.

有多种可能的原因:

1)检查mysqld是否在服务器上运行:

1) check that mysqld is running on the server:

ps -ef | grep mysqld

应返回以下内容:

root      2435  2342  0 15:49 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/var/ --user=mysql  
mysql     2480  2435  0 15:49 pts/1    00:00:00 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/var/ --user=mysql ...

要运行守护程序服务,请在redhat/fedora/centos上运行:

To run the daemon service, run on redhat/fedora/centos:

service mysqld start

或Fedora发行版> = 16,它依赖于systemd:

or on Fedora release >= 16, which relies on systemd:

systemctl start mysqld.service

并用于在系统引导时启用守护程序自动启动:

and for enabling daemon auto-startup at system boot:

systemctl enable mysqld.service

2)检查服务器上运行mysqld的端口:

2) check the port on which mysqld is running on the server:

netstat -lnp | grep mysql

应返回:

tcp        0      0 0.0.0.0:3306 0.0.0.0:* LISTEN 2480/mysqld 
unix  2      [ ACC ]     STREAM     LISTENING     8101   2480/mysqld /tmp/mysql.sock

后者是用于本地连接的套接字,第一个是用于网络连接的tcp端口(默认为3306).如果该端口不是默认端口,则必须在客户端上设置连接端口.如果使用mysql客户端:

the latter is the socket for local connections, the first the tcp port for networking (default 3306). If the port is not the default port, you must set the connection port on the client. If using mysql client:

mysql dbname -uuser -ppasswd -P<port> ...

3)位于不同的网络地址,请检查服务器是否侦听您要连接的网络地址:在文件/etc/my.cnf中搜索该行:

3) being on a different net address, check that the server listens for the net addrees your are connecting from: in file /etc/my.cnf search for the line:

bind_address=127.0.0.1

如果地址是127.0.0.1,则仅允许本地连接;如果是172.16.1.0,则无法从172.16.2.xxx连接

if the address is 127.0.0.1 only local connections are allowed; if it were 172.16.1.0, you could not connect from 172.16.2.xxx

4)检查服务器上是否没有运行防火墙并阻止与mysql端口的连接(默认端口3306);如果是redhat/fedora/centos运行

4) check that on the server there is no firewall running and blocking connections to mysql port (3306 is the default port); if it's a redhat/fedora/centos run

service iptables status