繁体   English   中英

使用 Node.js 连接到 MySQL 数据库时出现 ECONNREFUSED 127.0.0.1:3306 错误

[英]Getting ECONNREFUSED 127.0.0.1:3306 error while conencting toa MySQL db using Node.js

我正在尝试通过 node.js 连接到 MySQL 服务器,但得到 ECONNREFUSED 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: "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"
});

当您遇到此类问题时,请务必仔细检查文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM