Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 340
aftrumpet Avatar asked Nov 24 '25 20:11

aftrumpet


2 Answers

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
like image 100
M. Hamza Rajput Avatar answered Nov 27 '25 09:11

M. Hamza Rajput


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
like image 38
Renaat De Muynck Avatar answered Nov 27 '25 10:11

Renaat De Muynck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!