MySQL error ERROR 2002 of HY000: Can 't to local server through socket

  • 2020-08-22 22:52:46
  • OfStack

After installing MySQL and using the new configuration file, the MySQL server started successfully, but at the time of login, ERROR 2002 (HY000) appeared: Can 't connect local MySQL server socket, that is, it could not connect to the mysql server through socket and provided the location of the socket file. The following is a description of the problem and the solution.

1. Malfunction


[root@SZDB mysqldata]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/data/mysqldata/mysql.sock' (111)
# Fault environment
[root@SZDB mysqldata]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m

2. Fault analysis


# To view mysql State of the instance
[root@SZDB mysqldata]# netstat -ntlp  | grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      13001/mysqld
# To view my.cnf about socket The configuration of the
[root@SZDB mysqldata]# more /etc/my.cnf |grep sock
socket = /tmp/mysql.sock
# By a on the my.cnf Is defined in /tmp Directory, and the error message is /data/mysqldata/ directory
# That is to say, mysqld It has been claimed to be correct sock File, but the client connection is still found from the original directory sock file
# Let's look at the background log. There's one ERROR , is about a full query log, and is an error caused by a directory that does not exist, regardless of the current failure
[root@SZDB mysqldata]# more SZDB.err
             ............
2014-10-11 13:17:21 13001 [Note] InnoDB: 5.6.12 started; log sequence number 1625997
/app/soft/mysql/bin/mysqld: File '/log/mysql_logs/slowquery.log' not found (Errcode: 2 - No such file or directory)
2014-10-11 13:17:21 13001 [ERROR] Could not use /log/mysql_logs/slowquery.log for logging (error 2). Turning logging off for the who
le duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.
2014-10-11 13:17:21 13001 [Note] Server hostname (bind-address): '*'; port: 3306
2014-10-11 13:17:21 13001 [Note] IPv6 is available.
2014-10-11 13:17:21 13001 [Note]   - '::' resolves to '::';
2014-10-11 13:17:21 13001 [Note] Server socket created on IP: '::'.
2014-10-11 13:17:21 13001 [Note] Event Scheduler: Loaded 0 events
2014-10-11 13:17:21 13001 [Note] /app/soft/mysql/bin/mysqld: ready for connections.
Version: '5.6.12-log'  socket: '/tmp/mysql.sock'  port: 3306  Source distribution

3. Troubleshooting

a, by configuring my. cnf mysql option socket file location


# First stop mysql The server
[root@SZDB mysqldata]# service mysqld stop
Shutting down MySQL.[  OK  ]
# Modify the my.cnf As shown in the following
[root@SZDB mysqldata]# vi /etc/my.cnf
[mysql]
no-auto-rehash
socket = /tmp/mysql.sock  # Add the bank
# restart mysql The server
[root@SZDB mysqldata]# service mysqld start
Starting MySQL..[  OK  ]
# Reconnect normal
[root@SZDB mysqldata]# mysql -uroot -p
Enter password:
mysql> show variables like 'version';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.6.12-log |
+---------------+------------+

b. Establish a link for socket file


[root@SZDB mysqldata]# ln -s /tmp/mysql.sock /data/mysqldata/mysql.sock
ln: creating symbolic link `/data/mysqldata/mysql.sock' to `/tmp/mysql.sock': File exists
[root@SZDB mysqldata]# rm mysql.sock    # The above indicates that the file exists, so delete the previous one mysql.sock file
[root@SZDB mysqldata]# ln -s /tmp/mysql.sock /data/mysqldata/mysql.sock
[root@SZDB mysqldata]# ls -hltr mysql.sock
lrwxrwxrwx 1 root root 15 Oct 11 14:00 mysql.sock -> /tmp/mysql.sock
[root@SZDB mysqldata]# mysql -uroot -p
Enter password:
mysql> show variables like 'socket';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| socket        | /tmp/mysql.sock |
+---------------+-----------------+


Related articles: