Q:

MySql on Azure Throwing Connection Timeouts

I have 2 Virtual Machines on Azure in the same Virtual Network.
One virtual machine runs a NodeJs process which is responsible for MySQL operations.
Other virtual machine runs a MySQL instance. I can connect to it from the other VM and from the NodeJs process fine.
Sometimes it will fail and throw an error about Connection timeout when acquiring a connection from the pool.
My connection string uses a local IP address from within the Virtual Network to access the database so it should have this much delay to exceed a 10 second timeout. When it works it's rapid, I mean really fast! But sometimes it just breaks and randomly starts working again. Anyone ever come across this?
If it's any help this is a MySQL instance based on Ubuntu Server 15.10.
Exception:
{
  "error": {
    "name": "Error",
    "status": 500,
    "message": "connect ETIMEDOUT",
    "errorno": "ETIMEDOUT",
    "code": "ETIMEDOUT",
    "syscall": "connect",
    "fatal": true,
    "stack": "Error: connect ETIMEDOUT
            at PoolConnection.Connection._handleConnectTimeout (projectdir/node_modules/loopback-connector-mysql/node_modules/mysql/lib/Connection.js:375:13)
            at Socket.g (events.js:180:16)
            at Socket.EventEmitter.emit (events.js:92:17)
            at Socket._onTimeout (net.js:327:8)
            at Timer.unrefTimeout [as ontimeout] (timers.js:412:13)
            --------------------
            at Protocol._enqueue (projectdir/node_modules/loopback-connector-mysql/node_modules/mysql/lib/protocol/Protocol.js:135:48)
            at Protocol.handshake (projectdir/node_modules/loopback-connector-mysql/node_modules/mysql/lib/protocol/Protocol.js:52:41)
            at PoolConnection.connect (projectdir/node_modules/loopback-connector-mysql/node_modules/mysql/lib/Connection.js:123:18)
            at Pool.getConnection (projectdir/node_modules/loopback-connector-mysql/node_modules/mysql/lib/Pool.js:45:23)
            at MySQL.executeSQL (projectdir/node_modules/loopback-connector-mysql/lib/mysql.js:200:12)
            at projectdir/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:408:10
            at projectdir/node_modules/loopback-datasource-juggler/lib/observer.js:175:9
            at doNotify (projectdir/node_modules/loopback-datasource-juggler/lib/observer.js:93:49)
            at MySQL.ObserverMixin._notifyBaseObservers (projectdir/node_modules/loopback-datasource-juggler/lib/observer.js:116:5)
            at MySQL.ObserverMixin.notifyObserversOf (projectdir/node_modules/loopback-datasource-juggler/lib/observer.js:91:8)"
  }
}

A:

I have a process running all the time and when it idles for a while the connections in the pool are in a sleeping state. Eventually MySQL purges those connections based on the wait_timeout setting in my.cnf. Once this happens and I try to use a connection it will fail because the module assumes the connection is still live and tries to use it only to get a timeout or connection exception.
To prevent this you can either overwrite the mysql module code to support "connection lifetime" in the pool or stop using the pool and manage your own connections.

