简体   繁体   English

Ubuntu 16.04 Node.JS应用无法连接到MySQL数据库-错误:连接ECONNREFUSED

[英]Ubuntu 16.04 Node.JS app can't connect to MySQL database - Error: connect ECONNREFUSED

I'm trying to launch my Node.JS application on an Ubuntu virtual machine. 我正在尝试在Ubuntu虚拟机上启动Node.JS应用程序。 The application uses MySQL. 该应用程序使用MySQL。 When I run the app, I get the following error regarding what I'd assume is MySQL. 当我运行该应用程序时,我得到以下关于我认为是MySQL的错误。

events.js:182
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED myIP:3306
    at Object._errnoException (util.js:1041:11)
    at _exceptionWithHostPort (util.js:1064:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1153:14)
    --------------------
    at Protocol._enqueue (/root/pub code test/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Protocol.handshake (/root/pub code test/node_modules/mysql/lib/protocol/Protocol.js:52:23)
    at Connection.connect (/root/pub code test/node_modules/mysql/lib/Connection.js:130:18)
    at Object.<anonymous> (/root/pub code test/runTest.js:17:12)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)

Here's my code for connecting to MySQL: 这是我连接到MySQL的代码:

var connection = mysql.createConnection ({
  host: "myIP",
  user: "root",
  password: "password",
  database: "dbname"
});

connection.connect()

I've replaced the password, database, and hostname for security purposes, but I have indeed double checked all of them, along with the username 为了安全起见,我已经替换了密码,数据库和主机名,但是我确实仔细检查了所有密码以及用户名

Node.JS version: 8.4.0 Node.JS版本:8.4.0
NPM version: 5.3.0 NPM版本:5.3.0
MySQL version: 5.4.2 MySQL版本:5.4.2
Ubuntu: 16.04 Ubuntu的:16.04

I'm not sure what I did wrong to get this error but if anyone knows why, please do let me know. 我不确定我做错了什么,但是如果有人知道原因,请告诉我。 Thanks so much! 非常感谢!

Got it! 得到它了! My simple fix was to change the IP to localhost . 我的简单解决方法是将IP更改为localhost I was previously using just a standard public IP. 我以前只使用标准的公共IP。

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

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