aftrumpet
aftrumpet

Reputation: 1289

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:

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.

Upvotes: 20

Views: 46296

Answers (6)

Zyren
Zyren

Reputation: 663

I had to recently install a new WSL2 instance of ubuntu 22.04 and this is what worked for me. This works from installing WSL 2 fresh to getting mysql -u root working. As far as i could find this is the best and most up-to-date way of getting the latest WSL and mysql working. I just did it with WSL2 Ubuntu 22.04.2 LTS and mysql 8.0.34.

update ubuntu, install mysql, start mysql server:

sudo apt update && sudo apt upgrade
sudo apt install mysql-server
sudo /etc/init.d/mysql start

at this point, sudo mysql will let you log into mysql but if you run mysql -u root you may get the WSL: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) error. If not, skip this step.

This is what you do to get around the sock error:

sudo chmod 777 -R /var/run/mysqld

However, when you try to run mysql -u root you'll get ERROR 1698 (28000): Access denied for user 'root'@'localhost'. You need to update the mysql root password. this can be accomplished manually by going to mysql console:

sudo mysql

and then in mysql console running these lines to update your password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'INSERT_PASSWORD_HERE';
FLUSH PRIVILEGES;

then, exit out of the mysql console and when you type:

mysql -u root -p

and your password you'll get into mysql console and you're good! This will now allow you to run sudo mysql_secure_installation (if you want) without getting the access denied or sock errors.

NOTE on if sudo mysql giving access denied too: you probably FUBARed your mysql root user already by trying to do something from a previous answer on another post. It is technically possible to get it back with grant tables but I spent 4 hours trying to and it was faster to just redo my WSL instance and follow the guide above than to figure out how to get that working. Up to you.

Upvotes: 5

Stradin bien-aime
Stradin bien-aime

Reputation: 1

sudo /etc/init.d/mariadb start and then sudo mysql it worked for me

Upvotes: 0

M. Hamza Rajput
M. Hamza Rajput

Reputation: 10226

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

Upvotes: 31

Florian Richer
Florian Richer

Reputation: 31

In ubuntu version of WSL2 use this.

sudo chmod g+rx /var/run/mysqld
sudo usermod -aG mysql $USER

Upvotes: 3

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

Upvotes: 4

Renaat De Muynck
Renaat De Muynck

Reputation: 3347

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

Upvotes: 10

Related Questions