4

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.

2
  • How about trying to connect from server1 to server2 via command line? Commented Feb 24, 2018 at 16:54
  • @AyushGupta Thanks for your reply. I have no bindip set in /etc/mysql/my.cnf. Commented Feb 24, 2018 at 16:57

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! I was looking for bind-address in /etc/mysql/my.cnf but actually it's in /etc/mysql/mysql.conf.d/mysqld.cnf. Commenting it out didn't solve the problem, but setting it to 0.0.0.0 did. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.