Cant connect to local mysql server through socket /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?

After sleepless nights trying to find a solution to this problem, I have finally come to one. Thus, I’ve decided to share it and I know I’ll need it again in the future (It’s not the first time I install MySQL in a recently formatted Windows 10 computer and face this problem).

Note that I’m running Debian in a Linux Subsystem for Windows 10 and I suppose it might work with any subsystem.

Cleaning the Backyard

First of all, let’s start from scratch, you want to make sure you don’t have any remaining MySQL garbage in your system:

sudo apt-get remove --purge mysql*

After that, check if everything is clean, run the following code, and hope nothing shows up

dpkg -l | grep mysql

If that doesn’t do the trick, like in my case I still had some installed…

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

Try to purge them individually, like so (php is just an innocent amidst the sinful, so leave it be) :

sudo apt-get remove --purge mysql-apt-config

And now, clean everything else

sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean

Installing Mysql-server

Now, we can start from scratch and install it. First, we have to download our configuration file using (be sure to get the latest file, mine was 8.15–1. you can find them here http://repo.mysql.com):

wget http://repo.mysql.com/mysql-apt-config_0.8.15-1_all.deb

Once it is done, install it using:

sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb

It will prompt you to choose a MySQL version, take mysql-5.7. I was taken by the greed of choosing 8.0, but it gave me more problems than I had wished for, then humbly get 5.7:

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

Now, we can properly install mysql-server:

sudo apt update && sudo apt install mysql-server

You’ll need to enter a password…make sure you’ll remember it ;)

Secure Mysql Installation

Now, we want to change some default settings that might not be so secure, then run:

sudo mysql_secure_installation

Enter root password (I’ve told you to remember it) and…

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

Here’s when my nightmares started and I stumbled with:

Error: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

I noticed then that by default this service is not started, we have to start it, but using sudo service mysql start or sudo service mysqld start won’t start it, neither will sudo systemctl start mysql. They will just give you a bunch of problems, you should:

sudo /etc/init.d/mysql start

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

Now, we can again run:

sudo mysql_secure_installation

The settings I’ve used were:

Would you like to setup VALIDATE PASSWORD plugin? y
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Change the password for root?: n
Remove anonymous users? y
Disallow root login remotely? n
Remove test database and access to it? y
Reload privilege tables now? y

Making Sure Everything is Set

Now, before trying to connect to mysql, make sure we have a root has an authentication string. Enter MySQL Monitor:

mysql -u root -p

And then enter:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

Make sure you see something this

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

Now, if the authentication string is empty, we have to set up a new password for root. Still, in MySQL Monitor, run:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

And then reload grant tables:

mysql> FLUSH PRIVILEGES;

Now, check if our root user has an authentication_string:

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;

Using MySQL Workbench from Windows 10

I like connecting to the database from Windows itself, and once I have installed workbench in Windows (https://dev.mysql.com/downloads/workbench/), it opens up without any connections.

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

Now we have to create a new connection, by clicking in the add button:

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

Add a name to the connection and leave the other settings as they are:

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

Now you can simply connect to the database in your Linux Subsystem from Windows.

Can't connect to local MySQL through socket run Mysqld 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 local MySQL server?

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 server through socket '/ tmp MySQL sock?

It means either the MySQL server is not installed/running, or the file mysql. sock doesn't exist in /var/lib/mysql/ . There are a couple of solutions for this error. Then try to connect again.

Can't connect to local MySQL server through socket '/ tmp MySQL sock homebrew?

To fix this error, you need to see if MySQL server is already installed and running on your computer. That should start the server and generate the mysql. sock file. You can try to connect to your MySQL server again now.