Sometimes when we use commands such as "MySQL", "mysqladmin", "mysqldump" to manage the database, the server throws an error similar to the following:
1 |
ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2) |
What is the cause of this error? First, error site restore:
below we connect in three ways, then observe the error message of the prompt:
- 1, direct use of "MySQL" command, without host name parameters;
- 2, using the hostname "localhost" parameter "mysql-h localhost" command;
- 3. Use the "mysql-h 127.0.0.1" command with the hostname "127.0.0.1" parameter.
123456 |
[[email protected] ~]
# mysql
ERROR 2002 (HY000): Can
‘t connect to local MySQL server through socket ‘
/var/lib/mysql/mysql
.sock‘ (2)
[[email protected] ~]
# mysql -h localhost
ERROR 2002 (HY000): Can
‘t connect to local MySQL server through socket ‘
/var/lib/mysql/mysql
.sock‘ (2)
[[email protected] ~]
# mysql -h 127.0.0.1
ERROR 1045 (28000): Access denied
for
user
‘root‘
@
‘localhost‘
(using password: NO)
|
as can be seen from the above experiment, the first two ways can produce errors in the header, and the third way to connect is not to produce the title of the error (the third way here is due to the password problem denied access to the error message) (Love e-family). Second, the cause of error analysis:
This is because the host name parameter that we use to connect to the database is "localhost", or the hostname parameter is not used, and the server uses "localhost" as the hostname (love e-family) by default. Using the hostname parameter for "localhost" connection to the MySQL server, the MySQL client will be considered to be connected to the machine, so will try to connect as a socket file (socket file connection, more efficient than "IP: Port" mode), at this time according to the configuration file "/ Etc/my.cnf "path, this error is raised when the corresponding socket file is not found.
Three, repair before the failure to prepare: 1, see if the MySQL service is running:
since the "socket" file was created by the MySQL service runtime, if prompted "ERROR 2002 (HY000): Can ' t connect to local MySQL server through socket ' * * * * (2)", find Not to the "socket" file, we first need to confirm that the MySQL service is running.
123456789101112131415 |
# 1、 端口是否打开
[[email protected] ~]
# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE
/OFF
NODE NAME
mysqld 12207 mysql 14u IPv4 52350 0t0 TCP *:mysql (LISTEN)
# 2、mysqld服务是否正在运行
[[email protected] ~]
# service mysqld status
mysqld (pid 4717) is running...
# 3、如果mariadb,同样方法查服务是否正在运行:
[[email protected] ~]
# service mariadb status
Redirecting to
/bin/systemctl
status mariadb.service
● mariadb.service - MariaDB database server
Loaded: loaded (
/usr/lib/systemd/system/mariadb
.service; disabled; vendor preset: disabled)
Active: active (running) since 四 2016-11-03 13:47:37 CST; 23min ago
|
2. Determine the correct location of the "socket" file:
after you determine that the MySQL service is running correctly, the only reason for this error is that the "socket" file path is incorrect, and we can use the "find" command or the "lsof" command to determine the correct path to the socket file:
12345 |
[[email protected] ~] # lsof -c mysqld|grep sock$ mysqld 4717 mysql 12u unix 0xffff88010a655b80 0t0 77474827 /storage/db/mysql/mysql .sock [[email protected] ~] # find / -name ‘*.sock‘ /storage/db/mysql/mysql .sock |
Iv. Troubleshooting Solutions: Solution One:
Modify the "/etc/my.cnf" profile, add the "[Client]" option and the "[MySQL]" option in the configuration file, and use the "socket" parameter value under both options, and the "socket" parameter value under the "[Mysqld]" option, The path to the socket file is exactly the same. as follows:
123456789101112 |
[mysqld] datadir=/storage/db/mysql socket=/storage/db/mysql/mysql.sock ...省略n行(爱E族)... [client] default-character-set=utf8 socket=/storage/db/mysql/mysql.sock [mysql] default-character-set=utf8 socket=/storage/db/mysql/mysql.sock |
After modifying, restart the MYSQLD service to resolve this issue.
Solution Two:
using the "ln-s/storage/db/mysql/mysql.sock/var/lib/mysql/mysql.sock" command, the correct socket file location, the soft link to the wrong socket file path location, To resolve this issue:
1 2345 |
[[email protected] ~] # Ls/var/lib/mysql/mysql.sock ls /var/lib/mysql/mysql .sock:no such file or directory [[email protected] ~] # ln-s/storage/db/mysql/mysql.sock/var/lib/mysql/mysql.sock [[email protected] ~] # Ls/var/lib/mysql/mysql.sock /var/lib/mysql/mysql .sock |
Original address: http://aiezu.com/article/mysql_cant_connect_through_socket.html
Go ERROR 2002 (HY000): Can ' t connect to local MySQL server through socket ' * * * (2)