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

旧时模样 提交于 2019-12-09 03:16:11

问题


  • Server ip: 172.16.1.169
  • mysql user name: root
  • passwd: xxxxxxxxxx
  • database name: example

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:

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


回答1:


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:

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:

service mysqld start

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) check the port on which mysqld is running on the server:

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:

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.cnf search for the line:

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

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



回答2:


  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




回答3:


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

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

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



回答4:


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

Save and restart mysql service.



来源:https://stackoverflow.com/questions/5218733/problems-in-connecting-to-mysql-server-error-2003-hy000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!