Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect ECONNREFUSED using windows WSL

I installed nodejs through the WSL of windows 10, but any connection to the database made by the WSL terminal and refused.

The code used for testing

var mysql = require('mysql');

var con = mysql.createConnection({
  host:"localhost",
  user:"root",
  port: 3306,
  password:""
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

the error that always occurs

/mnt/d/Projetos/EstudoNode/teste.js:11
  if (err) throw err;
           ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
    --------------------
    at Protocol._enqueue (/mnt/d/Projetos/EstudoNode/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/mnt/d/Projetos/EstudoNode/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/mnt/d/Projetos/EstudoNode/node_modules/mysql/lib/Connection.js:116:18)      
    at Object.<anonymous> (/mnt/d/Projetos/EstudoNode/teste.js:10:5)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47 {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}

What can be done to connect mysql using WSL?


1 Answers

I know this post is old, but I recently had a similar issue. I tried many suggestions from other posts with the same problem. Unfortunately, running rm -rf .vscode-server and then code . didn’t fix it. It was a random issue, since there were no Windows, VS Code, or WSL updates, so I couldn’t figure out what was wrong.

It turns out the issue was on the host side: the service Host Network Service had stopped and would not restart, even after rebooting the laptop. To fix this, press the Windows button, type “Services,” find Host Network Service, change its startup type from Disabled to Automatic, and then start the service.

Services

I have to thank my colleague Alan, who helped me resolve this issue.

like image 197
Raul Fuzita Avatar answered Feb 15 '26 13:02

Raul Fuzita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!