Can’t connect to local MySQL server through socket

When mysql, mysqldump, and php connect to the mysql service, the following errors are often prompted:


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 running normally:

Since the socket file of mysql is created when the mysqld service is started, if the mysqld service is not started normally, the socket file will not be created, of course, the socket file will not be found. To determine whether the mysql service is started, we can use the following command:


# 1. Is the port 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. Whether the mysqld service is running

[[email protected] ~]# service mysqld status

mysqld (pid  4717) is running...

 


2. The socket file path is not set completely in the configuration file:

This is generally caused by modifying 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 of the "[client]" and "[mysql]" options, causing mysql to use the default socket file location to go to Looking for a socket file, resulting in this error being thrown when the socket file was not found.


Solution:

The following is the solution, for a more detailed introduction, please refer to: http://aiezu.com/article/mysql_cant_connect_through_socket.html


1. The mysql service is not running normally:

If the service is not started, we can run "service mysqld start" to start the service. If the service cannot be started, check the mysql service log, find the reason and solve it before restarting

[[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. Improve the mysql configuration file:

If it is confirmed that the mysql service is running normally, and this error in the title of the article is also prompted, it is a problem with the "/etc/my.cnf" configuration file. The solution is to modify the "/etc/my.cnf" configuration file, add the "[client]" option and the "[mysql]" option to the configuration file, and use the "socket" parameter value under these two options, and " The value of the "socket" parameter under the [mysqld]" option points to the exact same path to the socket file. as follows:

[mysqld]

datadir=/storage/db/mysql

socket=/storage/db/mysql/mysql.sock

...omit the n lines (love the 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 equal to socket is the location of the socket file. We only need to modify the my.cnf file, tell mysql, mysqldump, mysqladmin and other commands, where is the location of the socket file of the mysql service, and then restart the mysqld service.


3. The solution to the prompt "Can't connect to local MySQL server through socket..." when php connects to mysql service

Sometimes the mysql service runs normally, and the username and password are completely correct, but the mysql_connect function of php cannot be used to connect to mysql, and the mysql_error() function of php is called to prompt "Can't connect to local MySQL server through socket '/var/lib/mysql" /mysql.sock'", this is what we need to modify the /etc/php.ini file.

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]

...omitting n lines...

mysql.default_socket = "/storage/db/mysql/mysql.sock"

 

4. The solution to the prompt "Can't connect to local MySQL server through socket..." when python connects to mysql:

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. The solution to the prompt "Can't connect to local MySQL server through socket..." when php pdo connects to mysql:

Also add the location of the 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);

}

?>

The place to add is the location of the mysql.sock file. My file is in /tmp/mysql.sock but it is empty when opened.  

So there were some doubts at the time. It was proved by experiments that it was indeed this /tmp/mysql.sock. My problem was that php could not be connected.  

  The mysql client is OK, then change php.ini and restart php-fpm.  

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325304134&siteId=291194637