Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to MySQL database (running on WSL2) from Windows Desktop Application "MySQL Workbench"

I set up MySQL on Windows Subsystem for Linux (WSL2 version). I'm relatively new to MySQL, but I have confirmed the following:

  • It is running (ps ax | grep mysqld returns a value)
  • It is running on default host 127.0.0.1
  • It is running on default port 3306

To login to the mysql shell, I use the command sudo mysql -u root -p. Without sudo, I am unable to login to the shell.

I assume that this issue has something to do with the host that the MySQL service is running on, but I have no idea how to change that and properly connect. Below is a screenshot of the connection setup in MySQL Workbench.

enter image description here

And below is the error that I get when I use the settings shown and my root user password.

connect to mysql workbench 2

enter image description here

like image 825
Zach Gollwitzer Avatar asked Nov 17 '19 21:11

Zach Gollwitzer


People also ask

Why MySQL database is not connecting?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

Can not connect MySQL workbench?

Check that MySQL is running on address localhost. Check that MySQL is reachable on port 3306 (note: 3306 is the default, but this can be changed). Check the user root has rights to connect to localhost from your address (MySQL rights define what clients can connect to the server and from which machines).


Video Answer


2 Answers

Turns out, this had nothing to do with WSL at all, but rather the authentication method for the MySQL user.

As of MySQL version 5.5.10, users have the ability to use auth_socket authentication. In my case, I used the Linux apt repository to configure and install MySQL, and this was set as the default authentication method, as shown by the output of the following command:

SELECT user,authentication_string,plugin,host FROM mysql.user;

MySQL Workbench does not support this type of authentication, and therefore, you must revert back to the old method of authentication, mysql_native_password.

To do this, run the following command while logged in as root, or whatever user you are trying to connect to MySQL Workbench with:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new-password';

After doing that, MySQL workbench connects fine.

like image 175
Zach Gollwitzer Avatar answered Sep 17 '22 17:09

Zach Gollwitzer


I have had issues connecting to Mysql running in WSL2 at first but I am now able to connect to the WSL2 MySQL instance via localhost. You just have to make sure that MySQL is listening on all ports which it does by default in MySQL 8.

[mysql]
port=3307 # or any port you like
bind-address=0.0.0.0

MySQL WSL2 connection setup

[Testing connection from Mysql bench to Mysql running in WSL21

like image 35
Lamin Barrow Avatar answered Sep 19 '22 17:09

Lamin Barrow