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

Viewed 15034

I'm trying to use a fresh installation of MySQL on Windows Subsystem for Linux (Ubuntu) and can't seem to ever connect to it. I always get the error: WSL: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I've tried:

  • Switching the configuration to use localhost instead of sockets (I instead get an error saying I can't connect through localhost)
  • Using --skip-grant-tables by editing /etc/mysql/mysql.conf.d/mysqld.cnf and restarting

The file /var/run/mysqld/mysqld.sock never shows up either, though I figure this is because WSL doesn't have perfect socket support to begin with but it should still work. I suspect the problem might be specific to WSL and maybe I should try updating to WSL2? I don't know what else the issue could be.

4 Answers

I had the same issue.

Follow these steps provided by Microsoft: Add or connect a database with WSL

  1. Update your Ubuntu packages: sudo apt update
  2. install MySQL with: sudo apt install mysql-server
  3. Start a MySQL server: sudo /etc/init.d/mysql start
  4. Start the security script prompts: sudo mysql_secure_installation
  5. Open the MySQL prompt: sudo mysql

I have the same issue solved through the following steps.

Put these lines at the end of the file /etc/mysql/my.cnf

[mysqld]                                                                                                                
bind-address = 0.0.0.0                                                                                                    
user=root                                                                                                               
pid-file     = /var/run/mysqld/mysqld.pid                                                                                
socket       = /var/run/mysqld/mysqld.sock                                                                               
port         = 3306                                                                                                                                                                                                                              

[client]                                                                                                                
port         = 3306                                                                                                      
socket       = /var/run/mysqld/mysqld.sock

Then put these commands on terminal NOTE: if dir is not there then create one.

chmod 777 -R /var/run/mysqld
chmod 777 -R /var/lib/mysql
chmod 777 -R /var/log/mysql

Then start mysql using below command.

mysqld

Then open a new terminal and connect using the below command

mysql -uroot -pYourPass

Hopefully, it will work thanks.

Although M. Hamza Rajput's answer is working (checked), it might be the case that the problem happens simply because mysql is NOT running

Thus, start mysql service:

sudo service mysql start

and then you can call the secure installation

sudo mysql_secure_installation

I have to run mkdir /var/run/mysqld and then service mysql start everytime I want to run mysql. No clue why... here looking for answers but very annoying.

Related