[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.),

  • MySQL server through socket '/tmp/mysql.sock '
  • 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 means
"Unable to connect to local MySQL server through socket /tmp/mysql.sock "

A socket is a Unix domain socket (file system socket), and when communicating within a local system,
communication between the server and client is carried out through file input and 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

Check if the process exists.
If not, 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 error above shows that the client is looking at /tmp/mysql.sock , but
the MySQL server is looking at /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 useful, please click [Like]!
4
Loading...
4 votes, average: 1.00 / 14
58,457
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author