Stiofán
Stiofán

Reputation: 543

Node app cannot connect to remote MySQL database

Node app is on SERVER1, MySQL is on 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:

$ 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:

$ 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> 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:

$ mysql -u REMOTE_USER -pPASSWORD REMOTE_DATABASE

There is nothing in the MySQL or UFW logs.

I'm not sure what else to check...

Can you think of anything which could be causing this?

Thanks.

Upvotes: 4

Views: 3388

Answers (1)

Blue
Blue

Reputation: 22911

MySQL is only listening on local connections. Notice in your netstat command:

127.0.0.1:3306

See this answer for binding to 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.

Upvotes: 3

Related Questions