我在 Windows 计算机上安装了 mySQL 服务器,还安装了 WSL,这样我就可以在 Windows 计算机上使用 Linux 的所有优点。我有一个任务是使用 Express 来创建一个连接到数据库的 JS api,所以我运行我的 mySQL 服务器,一切都运行正常,但是当我使用 WSL 启动 JS apinode index.js并尝试连接到数据库时,它会抛出一个错误sql错误
Server running on port 8080 , http://localhost:8080
/mnt/c/Users/ngkil/Documents/VSCode/WebDevAssignment2022/Backend/index.js:43
if (err) { throw err; }
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
Node.js v18.1.0
Run Code Online (Sandbox Code Playgroud)
如果我从 CMD 运行相同的命令,node index.jsAPI 工作正常
Server running on port 8080 , http://localhost:8080
Connected to database …Run Code Online (Sandbox Code Playgroud)