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

TCP third handshake data transmission process diagram

The process packets with the SYN flag in the RFC7...

Examples of new selectors in CSS3

Structural (position) pseudo-class selector (CSS3...

MySQL Basic Tutorial: Detailed Explanation of DML Statements

Table of contents DML statements 1. Insert record...

JavaScript code to achieve a simple calendar effect

This article shares the specific code for JavaScr...

Install Docker on CentOS 7

If you don't have a Linux system, please refe...

Tutorial on installing Android Studio on Ubuntu 19 and below

Based on past experience, taking notes after comp...

React implements infinite loop scrolling information

This article shares the specific code of react to...

MySQL database terminal - common operation command codes

Table of contents 1. Add users 2. Change the user...

Installation steps of docker-ce on Raspberry Pi 4b ubuntu19 server

The Raspberry Pi model is 4b, 1G RAM. The system ...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...

HTML unordered list bullet points using images CSS writing

Create an HTML page with an unordered list of at l...