Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2)

My problem started off with me not being able to log in as root any more on my mysql install. I was attempting to run mysql without passwords turned on... but whenever I ran the command

# mysqld_safe --skip-grant-tables &

I would never get the prompt back. I was trying to follow these instructions to recover the password.

The screen just looks like this:

root@jj-SFF-PC:/usr/bin# mysqld_safe --skip-grant-tables
120816 11:40:53 mysqld_safe Logging to syslog.
120816 11:40:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

and I don't get a prompt to start typing the SQL commands to reset the password.

When I kill it by pressing CTRL + C, I get the following message:

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

Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

If I retry the command and leave it long enough, I do get the following series of messages:

root@jj-SFF-PC:/run/mysqld# 120816 13:15:02 mysqld_safe Logging to syslog.
120816 13:15:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120816 13:16:42 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

[1]+  Done                    mysqld_safe --skip-grant-tables
root@jj-SFF-PC:/run/mysqld#

But then if I try to log in as root by doing:

# mysql -u root

I get the following error message:

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

I checked and /var/run/mysqld/mysqld.sock file doesn't not exist. The folder does, but not the file.

Also, I don't know if this helps or not, but I ran find / -name mysqld and it came up with:

/var/run/mysqld - folder
/usr/sbin/mysqld - file
/run/mysqld - folder

I'm new to Linux and MySQL, so I don't know if this is normal or not. But I'm including this info just in case it helps.

I finally decided to uninstall and reinstall mysql.

apt-get remove mysql-server
apt-get remove mysql-client
apt-get remove mysql-common
apt-get remove phpmyadmin

After reinstalling all packages again in the same order as above, during the phpmyadmin install, I got the same error:

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

So I tried again to uninstall/reinstall. This time, after I uninstalled the packages, I also manually renamed all mysql files and directories to mysql.bad in their respective locations.

/var/lib/mysql 
/var/lib/mysql/mysql
/var/log/mysql
/usr/lib/perl5/DBD/mysql
/usr/lib/perl5/auto/DBD/mysql
/usr/lib/mysql
/usr/bin/mysql
/usr/share/mysql
/usr/share/dbconfig-common/internal/mysql
/etc/init.d/mysql
/etc/apparmor.d/abstractions/mysql
/etc/mysql

Then I tried to reinstall mysql-server and mysql-client again. But I've noticed that it doesn't prompt me for a password. Isn't it supposed to ask for an admin password?

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.

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2)

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

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2)

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

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2)

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

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2)

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.

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2)

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.

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.

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

Cant connect to local mysql server through socket /var/run/mysqld/mysqld.sock (2)

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.

How do I connect to a local mysql server through socket?

On the server host in the command line, run the following command: mysql -u root -p -h 127.0.0.1 -e "select @@socket".
Type a password for your root user and press Enter ..

Can not connect to mysql through socket?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

Can't connect to local mysql server through socket '[ path to mysqld sock ]'?

How to Fix 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.
Method 1: Check the MySQL Service..
Method 2: Verify the mysqld.sock Location..
Method 3: Check the MySQL Folder Permission..
Method 4: Check for Multiple MySQL Instances..

Can't connect to mysql server through socket Ubuntu?

"Try" to run mysql via /etc/init. d/mysql start if it gives you the exact same error from above then you need to copy the mysql. server file from the mysql you downloaded which can be found in the support-files folder inside the mysql folder you downloaded or in the /usr/local/mysql folder and copy it to /etc/init.