How to Fix 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'

Introduction

Users working with MySQL can run into the error 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock (2)' when logging into the MySQL interface. This problem usually arises if MySQL can't access the mysqld.sock socket file.

In this tutorial, we will go over the potential causes of the 'Can't connect to local MySQL server through socket' error and show you different methods of resolving this issue.

How to fix 'can't connect to local MySQL server through socket' error

Prerequisites

  • A system running Ubuntu 20.04
  • A user account with sudo privileges
  • Access to the terminal window/command line
  • A copy of MySQL installed and ready to use (learn how to install it with our guide to installing MySQL on Ubuntu 20.04)

Resolving the 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' Error

There are multiple ways to solve the 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock (2)' error. If one solution doesn't work, move down the list until you find the one that resolves the issue.

Method 1: Check the MySQL Service

1. Check the status of the MySQL service with:

sudo systemctl status mysql
Checking the status of the MySQL service

2. If the service is not running, restart it by using:

sudo systemctl start mysql

3. To prevent this issue from happening, set the MySQL service to automatically start at boot:

sudo systemctl enable mysql
Set the MySQL service to automatically start when the system boots up

Method 2: Verify the mysqld.sock Location

The 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' error also happens if MySQL can't find the mysql.sock socket file.

1. Find the current mysqld.sock location by using the find command to list all the socket files on your system:

sudo find / -type s
List all the socket files on your system using the find command

Note: Learn more about this command in our guide for the Linux find command.

2. Open the MySQL configuration file in a text editor of your choice. In this example, we use nano:

sudo nano /etc/mysql/my.cnf

3. Then, add the following lines at the end of the MySQL configuration file:

[mysqld]
socket=[path to mysqld.sock]
[client]
socket=[path to mysqld.sock]

Where:

  • [path to mysqld.sock]: Path to the mysqld.sock socket file you found in Step 1.
Edit the MySQL configuration file

Another method is to create a symlink from the location of mysqld.sock to the /var/run/mysqld directory:

ln -s [path to mysqld.sock] /var/run/mysqld/mysqld.sock

4. Press Ctrl+X to close the configuration file and type Y and press Enter to save the changes you made.

Note: Learn how to create symbolic links with our guide to the Linux ln command.

4. Finally, restart the MySQL service:

sudo systemctl restart mysql

Method 3: Check the MySQL Folder Permission

Another potential cause could be that the MySQL Service can't access the /var/run/mysqld directory due to permission restrictions:

1. To resolve this issue, change the permission settings for the mysqld directory with:

sudo chmod -R 755 /var/run/mysqld

Setting the permission to 755 allows the root user to read, write, and execute the directory, while other users can only read and execute.

Note: You can change permissions for files and folders with the chmod command in Linux.

2. Restart the MySQL service for the changes to take effect:

sudo systemctl restart mysql

Method 4: Check for Multiple MySQL Instances

The error also occurs if there are multiple instances of MySQL running at the same time.

1. To list all the instances of MySQL, use:

ps -A|grep mysqld
Listing all currently running instance of MySQL

Note: Check out our guide to learn how to use the grep command in Linux.

2. If there are multiple MySQL instances running, terminate them with:

sudo pkill mysqld

3. Restart the MySQL service to start a single instance of MySQL:

sudo systemctl restart mysql

Conclusion

After reading this tutorial, you should have identified the cause of the 'Can't connect to local MySQL server through socket' error and applied the appropriate solution.

For more help with using MySQL, consult our MySQL Commands Cheat Sheet.

Was this article helpful?
YesNo
Aleksandar Kovačević
Aleksandar Kovacevic is an aspiring Technical Writer at phoenixNAP. With a background in both design and writing, he aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Create a Table in MySQL
November 3, 2020

MySQL is a well-known, free and open-source database application. One of the most crucial processes in MySQL is creating tables and databases. Follow this tutorial to learn how to create a table in MySQL and insert data.
Read more
How To Optimize MySQL Tables
June 24, 2021

MySQL tables fragment over time as delete and update queries are performed. With a large database, the fragmentation slows down queries significantly.
Read more
MySQL Data Types
March 4, 2021

Data types are important to understand as a name and a data type define each column in a database table. Learn all about MySQL data types and when to use them in this comprehensive guide.
Read more
MySQL Stored Procedures (Create, List, Alter, & Drop)
April 13, 2021

MySQL stored procedures group multiple tasks into one and save the task on the server for future use. Stored procedures simplify database management and reduce network traffic. Learn how to use stored procedures in this tutorial.
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.