代码之家  ›  专栏  ›  技术社区  ›  Stiofán

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

  •  3
  • Stiofán  · 技术社区  · 7 年前

    节点应用程序位于SERVER1上,MySQL位于SERVER2上

    节点应用程序连接代码:

    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...");
    

    错误消息:

    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正在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   
    

    服务器2上的UFW允许端口3306:

    $ sudo ufw status
    Status: active
    
    To                         Action      From
    --                         ------      ----
    3306                       ALLOW       Anywhere                  
    3306 (v6)                  ALLOW       Anywhere (v6)   
    

    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           |
    +-----------------------------+---------------+----------------+--------------+  
    

    在SERVER2上通过命令行连接MySQL可以正常工作:

    $ mysql -u REMOTE_USER -pPASSWORD REMOTE_DATABASE
    

    MySQL或UFW日志中没有任何内容。

    我不知道还要检查什么。。。

    你能想出什么可能导致这种情况吗?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   Blue    7 年前

    MySQL只监听本地连接。请注意,在netstat命令中:

    127.0.0.1:3306
    

    看见 this answer 用于绑定到0.0.0.0。基本上,你只是想确定 bind-address 在您的 my.cnf 文件,它应该开始侦听所有接口。