ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’

Time:2020-9-4

Error message:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

Causes of problems:

It could be my.cnf The parameter socket of [mysqld] is set in the configuration file, but the parameter socket of [Client] is not set

mysql.sock What is the purpose of the document

MySQL supports socket and TCP / IP connections. So mysql.sock What’s the use of this document? Connecting to localhost is usually done through a UNIX domain socket file, usually / tmp/ mysql.sock 。 If the socket file is deleted, the local client cannot connect. /Tmp folder is a temporary file and may be deleted at any time.

1. TCP connection(if error / TMP is reported/ mysql.sock , you can try this way to connect)

mysql -uroot -h 127.0.0.1 -p

2. Socket connection

mysql -uroot -p

Solution:

Add the [Client] configuration item, as shown below

Before configuration: (before configuring [Client], an error ‘/ tmp will be reported/ mysql.sock ‘ (2))

[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

After configuration: (after configuring [Client], restart MySQL service)

[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/var/lib/mysql/ mysql.sock (same as the socket path)
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
port=3306
socket=/var/lib/mysql/mysql.sock

End, the end of this article, I hope to help you!

Recommended Today

Leetcode PHP题解–D127 455. Assign Cookies

D127 455. Assign Cookies topic link 455. Assign Cookies topic analysis Given two arrays, the first array represents the capacity of each container, and the second array represents the number of each item. The items need to be stuffed into the container, but there are two conditions. Condition 1: Only one item can be stuffed […]