[2002] Three causes of MySQL socket errors and how to fix them

When connecting to a MySQL server from MySQL client software (mysql, PHP, Perl, etc.),

  • server through socket '/tmp/mysql.sockMySQL'
  • No such file or directory (trying to connect via unix:///tmp/mysql.sock)

You may encounter an error like this and be unable to connect

This"Unable to connect to the local MySQL server via socket /tmp/mysql.sock."means
(This is just a translation.)

A socket, in the context of Unix domain sockets (filesystem sockets),
is used for communication between servers and clients within a local system through file input/output.
It acts as an intermediary.

So, I would like to explain why this error occurs and how to deal with it

Cause of the error

Cause 1. The MySQL server is not running

$ ps aux | grep mysqld

Let's check if the process exists.
If not, let's start it.

$ sudo /etc/init.d/mysqld status 

Cause 2. The Unix socket path used by the MySQL server and the Unix socket path used by the client software are different

No such file or directory (trying to connect via unix:///tmp/mysql.sock)

For example, the above error indicates that the client is referencing /tmp/mysql.sock, but
the MySQL server is referencing /var/lib/mysql/mysql.sock.

There are two solutions in this case:

Solution A. Match the client

If you look at the MySQL configuration file, /etc/my.cnf,

[mysqld] socket=/var/lib/mysql/mysql.sock

The path is written like this, so you just need to set the socket of the client software to the same path

Solution B. Match MySQL

Rewrite /etc/my.cnf as follows:

[mysqld] socket=same socket path as client [client] socket=same socket path as client [shell] After rewriting, restart MySQL. [shell]$ sudo /etc/init.d/mysqld restart

Cause 3. There is no socket file

There are two ways to solve this too

Solution A. Restart the MySQL server and it will be restored

(*This is a server restart, not a mysql restart.)

$ sudo shutdown -r now

Solution B. Create a socket file

Check your path by looking at /etc/my.cnf

$ touch /path/mysql.sock $ chown mysql:mysql /path/mysql.sock

Create a socket at that path and restart MySQL

$ sudo /etc/init.d/mysql restart

The above is the cause and solution of the MySQL 2002 error

Introducing our company Beyond

Since its founding, Beyond has used the technical capabilities it has cultivated as a multi-cloud integrator and managed service provider (MSP) to design, build, and migrate systems using a variety of cloud/server platforms, including AWS, GCP, Azure, and Oracle Cloud

We provide a custom-made cloud/server environment optimized for our customers based on the specifications and functions of the systems and applications they require, so if you are interested in the cloud, please feel free to contact us

● Cloud/Server design and construction
● Cloud/Server migration
● Cloud/Server operation, maintenance, and monitoring (24 hours a day, 365 days a year)

If you found this article helpful,please give it a "Like"!
4
Loading...
4 votes, average: 1.00 / 14
58,741
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author