Solution to connect Mysql prompt Can't connect to local MySQL server through socket

Keywords: MySQL socket PHP mysqldump

mysql, mysqldump, mysqldump, php connection 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 generally two reasons for this problem:
1. The mysql service is not functioning properly:
Since mysql's socket file is created when mysqld service starts, if mysqld service does not start properly, the socket file will not be created naturally. Of course, the socket file will not be found. To determine whether the MySQL service is started, we can use the following commands:

# 1. Is the port open?
[root@aiezu.com ~]# lsof -i:3306
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  12207 mysql   14u  IPv4  52350      0t0  TCP *:mysql (LISTEN)

# 2. Is the mysqld service running
[root@aiezu.com ~]# service mysqld status
mysqld (pid  4717) is running..

2. The socket file path is incompletely set in the configuration file:
This is generally due to our modification of the MySQL configuration "/etc/my.cnf". For example, we modified the "socket" parameter under the "mysql" option in the configuration file, but did not specify the "socket" parameter under the "client" and "mysql" options, which led to MySQL using the default socket file location to find the socket file, resulting in the error of not finding the socket file.

Solution:
Following is the solution, more detailed introduction can be referred to: http://aiezu.com/article/mysql_cant_connect_through_socket.html
1. The mysql service is not functioning properly:
If the service is not started, we run "service mysqld start" to start the service. If the service fails to start, check the mysql service log to find out why and resolve restart

[root@aiezu.com ~]# service mysqld start
Starting mysqld:                                           [  OK  ]
[root@aiezu.com ~]# lsof -i:3306
COMMAND   PID  USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
mysqld  14109 mysql   10u  IPv4 247183583      0t0  TCP *:mysql (LISTEN)
[root@aiezu.com ~]# service mysqld status
mysqld (pid  14109) is running...

2. Perfect mysql configuration file:
If you confirm that the MySQL service is working properly, you will also be prompted for this error in the title of the article. That is the problem with the "/ etc / my. cnf" configuration file. The solution is to modify the "/etc/my.cnf" configuration file, add the "[client]" option and "[mysql]" option in the configuration file, and use the "socket" parameter value under these two options to point to the socket file path exactly the same as the "socket" parameter value under the "[mysqld]" option. As follows:

[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
...ellipsis n Row (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

The path that socket equals is the location of socket file. We just need to modify my.cnf file, tell mysql, mysqldump, mysqladmin and other commands, where is the location of the socket file of MySQL service, and then restart mysqld service.

3. php connection mysql service prompt "Can't connect to local MySQL server through socket..." Solutions
Sometimes the MySQL service runs normally and the user name password is completely correct. The mysql_connect function of PHP can't connect mysql. Calling the mysql_error() function of PHP prompts "Can't connect to local MySQL server through socket'/var/lib/mysql/mysql.sock'", which is the file we need to modify / etc/php.ini.
Find "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]
...ellipsis n That's ok...
mysql.default_socket = "/storage/db/mysql/mysql.sock"

4. python connection mysql prompt "Can't connect to local MySQL server through socket..." Solutions:
Specify the socket file in the connection 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()
  1. php pdo connection mysql prompt "Can't connect to local MySQL server through socket..." Solutions:
    Similarly, you can add mysql socket file to 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);
}
?>

Posted by chrille112 on Tue, 18 Dec 2018 01:30:04 -0800