WSL: ERROR 2002 (HY000): Can't connect to local MySQL server through socket...

Hello!

I’m trying to install MySQL on Bash on Ubuntu on Windows. I’m running bash as admin and run sudo apt-get install mysql-server. In the middle of the installation I can find this:

initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
runlevel:/var/run/utmp: No such file or directory
invoke-rc.d: policy-rc.d denied execution of stop.
initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
runlevel:/var/run/utmp: No such file or directory
invoke-rc.d: policy-rc.d denied execution of start.

And when I start mysql, I get this:

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

Are this sockets not supported yet or am I doing something wrong? Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 17
  • Comments: 34 (2 by maintainers)

Most upvoted comments

I had this issue as well. MySQL is installed but could not be run automatically. You can start MySQL with this command:

sudo /etc/init.d/mysql start

@danwhitston @Kubitomakita

I found a temporary solution. Open /etc/mysql/mysqld.conf.d/mysqld.conf and add the following to the bottom of the file

[client]
protocol=tcp

This will force the mysql client to use TCP clients instead of unix sockets. That way you can connect to your localhost database running on Windows.

If you don’t need it as default, append --protocol=tcp to the mysql command.

Same thing here image

Hi

I have the same issue with Mysql.

sudo service mysql start initctl: Impossible de se connecter à Upstart: Failed to connect to socket /com/ubuntu/upstart: Connexion refusée /lib/lsb/init-functions.d/50-ubuntu-logging : ligne 5 : 214 Abandon (core dumped) plymouth --ping > /dev/null 2>&1 Starting MySQL database server mysqld /lib/lsb/init-functions.d/50-ubuntu-logging : ligne 5 : 671 Abandon (core dumped) plymouth --ping > /dev/null 2>&1 [ OK ] Checking for tables which need an upgrade, are corrupt or were not closed cleanly.

With this

mysql --host=localhost --user=root -p Enter password: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

Not exactly the same error with this

mysql --host=127.0.0.1 --user=root -p Enter password: ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (111)

i try this

mysql -uroot -p --host=localhost --port=3308 --protocol=TCP Enter password: ERROR 2003 (HY000): Can’t connect to MySQL server on ‘localhost’ (111)

Build 15063.138

Need some help please… thank you! 😃

For future visitors, I fixed my problem with MySQL version 8 installation using this comment:

Direct installation of MySQL 8.x will not work on WSL, even after your have selected 8.x as the default install candidate. It has problem starting the server.

Steps below should work (tried out on few WSL instances):

Remove MySQL 8.x: sudo apt-get purge mysql-server mysql-client sudo apt-get -y autoremove Change to MySQL 5.x candidate: sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb (if you don’t have it, get it from here: https://dev.mysql.com/downloads/repo/apt/) Double check current apt policy of MySQL is 5.x: sudo apt policy mysql-server (it will show 5.x is the default candidate) sudo apt-get update Install MySQL 5.x sudo apt-get -y install mysql-server sudo service mysql start (this should work without error) Change to MySQL 8.x candidate sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb (select 8.x) sudo apt-get update sudo apt policy mysql-server (it will show 8.x is the default candidate) Install MySQL 8.x sudo apt-get -y install mysql-server Modify a script as there is a bug sudo nano /etc/init.d/mysql (search for a line “. /usr/share/mysql/mysql-helpers” and change it to “. /usr/share/mysql-8.0/mysql-helpers”) Upgrade system tables to MySQL 8.x sudo service mysql start (this should start without error) sudo mysql -u root -p

I checked the error log in /var/log/mysql/error.log and realized there was a problem with this description “Linux Native AIO interface is not supported on this platform.” so I Googled it and bumped into the [very helpful] above comment. Hope this also works for you!

Just dropping something related here:

Say you run mysqld under Windows (Win64). And you want to connect from WSL. It will try to connect to through a socket file (named pipe), but that won’t work. Here’s how to do that:

mysql --protocol=TCP

You of course need to add all your own options (e.g -u <name> -p).

You can set this as the default behaviour. sudo vim /etc/mysql/mariadb.conf.d/50-client.cnf, under [client] comment out the socket line with a #, add a line that reads protocol = TCP (to exit vim, press [ESC] and type :wq). The path of this file might be different with an actual Oracle MySQL install, instead of MariaDB.

Still trying to figure out how to do this from the various mysql drivers in PHP. The recommended way to connect to 127.0.0.1 to circumvent the localhost heuristics doesn’t work with whatever artisan/laravel uses.

https://stackoverflow.com/a/55844865/273668

How is this said to be fixed? I’m having the same issue.

@aderchox’s answer helped me, I’m on windows 10 with wsl 2 running ubuntu 18.04. in my case the problem was in the installation of MySQL using the current apt .deb package, it proceeded with the installation but never added the mysql.sock file at /var/bin/mysql/ neither /var/lib/mysql/ . Just adding to @aderchox’s answer, the link of mysql-apt-config_0.8.15-1_all.deb is here, and if there is a problem like “MySQL 8.x could not start” or something like that, just kill the process and start again the server

@mbeijen thanks for the information. It worked out with --protocol=tcp but I had to rewrite some lines of the CLI until I got to the point where some parts weren’t editable. 😃

Still looking for some better solution.

@hacdias - Thanks for reporting this. Is manually starting MySQL using mysql start working for you? Currently, in WSL we do not start the ‘upstart’ service and so you are seeing the failure. We are looking into supporting some of these services\deamons.

@aderchox Thank you, after reseting debian to remove the old mysql8 packages your solution worked

I found that I needed to stop mysql service running in windows before I could run mysql in WSL.

I already have another mysqld server running on port: 3306

See this answer on S/O: https://stackoverflow.com/questions/44247342/starting-mysql-on-bash-on-ubuntu-on-windows

@VoLuong - Yes, Xenial is officially supported in the Insider builds. It will be also be officially supported in the Windows Creators Update release.