Cannot connect to database in WSL with "Connection refused"
Problem
When the IDE is running inside WSL, the Database tool fails to connect and reports that it is trying to connect to 127.0.0.1, even though a different database host is configured in the data source settings:
#c.i.e.r.RemoteProcessSupport - The cook failed to start due to java.net.ConnectException: Connection refused
#c.i.d.d.BaseDatabaseErrorHandler$IOErrorInfo - Connection refused
java.net.ConnectException: Connection refused
At the same time, it's possible to connect to the same database from the Terminal inside the IDE.
Cause
One possible cause is the WSL networking backend.
In the confirmed case, the issue occurred when WSL networking mode was set to VirtioProxy. Switching the networking mode to NAT resolved the problem.
Solution
Change the WSL networking mode from VirtioProxy to NAT. Example %UserProfile%\.wslconfig:
[wsl2]
networkingMode=nat
instead of networkingMode=VirtioProxy.
Then restart WSL for the changes to take effect:
wsl --shutdown
After WSL starts again, reopen the IDE and test the database connection.
Please sign in to leave a comment.