Snippets

How To Troubleshoot Socket Errors in MySQL – Ubuntu

MySQL manages connections to the database server through the use of a socket file, a special kind of file that facilitates communications between different processes. The MySQL server’s socket file is named mysqld.sock and on Ubuntu systems it’s usually stored in the /var/run/mysqld/ directory. This file is created by the MySQL service automatically.

Sometimes, changes to your system or your MySQL configuration can result in MySQL being unable to read the socket file, preventing you from gaining access to your databases. The most common socket error looks like this:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

One common cause of this error is that the MySQL service is stopped or did not start to begin with, meaning that it was unable to create the socket file in the first place. To find out if this is the reason you’re seeing this error, try starting the service with systemctl:

sudo systemctl start mysql

Then try accessing the MySQL prompt again. If you still receive the socket error, double check the location where your MySQL installation is looking for the socket file. This information can be found in the mysqld.cnf file.

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Look for the socket parameter in the [mysqld] section of this file. It will look like this:

. . .
[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
. . .

Close this file, then ensure that the mysqld.sock file exists by running an ls command on the directory where MySQL expects to find it:

ls -a /var/run/mysqld/

Output
. .. mysqld.pid mysqld.sock mysqld.sock.lock

If the file does not exist, the reason may be that MySQL is trying to create it, but does not have adequate permissions to do so. You can ensure that the correct permissions are in place by changing the directory’s ownership to the mysql user (user as in mysqld.cnf file shows above) and group:

sudo chown mysql:mysql /var/run/mysqld/

Then ensure that the mysql user has the appropriate permissions over the directory. Setting these to 775 will work in most cases:

sudo chmod -R 755 /var/run/mysqld/

Finally, restart the MySQL service so it can attempt to create the socket file again:

sudo systemctl restart mysql

Then try accessing the MySQL prompt once again. If you still encounter the socket error, there’s likely a deeper issue with your MySQL instance, in which case you should review the error log.

Recent Posts

How to remove .html, .php, etc (extension) from URL?

To remove the .html extension from your urls, you can use the following code in…

5 years ago

Ubuntu with Apache2: Installing and Configuring Your SSL Certificate

Use free/paid SSL certificates https://www.sslforfree.com Enable SSL Module Replace 'default-ssl' with the real site name…

5 years ago

Create zip archive excluding specific Files & Directories

Create archive of all files under public_html directory ignoring all files and folders including text…

6 years ago

how to use sudo command to install .tar.gz?

To install some file *.tar.gz, you basically would do: Open a console, and go to…

6 years ago

Change ownership of the directory in ubuntu

The chown command has the following syntax: [crayon-66368b397391f825082917/] So in your example command it is your primary…

6 years ago

failed to retrieve share list from server connection refused ubuntu 18.04

How to Save: To save in nano use "CTRL-O", then "CTRL-X". Tip: Replacing sudo nano with gksudo gedit gives you a…

6 years ago