mysql,mysqldump,php connecting to MySQL service often prompts the following error:
ERROR 2002 (HY000): Can ' t connect to local MySQL server through socket '/var/lib/mysql/mysql.sock ' (2)
There are typically two reasons for this problem:
1. The MySQL service is not running properly:
Since the MySQL socket file was created by the MYSQLD service startup, if the Mysqld service does not start properly, the socket file will not be created, of course, the socket file cannot be found. To determine if the MySQL service is starting, we can use the following command:
# 1, whether the port is open
[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 service is running
[[Email protected] ~]# service mysqld status
MYSQLD (PID 4717) is running ...
2. The socket file path is not fully set in the configuration file:
This is generally due to the fact that we modified the MySQL configuration "/etc/my.cnf". For example, we modified the "[MySQL]" option in the configuration file "socket" parameter, but did not specify "[Client]", "[MySQL]" option of "socket" parameter, causing MySQL to use the default socket file location to find the socket file, This error is raised because the socket file was not found.
Workaround:
The following is a workaround, more detailed information can be consulted: http://aiezu.com/article/mysql_cant_connect_through_socket.html
1. The MySQL service is not running properly:
If the service is not started, we can run service mysqld start. If the service does not start, go to the MySQL service log, look for the cause and resolve the restart
[[Email protected] ~]# service mysqld start
Starting mysqld: [OK]
[Email protected] ~]# lsof-i:3306
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
Mysqld 14109 MySQL 10u IPv4 247183583 0t0 TCP *:mysql (LISTEN)
[[Email protected] ~]# service mysqld status
MYSQLD (PID 14109) is running ...
2. Perfect MySQL configuration file:
If you confirm that the MySQL service is working correctly, you are also prompted for this error in the article title, which is a problem with the "/etc/my.cnf" configuration file. The workaround is to 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:
[Mysqld]
Datadir=/storage/db/mysql
Socket=/storage/db/mysql/mysql.sock
... Omit n rows (Love e-family) ...
[Client]
Default-character-set=utf8
Socket=/storage/db/mysql/mysql.sock
[MySQL]
Default-character-set=utf8
Socket=/storage/db/mysql/mysql.sock
Where the socket is equal to the location of the socket file, we just modify the my.cnf file, tell Mysql,mysqldump,mysqladmin and other commands, the location of the MySQL service socket file, and then restart the Mysqld service.
3. PHP Connection MySQL service tip "Can ' t connect to local MySQL server through socket ..." Workaround
Sometimes the MySQL service is working properly, the user name password is also correct, using PHP mysql_connect function is not connected to MySQL, call PHP mysql_error () function hint "Can ' t connect to local MySQL Server through socket '/var/lib/mysql/mysql.sock ', which is the/etc/php.ini file we need to modify.
Locate "Mysql.default_socket" under "[MySQL]" in the/etc/php.ini file and set its value to point to the correct MySQL service socket file, such as:
[MySQL]
... Omit n rows ...
Mysql.default_socket = "/storage/db/mysql/mysql.sock"
4. Python connection mysql hint "Can ' t connect to local MySQL server through socket ..." Workaround:
Specify the socket file in the Connect MySQL database function as follows:
#!/usr/bin/python
From MYSQLDB Import Connect
conn = Connect (db= "Pzy", user= "root", host= "localhost", unix_socket= "/storage/db/mysql/mysql.sock")
cur = conn.cursor ()
Count=cur.execute ("Show Databases")
print ' There has%s dbs '% count
Conn.commit ()
Conn.close ()
5. PHP PDO connection MySQL hint "Can ' t connect to local MySQL server through socket ..." Workaround:
Also add the location of the MySQL socket file in the connection string, as follows:
<?php
$DSN = "Mysql:host=localhost;dbname=pzy;unix_socket=/storage/db/mysql/mysql.sock";
$db = new PDO ($dsn, ' root ', ');
$rs = $db->query ("SELECT * from Qrtest");
while ($row = $rs->fetch ()) {
Print_r ($row);
}
?>
The place to add is to say the location of this mysql.sock file my file is in/tmp/mysql.sock but open it is empty
So there was some confusion, and the experiment proved it was the/tmp/mysql.sock. My problem is that PHP is not connected
The MySQL client is available and then changed php.ini restart PHP-FPM.
Can ' t connect to local MySQL server through socket