
This post was originally posted on my personal blog: xzhan.me
Recently, WSL 2 landed on the slow ring of Windows Preview. As a fan of WSL myself...
For further actions, you may consider blocking this person and/or reporting abuse
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:
I am able to connect to MariaDB from the terminal. I have tried to reinstall/install.
Any ideas how to debug this issue? Thanks!
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.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.
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
andlocalhost
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.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.I'd love to provide you with more detail. Here is my
my.cnf
: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:
Then
Start the service:
Config by following this
edit
/etc/mysql/my.cnf
(optional)Then restart mariaDB:
sudo service mysql restart
"I had a similar issue when setting up MariaDB on WSL2 – everything worked fine from the terminal, but SQLAlchemy and DataGrip couldn't connect.
What helped me:
1️⃣ Grant access to the user beyond localhost:
sql
Copy code
GRANT ALL PRIVILEGES ON . TO 'admin'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
2️⃣ Ensure MariaDB is listening on the correct port:
Check the port in use:
bash
Copy code
sudo netstat -tulnp | grep mysql
If MariaDB is running on a non-default port (e.g., 3307), try explicitly specifying it in DataGrip or SQLAlchemy:
python
Copy code
engine = create_engine("mysql+pymysql://admin:password@127.0.0.1:3307/db_name")
3️⃣ Adjust WSL2 firewall settings:
bash
Copy code
sudo ufw allow 3307/tcp
sudo systemctl restart mariadb
4️⃣ Consider a containerized approach:
If the issue persists, deploying MariaDB in an isolated environment might help. I had success running a headless eCommerce system on Ubuntu 22.04 using this step-by-step deployment guide, and the database connection worked without any conflicts.
Try these steps—containerization might resolve compatibility issues. Hope this helps!"
"Thanks for sharing this workaround! I’ve also encountered the localhost/127.0.0.1 connection issue in recent WSL2 updates, and shutting down WSL with wsl --shutdown before restarting does seem to help. It’s frustrating that disabling fast boot doesn’t fully resolve it, but at least there’s a quick fix.
By the way, if anyone is running Ubuntu on WSL and considering an upgrade, this guide on upgrading from Ubuntu 22.04 to 24.04 provides a smooth transition process. Hope this helps!"
Running mysql on windows will slowdown your wsl 2 app.
I moved mysql from windows to inside the wsl 2, and my app response is increase 10 times.
Definitely, and that's to be expected with any local vs remote network access comparison. Network latency is so much higher with the latter.

A simple test, which confirms your number of 10 times faster:
How can I start mysql automatically on WSL startup?
I am afraid I don't have the best answer. The WSL distros aren't managed by systemd so normal ways of
sudo systemctl enable xxx.service
won't work. You can try addingsh -c "sudo service mysql start"
at the end of .zshrc or .bashrc file.Thanks!!!!!