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

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

error message:

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

Cause of the problem:

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

What is the mysql.sock file used for:

mysql supports both socket and TCP/IP connections. So what is the use of the mysql.sock file? Connections to localhost are usually made through a Unix domain socket file, typically /tmp/mysql.sock. If the socket file is deleted, local clients will not be able to connect. The /tmp folder is a temporary file and may be deleted at any time.

1. TCP connection (if an error message is displayed for /tmp/mysql.sock, you can try this method 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 message will be reported: '/tmp/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 the 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, this article ends, I hope it will be helpful to everyone!

You may also be interested in:
  • MySQL bypasses granting information_schema objects and reports ERROR 1044 (4200) error
  • mysql databasemysql: [ERROR] unknown option ''--skip-grant-tables''
  • How to solve the 10060 unknow error when Navicat remotely connects to MySQL
  • Ubuntu 18.0.4 installs mysql and solves ERROR 1698 (28000): Access denied for user ''''root''''@''''localhost'''
  • Solution to MySQL server login error ERROR 1820 (HY000)
  • Solve MySQL startup error: ERROR 2003 (HY000): Can't connect to MySQL server on ''localhost'' (10061)

<<:  Tutorial on deploying multiple servers with WebApi and configuring Nginx load balancing

>>:  Vue uses WebSocket to simulate the chat function

Recommend

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

Summary of Ubuntu backup methods (four types)

Method 1: To use respin, follow these steps: sudo...

8 powerful techniques for HTML web page creation

<br />Although there are many web page creat...

Timeline implementation method based on ccs3

In web projects we often use the timeline control...

A brief introduction to web2.0 products and functions

<br />What is web2.0? Web2.0 includes those ...

MySQL join buffer principle

Table of contents 1. MySQL join buffer 2. JoinBuf...

Implementation of Element-ui Layout (Row and Col components)

Table of contents Basic instructions and usage An...

How to install Chrome browser on CentOS 7

This article introduces how to install Chrome bro...

How to modify the "Browse" button of the html form to upload files

Copy code The code is as follows: <!DOCTYPE HT...

Summary of the Differences between SQL and NoSQL

Main differences: 1. Type SQL databases are prima...

VUE Getting Started Learning Event Handling

Table of contents 1. Function Binding 2. With par...

Implementing long shadow of text in less in CSS3

This article mainly introduces how to implement l...

What is jQuery used for? jQuery is actually a js framework

Introduction to jQuery The jQuery library can be ...