[2002] Three causes and solutions for MySQL socket errors
table of contents
When connecting to MySQL server from MySQL client software (mysql, PHP, Perl, etc.),
- socket '/tmp/mysql.sock '
- No such file or directory (trying to connect via unix : ///tmp/mysql . sock)
You may be unable to connect due to an error like this.
This means
"Unable to connect to local MySQL server through socket /tmp/mysql.sock." (I just translated it)
A socket is a Unix domain socket (file system socket) that
communicates between the server and client through input/output of this file when communicating within the local system.
He's the intermediary.
Now, I would like to explain the causes of such errors and how to deal with them.
Cause of the error
Cause 1. MySQL server is not started
$ ps aux | grep mysqld
Check if the process exists.
If not, start it.
$ sudo /etc/init.d/mysqld status
Cause 2. The paths of the Unix socket used by the MySQL server and the Unix socket 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 shows that the client is referring to /tmp/mysql.sock, but
the MySQL server is referring to /var/lib/mysql/mysql.sock.
There are two solutions in this case.
Solution A. Align clients
Referring to the MySQL configuration file, /etc/my.cnf,
[mysqld] socket=/var/lib/mysql/mysql.sock
Since the path is written like this, all you need to do is set the socket of the client software to the same path.
Solution B. Match MySQL
Let's rewrite /etc/my.cnf as shown below.
[mysqld] socket=same socket path as client [client] soket=same socket path as client [shell] After rewriting, restart MySQL. [shell]$ sudo /etc/init.d/mysqld restart
Cause 3. There is no socket file in the first place.
There are two ways to solve this too.
Solution A. Restart the MySQL server and it will be restored.
(*Restart the server, not mysql.)
$ sudo shutdown -r now
Solution B. Create a socket file
After checking /etc/my.cnf and checking the path,
$ 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
These are the causes and solutions for MySQL 2002 errors.
Introducing our Beyond
Since its founding, Beyond has developed technology as a multi-cloud integrator and managed service provider (MSP) and has designed, constructed and migrated using a variety of cloud/server platforms, including AWS, GCP, Azure, and Oracle Cloud.
We provide custom-made cloud/server environments that are optimized for customers according to 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/construction
● Cloud/server migration/migration
● Cloud/server operation, maintenance, and monitoring (24 hours a day, 365 days a year)