microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.24k stars 811 forks source link

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

Open OmerMessing opened 1 year ago

OmerMessing commented 1 year ago

Windows Version

Microsoft Windows [Version 10.0.19044.2846]

WSL Version

wsl/wsl.exe --version command not recognized

Are you using WSL 1 or WSL 2?

Kernel Version

5.10.16

Distro Version

Ubuntu 22.04

Other Software

mysql Ver 8.0.31-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))

Repro Steps

  1. Install WSL2
  2. Install Ubuntu 22.04
  3. Install mysql 8
  4. Try to connect to mysql or run "mysql" on ubuntu

Expected Behavior

mysql should run as expected.

Actual Behavior

This error appears:

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

Additional information:

I looked for everywhere to fix this problem which dates back several years (from WSL1), the only solution I found in a stackoverflow post is to run the following 3 commands and then it works. The only problem is I have to run them every time I restart my computer. There were old reported issues of this error that were closed but I don't think this issue is fixed since it comes up again after installing all recent versions, this issue also persists in colleagues of mine that recently installed WSL2 on their machines.

sudo mkdir /var/run/mysqld
sudo chmod 777 -R /var/run/mysqld
mysqld 

Diagnostic Logs

No response

elsaco commented 1 year ago

@OmerMessing try sudo mysql . Don't touch /var/run/mysqld!

OmerMessing commented 1 year ago

@elsaco believe me, I tried far more than sudo mysql. It only works running the first 3 commands I copied in my original post (first I have to create the directory because I can't change its permissions if it doesn't exist! and it gets deleted on every restart), then I have to give it permissions, only then I can run using mysqld.

elsaco commented 1 year ago

On a fresh WSL Ubuntu installation adding mysql-server does not require any changes to /var/run/mysqld. By default MySQL has authentication method set to auth_socket and running sudo mysql will open a session:

elsaco@focal:~$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

and status show the connection via a socket:

mysql> status
--------------
mysql  Ver 8.0.32-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))

Connection id:          8
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.32-0ubuntu0.20.04.2 (Ubuntu)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
UNIX socket:            /var/run/mysqld/mysqld.sock
Binary data as:         Hexadecimal
Uptime:                 57 sec

Threads: 2  Questions: 5  Slow queries: 0  Opens: 119  Flush tables: 3  Open tables: 38  Queries per second avg: 0.087
--------------
OmerMessing commented 1 year ago

@elsaco

This is what I get when I try sudo mysql:

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

Regardless, I think we have different versions of software since I'm running Ubuntu 22.

OneBlue commented 1 year ago

@OmerMessing: Are you using systemd ?

If not, the mysql daemon needs to be started manually, that's done by running mysqld as root (mysql is just the client).

If mysqld is running, can you share an strace via strace -f mysqld ?

OmerMessing commented 1 year ago

@OneBlue

How can I restrict strace to not show sensitive information?

urbgimtam commented 11 months ago

Had the same issue today. Found this SO reply, which worked for me. https://stackoverflow.com/a/74315373

This issue occurs in WSL because mysql or mariadb is installed but is not running yet.

Use  sudo /etc/init.d/mysql start to start the server.

If this doesn't work you might also had an error during installation because it couldn't set the runlevel. In my case I had to deinstall mysql/mariadb and install it as follows:

sudo su
export RUNLEVEL=1
apt install mysql-server
/etc/init.d/mysql start
Apticle commented 8 months ago

I was able to resolve this issue by enabling systemd at boot via the wsl.conf file (reference docs).

/etc/wsl.conf

[boot]
systemd=true

Then, open powershell and restart wsl via wsl --shutdown.

You can then test with the service mysql status command, or connecting to mysql client.

If this still doesn't work, you may need to update WSL with wsl --update.