DEV Community

loading...

Discussion on: Setting up WSL 2 for Web Development

Collapse
quillen profile image
Quillen

After spending hours on connection to MariaDB from Windows, I still coulndt get it working.
I am using Ubuntu 20.04 LTS distro, WSL2 on Win10 64 bit.

This is what I got from Datagrip:

[08000][-1] Could not connect to address=(host=localhost)(port=3307)(type=master) : Socket fail to connect to host:localhost, port:3307. Connection refused: connect
java.net.ConnectException: Connection refused: connect.
Enter fullscreen mode Exit fullscreen mode

I am able to connect to MariaDB from the terminal. I have tried to reinstall/install.
Any ideas how to debug this issue? Thanks!

Collapse
hymanzhan profile image
Xucong ZHAN Author • Edited

I cannot guarantee a fix but I know there's a bug in recent WSL2 updates where people cannot connect to localhost/127.0.0.1 on startup. People say it's related to Windows fast boot in BIOS but even if I disable it I still hit this error.

As for the possible fix, if you are sure that your MariaDB is running in WSL and you can access from WSL terminal, try wsl --shut-down from PowerShell and restart WSL2 by opening a new WSL2 terminal. I am able to fix my connection error every time.

Collapse
quillen profile image
Quillen • Edited

Thanks for the help. After trying what you suggested, I still couldn't connect to it. Neither cloud my python application. But I can connecct to MariaDB within terminal.

EDIT: When I use the connecter provided by MariaDB, I can connect to the database from my Python app. But when I use the method described here, I got connection refused error.

Have you ever worked with MariaDB+SQLAlchemy? Love to hear how you work arond with it.

Thread Thread
hymanzhan profile image
Xucong ZHAN Author • Edited

Just to be clear, is your python app running in WSL or Windows?
Also, did you add the line bind-address=0.0.0.0 in your /etc/mysql/my.cnf?
I don't know your connection config, but double-check you are using the same host. 127.0.0.1 and localhost is not the same nor always interchangeable.

It's hard to tell what the problem is based on what you described, but I can try and replicate if that's also the case on my machine.

Update: OK I just tried, and it seems that with the latest WSL2 and MariaDB the bind-address=0.0.0.0 in /etc/mysql/my.cnf is no longer necessary. I can connect to my MariaDB from both WSL and Windows app with sqlalchemy and pymysql.

sqlalchemy to wsl mariadb

Thread Thread
quillen profile image
Quillen • Edited

OK. I think not being able to connect from datagrip and sqlalchemy is related.
MariaDB is running on WSL2 and I am able to connect to it from the terminal. I created admin user with root privilege.

MariaDB [(none)]> select user, host from information_schema.processlist;
+-------+-----------+
| user  | host      |
+-------+-----------+
| admin | localhost |
+-------+-----------+
1 row in set (0.000 sec)

I'd love to provide you with more detail. Here is my my.cnf:

[client-server]
# Port or socket location where to connect
# port = 3307
socket = /run/mysqld/mysqld.sock

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/

I tried both what you described in this blog post and not binding the address. Both not working.
Would you mind share your my.cnf with me?

Here is my complete setup:

Clean uninstall:

sudo dpkg -l | grep mariadb 
 sudo apt-get purge mariadb*

Then

sudo apt update
sudo apt install mariadb-server

Start the service:

sudo service mysql start

Config by following this

sudo mysql_secure_installation

edit /etc/mysql/my.cnf (optional)

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

Then restart mariaDB:
sudo service mysql restart