Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in Linux Subsystem for Windows 10

Alef Duarte
4 min readMar 21, 2020

--

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…

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:

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…

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

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

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.

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

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

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

--

--