MySQL uses a special type of file that facilitates communication between processes, which is called socket file. With this file, MySQL can manage connections to the database server. MySQL’s socket file is called mysql.sock and it’s located in the /var/run/mysqld/ Ubuntu directory. The socket file is automatically created by the MySQL service.

Sometimes, changes and modifications on your system can make MySQL unable to read the socket file, which can interfere in your access to your databases. As an example, the most current socket error is something like this:

output

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

There are some reasons for this error to occur, and some different ways to resolve it. A common cause to this error is that the database service is stopped or didn’t start to begin with, which means that it couldn't create the socket file. To discover if this is the reason why you’re seeing this error, try starting the service with systemctl:

$

sudo systemctl start mysql

After, try to access the MySQL prompt again. If you still get the socket error, check the location where your MySQL installation is looking for it again. This can be found in the mysqld.cnf file:

$

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

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

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

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

Close the file, and ensure that the mysqld.sock file is there by writing an ls command on the directory where the database expects:

$

ls -a /var/run/mysqld/

If the socket file is there, you’ll see it in this command’s output:

output

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

If the file doesn’t exist, the reason is probable to be that MySQL is trying to create it, but doesn’t have the permissions to make it. Ensure that the permissions are in place by changing the directory ownership to the mysql user and group:

$

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

After, ensure that the database user has the proper permissions over the directory. Setting these to 75 will not be a solution in most cases.

$

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

Now you should restart the MySQL service so it can try to create the socket file:

$

sudo systemctl restart mysql

Try to access the MySQL prompt again. If you still find the error, there’s probably another kind of issue with your MySQL instance, in which case you should review the error log to see if you get any clues.

Read more about: MySQL