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

Html long text automatically cuts off when it exceeds the tag width

When we display long text, we often need to interc...

Solution to many line breaks and carriage returns in MySQL data

Table of contents Find the problem 1. How to remo...

How to modify port 3389 of Windows server 2008 R2 remote desktop

The default port number of the Windows server rem...

Sample code for batch deployment of Nginx with Ansible

1.1 Copy the nginx installation package and insta...

The implementation process of extracting oracle data to mysql database

In the migration of Oracle database to MySQL data...

Embed player in web page embed element autostart false invalid

Recently, I encountered the need to embed a player...

Detailed tutorial on installing mysql on centos 6.9

1. Confirm whether MySQL has been installed. You ...

DockerToolBox file mounting implementation code

When using docker, you may find that the file can...

An article to quickly understand Angular and Ionic life cycle and hook functions

Table of contents Angular accomplish Calling orde...

Control the light switch with js

Use js to control the light switch for your refer...

In-depth understanding of the creation and implementation of servlets in tomcat

1. What is a servlet 1.1. Explain in official wor...

Detailed explanation of TS object spread operator and rest operator

Table of contents Overview Object rest attribute ...

Implementing calculator functions with WeChat applet

This article is a simple calculator written using...

Correct use of MySQL partition tables

Overview of MySQL Partitioned Tables We often enc...

Steps to build the vite+vue3+element-plus project

Use vite to build a vue3 project You can quickly ...