Linux 连接mysql服务器的问题:ERROR 2003 (HY000)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5218733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:09:13  来源:igfitidea点击:

Problems in connecting to mysql server: ERROR 2003 (HY000)

mysqllinuxfedoramysql-error-2003

提问by Jagan

  • Server ip: 172.16.1.169
  • mysql user name: root
  • passwd: xxxxxxxxxx
  • database name: example
  • 服务器ip:172.16.1.169
  • mysql用户名:root
  • 密码:xxxxxxxxxxxx
  • 数据库名称:示例

I'm trying to access a database from a client (ip 172.16.0.114). Both the server and client are running the Fedora distribution of Linux. What settings need to be configured, and what should they be set to, for both the server and client? How do I access a specific database (here, "example")? I tried but I got an error:

我正在尝试从客户端(IP 172.16.0.114)访问数据库。服务器和客户端都运行 Linux 的 Fedora 发行版。需要为服务器和客户端配置哪些设置,应该将它们设置为什么?如何访问特定数据库(此处为“示例”)?我试过了,但出现错误:

ERROR 2003 (HY000): Can't connect to MySQL server on '172.16.1.169'.

错误 2003 (HY000):无法连接到“172.16.1.169”上的 MySQL 服务器。

回答by guido

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.

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

There are various possible causes to that:

有多种可能的原因:

1) check that mysqld is running on the server:

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

ps -ef | grep mysqld

should return something like:

应该返回如下内容:

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 ...

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

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

service mysqld start

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

或者在 Fedora 版本 >= 16 上,它依赖于 systemd:

systemctl start mysqld.service

and for enabling daemon auto-startup at system boot:

以及在系统启动时启用守护程序自动启动:

systemctl enable mysqld.service

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

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

netstat -lnp | grep mysql

should return:

应该返回:

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

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:

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

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

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

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

bind_address=127.0.0.1

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

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

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

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

service iptables status

回答by Pankaj Khurana

  1. Open MySQL config file

    sudo vim my.cnf

  2. Ensure that the following are commented out.

    #skip-external-locking

    #skip-networking

    #bind-address = xx.xx.xx.xx

    Save and exit

  3. Restart mysql service

  1. 打开 MySQL 配置文件

    须藤vim my.cnf

  2. 确保注释掉以下内容。

    #skip-external-locking

    #skip-networking

    #绑定地址= xx.xx.xx.xx

    保存并退出

  3. 重启mysql服务

回答by Vanmen

I think the destination mysqlserver might use a different port. You have to find the correct port first.

我认为目标mysql服务器可能使用不同的端口。您必须先找到正确的端口。

Once you get the correct port you can connect to that mysqlserver by using this command:

获得正确的端口后,您可以mysql使用以下命令连接到该服务器:

mysql -h 172.16.1.169 -P (port) -u root -p (password)

回答by Ramil Mammadov

In MySQL config file (/etc/mysql/my.cnf) comment '#bind-address = 127.0.0.1'

在 MySQL 配置文件 (/etc/mysql/my.cnf) 中注释 '#bind-address = 127.0.0.1'

Save and restart mysql service.

保存并重启mysql服务。