使用Node.js连接到MySQL数据库时,获取EConRefuse127.0.0.1:3306错误
我正试图通过node.js连接到MySQL服务器,但得到了EConRefused 127.0.0.1:3306。 我在windows 10桌面上运行node,MySQL在远程Linux服务器上运行。我有MySQL的管理员权限,但无法通过node访问它 我的代码:使用Node.js连接到MySQL数据库时,获取EConRefuse127.0.0.1:3306错误,mysql,node.js,Mysql,Node.js,我正试图通过node.js连接到MySQL服务器,但得到了EConRefused 127.0.0.1:3306。 我在windows 10桌面上运行node,MySQL在远程Linux服务器上运行。我有MySQL的管理员权限,但无法通过node访问它 我的代码: var conn = mysql.createConnection({ hostname: "My mySQL server", port: 3306, user: "adminid", password: "admi
var conn = mysql.createConnection({
hostname: "My mySQL server",
port: 3306,
user: "adminid",
password: "adminpassword"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected");
conn.end();
});
=================================
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
--------------------
at Protocol._enqueue
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
参数是
host
而不是hostname
,由于您没有指定host
参数,因此默认为'localhost'
更改初始值:
let conn = mysql.createConnection({
host: "My mySQL server",
port: 3306,
user: "adminid",
password: "adminpassword"
});
当您遇到这样的问题时,请始终仔细检查文档