【发布时间】:2023-01-16 21:13:47
【问题描述】:
我已经为 Node 编写了一个简单的测试文件来访问我的 Ubuntu 服务器上的 MySQL。对于 Node 版本 14、15、16,它可以连接。对于 Node 版本 17、18、19,它不能。
我在 Ubuntu 20.04 上使用 MySQL 8.0.31。测试文件名为 test.js 并具有以下代码:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "correctpasswordhere"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
我使用 nvm 来选择我想要测试的节点版本。例如,我跑
nvm install 16
node test.js
输出:已连接!
但是当我使用时:
nvm install 17
node test.js
输出:
if (err) throw err;
^
Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16)
--------------------
at Protocol._enqueue (/root/gatsby_test/node_test/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/root/gatsby_test/node_test/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/root/gatsby_test/node_test/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/root/gatsby_test/node_test/test.js:9:5)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3306,
fatal: true
}
Node.js v17.9.1
我想使用最新版本的 Node,但我需要它来连接 MySQL。我用 Google 搜索了一下,但只发现有人提到登录凭据错误。有人知道发生了什么事吗?谢谢!
【问题讨论】:
-
Connection refused 表示连接请求连mysql都没有到。