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.
Read More:
- The server just without updating PID file and can’t connect to local MySQL server through socket are solved
- The MySQL service suddenly hangs up with the error message can’t connect to MySQL server on ‘localhost’ (10061)
- MySQL: if the remote connection using navicatip fails, prompt “is not allowed to connect to this MySQL server”
- mysql problem solving: mysqladmin: connect to server at’localhost’ failed
- MYSQL login error: mysqladmin: connect to server at’localhost’ failed
- Can’t connect to MySQL server error 111
- ERROR 2002 (HY000): Can’t connect to MySQL server on ‘localhost’ (10061)
- Can’t connect to MySQL server on ‘192.168.64.132‘
- Error 2003 (HY000): can’t connect to MySQL server on ‘localhost’ (10061)
- Cannot connect to MySQL server on “host” (113)
- MySQL local connection Error 1130_ The solution of MySQL 1130 error report when remote connection through Navicat for MySQL
- PHP connection to MySQL database error: call to undefined function MySQL_ connect()
- Sqlog connect to MySQL 8.0.24 remote server, error: 2058 solution
- After installing MySQL again under Linux, the solution of ‘MySQL module failed to start’ appears
- Host is not allowed to connect to this MySQL server
- 2003 – Can‘t connect to MySQL server on ‘192.168.163.128‘(Unknown error )
- node.js Server MySQL database connection timeout (error: connect etimeout)
- MySQL Workbench: Error Code: 2013. Lost connection to MySQL server during query solution
- MySQL Workbench Failed to Connect to MySQL at 127.0.0.1:3306 with user root Bad handshake
- CentOS Liunx 7 uses rdesktop to remotely connect to the Alibaba Cloud server and it appears Failed to connect, CredSSP required by server.