Connect to MySQL on WSL Ubuntu from the Windows host

MySQLnetworkingUbuntu

I am trying to connect to the MySQL server on my WSL2 Ubuntu 24.04.

I created a user in the following way inside Ubuntu:

CREATE USER 'myuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON cloud.* TO 'myuser'@'localhost'

I even tried to create a user with the % wildcard:

CREATE USER 'myuser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON cloud.* TO 'myuser'@'%'

Edit: I even tried to create another user with 0.0.0.0 because I read it is for remote access, but that didn't work either:

CREATE USER 'myuser'@'0.0.0.0' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON cloud.* TO 'myuser'@'0.0.0.0'

I then try to connect to the server from MySQL Workbench with the following details:

Connection method: Standard (TCP/IP)
Hostname: 172.26.15.15
Port: 3306
Username: myuser
Password: password

But I get:

Your connection attempt failed for user 'myuser' to the MySQL server at 172.26.15.15:3306

172.26.15.15 is my Ubuntu IP, I can even ping it from the Windows host

What am I doing wrong?

Best Answer

I found the answer - first of all I needed to edit the file /etc/mysql/mysql.conf.d/mysqld.cnf and change the line that shows bind-address to allow remote connections using 0.0.0.0:

bind-address 0.0.0.0 #originally it was 127.0.0.1

But I was still getting connection errors. This time however, I got a new error, telling me that the host that is refused is <PC_NAME>.mshome.net. So that meant that the WSL2 instance sees the host as <PC_NAME>.mshome.net

I then created a new user in MySQL and granted him permissions:

CREATE USER 'myuser'@'DESKTOP-SMF8KJ1.mshome.net' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON schema.* TO 'myuser'@'DESKTOP-SMF8KJ1.mshome.net';

And that's it, I was able to connect to the WSL MySQL server from the Windows host

Related Question