node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Connected to remote host, connection lost after a while

Open EmmEm opened this issue 10 years ago • 12 comments

Hi,

When I connect to a remote mysql server everything works fine in the beginning, but when I let the node server be idle for a couple of minutes. And then try to query the database nothing happens, the query is just pending until the http connection to the node server timeouts. The same setup I use works fine when the MySql server is on the same server as the node server.

MySql 5.6 & 5.7 (Tested both) mysql2 0.15.8 node 4.4.4

mysql.createPool({
    host: "", //Omitted, tried both IP and DNS
    user: databaseDescription.apiUserName,
    database: databaseDescription.adminDB,
    connectionLimit: config.db.connectionLimit,
    ssl : {
        ca : fs.readFileSync(config.paths.backend + '/server/keystore/db/ca-cert.pem'),
        cert : fs.readFileSync(config.paths.backend + '/server/keystore/db/client-cert.pem'),
        key : fs.readFileSync(config.paths.backend + '/server/keystore/db/client-key.pem'),
        passphrase : config.db.password
    },
    debug : true
});

The pool connection is release after the query is done.

Debug output from the query that didn't make it to the mysql server:

Add command: query
        Sending query command: SELECT //Omitted the query
0 19 <== query#start(0,,4274)

Nothing more appears in the console and when checking the mysql server I can see that it haven't reached it. It feels like it looses it's connection somehow to the mysql server but doesn't realise it.

The sql connection works fine then I use mysql client "Sequel Pro".

Let me know if you need any more information.

EmmEm avatar May 25 '16 12:05 EmmEm

How many queries on average are you ab;e to perform with connection? Doe that happen only after some idle tome or you see it with active usage as well? Do you have successful queries at all with these credentials?

sidorares avatar May 25 '16 13:05 sidorares

As long as I keep the queries coming with a relative short intervall (1-2min) the queries are successful. It's only after some idle time it happens.

EmmEm avatar May 25 '16 13:05 EmmEm

The way to get it to work again is to restart the node server.

EmmEm avatar May 25 '16 13:05 EmmEm

have you tried using original node-mysql ? does it show similar behaviour? What's your value of wait_timeout variable? http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_wait_timeout

sidorares avatar May 25 '16 13:05 sidorares

Basicly the same problem with node-mysql: But it managed to reconnect after 60s

--> ComPingPacket
ComPingPacket { command: 14 }

--> ComPingPacket
ComPingPacket { command: 14 }

--> ComPingPacket
ComPingPacket { command: 14 }

--> ComPingPacket
ComPingPacket { command: 14 }

--> ComPingPacket
ComPingPacket { command: 14 }

--> ComPingPacket
ComPingPacket { command: 14 }

<-- HandshakeInitializationPacket
HandshakeInitializationPacket {
connect_timeout 10
delayed_insert_timeout  300
have_statement_timeout  YES
innodb_flush_log_at_timeout 1
innodb_lock_wait_timeout    50
innodb_rollback_on_timeout  OFF
interactive_timeout 28800
lock_wait_timeout   31536000
net_read_timeout    30
net_write_timeout   60
rpl_stop_slave_timeout  31536000
slave_net_timeout   60
wait_timeout    28800

EmmEm avatar May 26 '16 06:05 EmmEm

I think I have found the problem, it's described here:

http://stackoverflow.com/questions/32659035/windows-azure-vms-connection-timeout-to-remote-mysql-database

and the solution is here:

https://azure.microsoft.com/en-us/blog/create-your-own-dedicated-mysql-server-for-your-azure-websites/

Especially the ssh tunnel part and ICMP blocking.

EmmEm avatar May 26 '16 07:05 EmmEm

You could try to connect over ssh using ssh2 module as suggested here - http://stackoverflow.com/questions/30658906/trouble-connecting-to-mysql-via-ssh/30669454#30669454

sidorares avatar May 26 '16 07:05 sidorares

Hi thanks for the help!

Currently i'm checking out this: https://azure.microsoft.com/en-us/documentation/articles/virtual-networks-instance-level-public-ip/

It should allow ICMP, which I believe is the problem here. I will get back with which solution I use.

EmmEm avatar May 26 '16 07:05 EmmEm

I've found the problem, altho not the solution.. yet.

The problem is described here: http://serverfault.com/questions/661704/azure-closing-idle-network-connections

Azure closes idle connections after a period of time that can span 4-30 minutes(Default 4). I've tested this by trying to query with different intervalls:

Timeline starts at 00:00:00, hh:mm:ss 00:00:00 - Success 00:03:50 - Success

Reset timeline 00:00:00 - Success 00:04:10 - Failed

Increase the azure timeout to 10, reset timeline 00:00:00 - Success 00:08:00 - Success

Reset timeline 00:00:00 - Success 00:10:10 - Failed

It seems like I need to ping the db with a set intervall, is there any support for that?

EmmEm avatar May 26 '16 13:05 EmmEm

There is no built-in "ping at interval" - you'd need to do that manually

If implemented in the library level it would look something like this: https://github.com/sidorares/node-mysql2/blob/409b797e9a37226fb7e867d2837cbade05e77e85/lib/connection.js#L265

  var done = this._command.execute(packet, this);
  if (done) {
    // console.log('RESET SEQUENCE ID')
    this.sequenceId = 0;
    this._command = this._commands.shift();
    if (this._command) {
      this.handlePacket();
      /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! heartbeat part
    } else {
       // check config for interval value anc ommand to perform
       this._heartbeatTimeout = setTimeout( () => {
          this.ping(); 
       }, 60000 );
    }
  }
};

Connection.prototype.addCommand = function (cmd) {
  /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! heartbeat part
  if (this._heartbeatTimeout) {
    // if ping was not sent yet, no need for it
    clearTimeout(this._heartbeatTimeout);
    this._heartbeatTimeout = null;
  }
  if (this.config.debug) {
    console.log('Add command: ' + arguments.callee.caller.name);
    cmd._commandName = arguments.callee.caller.name;
  }

sidorares avatar May 26 '16 13:05 sidorares

Great, I'll try it out. Thanks for your help.

EmmEm avatar May 26 '16 13:05 EmmEm

Update. The way I solved it was to lower the wait_timeout to below the azure timeout. Yes, this means that new connections will be made more often, but if I increase the azure timeout to 30min and decrease the wait_timeout to 25min it will work and in our system if no query have been asked in 25min. It should be ok to drop and remake the connection.

Hope this information will help someone else.. and spare them days of digging for information. Never thought it would be this difficult to host a dedicated mysql vm server in azure..

EmmEm avatar May 27 '16 08:05 EmmEm