root@iZufkfljcZ:~# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
I searched online for a long time, but all I found were repeated articles and did not explain why this problem occurred. I searched for a day and then found an article explaining the function of mysql.sock file. Then I analyzed why this problem occurred and made a summary for future reference.
The problem I have is: can’t find mysql.sock if you can run it
find/-name mysql.sock
This command, and can look up the results, just look up the results of a soft connection to the/TMP directory can be resolved (the Internet is so resolved).
However, after I executed this statement, there was no response and I did not find the mysql.sock file.
Before we do that, we need to understand what this mysql.sock file is for.
The localhost connection is usually made through a Unix domain socket file, typically/TMP /mysql.sock. If the socket file is deleted, the local customer cannot connect. This may occur when your system is running a CRon task that removes temporary files under/TMP.
If you are unable to connect because of a lost socket file, you can simply recreate it by restarting the server. Because the server recreates it at startup.
If, like me, restarting the server doesn’t change anything, you can start by executing the following statement:
# mysql -uroot -h 127.0.0.1 -p
Not surprisingly, this sentence should be enforceable,
You can’t make a socket connection now because it’s gone, so you can make a TCP/IP connection
If the socket file is deleted by a cron task, the problem will repeat itself unless you modify the CRon task or use a different socket file or use a different socket file. My solution is to re-specify a different socket, or I don’t have a mysql.sock file at the moment, so I’m going to figure out a way to generate one.
First, change the my.cnf file. The directory in my server is /etc/my.cnf. If not, you can use find to find it,
The next step is to save the exit, make sure the directory exists, and change the permissions on the directory
# chmod 777 /var/lib/mysql
The preparation steps are done, and then the mysql and mySQLD services restart
# service mysql restart
# service mysqld restart
When I restarted mySQLD service, the reboot failed, as shown below:
root@iZufkfljcZ:~# service mysqld start
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalc
tl -xe" for details.
At this time, the prompt can input SystemCTL Status mysqld.service to see the specific reason for the failure, then:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
● mysqld.service – LSB: start and stop MySQL
Loaded: Loaded (/etc/init.d/mysqld; bad; Vendor presets: enabled)
Active: failed(Result: exit-code) since March 2017-12-20 10:38:30 CST; 45s ago
Docs: man:systemd-sysv-generator(8)
Process: 2154 ExecStart=/etc/init.d/mysqld start (code=exited, status=1/FAILURE)
. Starting LSB: start and stop MySQL…
December 20 10:38:29 iZufkfljcZ mysqld[2154]:
: iZufkfljcZ mysqld_safe[2689]:Logging to ‘/var/log/ MySQL /error.log’.
: iZufkfljcZ mysqld_safe[2693]:Directory ‘/var/run/mysqld’ for UNIX socket file Don ‘t The exists.
on December 20 10:38:30 iZufkfljcZ mysqld [2154] :. * The server quit without updating The PID file (/ var/run/mysqld/mysqld. PID).
on December 20 10:38:30 iZufkfljcZ systemd [1] : mysqld. Service: Control process exited, code=exited status=1
, dec.20 10:38:30 iZufkfljcZ systemd[1]: Failed to start LSB: start and stop MySQL.
, dec.20 10:38:30 iZufkfljcZ systemd[1]: mysqld.service: Unit dropped failed state.
dec.20 10:38:30 iZufkfljcZ systemd[1]: mysqld.service: failed with result ‘exe-code’
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The /var/run/mysqld directory does not exist. This directory is required for the mySQld service restart.
root@iZufkfljcZ:~# mkdir /var/run/mysqld
root@iZufkfljcZ:~# chmod 777 /var/run/mysqld/
root@iZufkfljcZ:~# service mysqld start
root@iZufkfljcZ:~#
After the directory is built, reruns the mySQld service and finds that the restart was successful, so let’s look again at why this directory was built in the first place. Open this directory and see:
root@iZufkfljcZ# ls /var/run/mysqld
mysqld.pid mysqld.sock mysqld.sock.lock
Found a familiar object, mysqld.sock, but is this what we need?Ignore him, use this sock file to log into mysql to see if you can:
root@iZufkfljcZ:/var/run/mysqld# mysql -uroot -p -S /var/run/mysqld/mysqld.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
, once we run it, we find that it seems to be ok, then we can do it. Let’s change the previous configuration back, the previous directory should be/TMP /mysql. Sock, we can establish a soft connection to it.
root@iZufkfljcZ:~# ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
root@iZufkfljcZ:~# ls /tmp/
mysql.sock
So you have the mysql.sock file that you need in your my.cnF configuration file in the TMP directory, and then you can just change my.cnF back into it.