ERROR 2002 of HY000 : Can't connect to local MySQL server through socket ' and tmp and mysql.sock'

  • 2021-12-19 07:05:07
  • OfStack

Error message:

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

Reason for the problem:

It is possible that the parameter socket of [mysqld] is set in the my. cnf configuration file, but the parameter socket of [client] is not set

What is the use of the mysql. sock file:

mysql supports socket and TCP/IP connections. So what's the use of mysql. sock? Connecting to localhost is usually done through an Unix domain socket file, 1 is/tmp/mysql. sock. If the socket file is deleted, the local customer cannot connect. The/tmp folder is a temporary file and can be deleted at any time.

1. TCP Connection (If an error is reported/tmp/mysql. sock, you can try this connection)

mysql -uroot -h 127.0.0.1 -p

2. socket Connection

mysql -uroot -p

Solution:

Add the [client] configuration item as follows

Before configuration: (Error '/tmp/mysql. sock' will be reported before configuration of [client] (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: (Restart the mysql service after configuring [client])

[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/var/lib/mysql/mysql. sock (like this socket path 1)
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 everyone!


Related articles: