简体   繁体   English

节点应用程序无法连接到远程 MySQL 数据库

[英]Node app cannot connect to remote MySQL database

Node app is on SERVER1, MySQL is on SERVER2节点应用程序在 SERVER1 上,MySQL 在 SERVER2 上

Node app connection code:节点应用程序连接代码:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "REMOTE_IP",
  port: "3306",
  database: "REMOTE_DATABASE",
  user: "REMOTE_USER",
  password: "PASSWORD"
});


con.connect(function(err) {
  if (err) {
        console.log("MySQL connection error: " + err.stack);
        process.exit(1);
  }

  console.log("Connected to MySQL...");

Error message:错误信息:

MySQL connection error: Error: connect ECONNREFUSED REMOTE_IP:3306
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
--------------------
at Protocol._enqueue (/home/process/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/process/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/process/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/process/process.js:27:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3

MySQL is running on port 3306 on SERVER2: MySQL 在 SERVER2 的 3306 端口上运行:

$ sudo netstat -plunt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      4893/mysqld   

Port 3306 is allowed by UFW on SERVER2: SERVER2 上的 UFW 允许端口 3306:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
3306                       ALLOW       Anywhere                  
3306 (v6)                  ALLOW       Anywhere (v6)   

The MySQL user has remote access rights on SERVER2: MySQL 用户在 SERVER2 上具有远程访问权限:

mysql> SELECT * from information_schema.user_privileges where grantee like "'REMOTE_USER'%";
+-----------------------------+---------------+----------------+--------------+
| GRANTEE                     | TABLE_CATALOG | PRIVILEGE_TYPE | IS_GRANTABLE |
+-----------------------------+---------------+----------------+--------------+
| 'REMOTE_USER'@'localhost'     | def           | USAGE          | NO           |
| 'REMOTE_USER'@'SERVER1_PUBLIC_IP' | def           | USAGE          | NO           |
| 'REMOTE_USER'@'SERVER1_PRIVATE_IP'  | def           | USAGE          | NO           |
+-----------------------------+---------------+----------------+--------------+  

Connecting to MySQL via the command line on SERVER2 works fine:通过 SERVER2 上的命令行连接到 MySQL 工作正常:

$ mysql -u REMOTE_USER -pPASSWORD REMOTE_DATABASE

There is nothing in the MySQL or UFW logs. MySQL 或 UFW 日志中没有任何内容。

I'm not sure what else to check...我不知道还有什么要检查的......

Can you think of anything which could be causing this?你能想到什么可能导致这种情况吗?

Thanks.谢谢。

MySQL is only listening on local connections. MySQL 只侦听本地连接。 Notice in your netstat command:请注意您的 netstat 命令:

127.0.0.1:3306

See this answer for binding to 0.0.0.0.请参阅此答案以了解绑定到 0.0.0.0。 Basically, you just want to make sure bind-address is commented out in your my.cnf file, and it should start listening on all interfaces.基本上,您只想确保在my.cnf文件中注释掉bind-address ,并且它应该开始侦听所有接口。

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

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