0

I am attempting to use the node-mysql module which can be found here. I've used this before without any issue but on a recent project came into a strange issue of it never being able to connect to the database. I know my login credentials are correct as they work via normal MySQL command line.

However, I will always receive errors when the node-mysql module attempts to connect. This is the code I have so far, with debugging enabled.

var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'password',
    database : 'Coachletic',
    pool     : { maxConnections: 50, maxIdleTime: 30},
    debug    : true
});

connection.connect(function(err){
    if(!err) {
        console.log("MySQL Database is connected.");
    } else {
        console.log(err);
        console.log("Error connecting to MySQL Database.");  
    }
});

global.connection = connection;

This is the output from console:

<-- HandshakeInitializationPacket
HandshakeInitializationPacket {
  protocolVersion: 10,
  serverVersion: '5.6.19-0ubuntu0.14.04.1',
  threadId: 64,
  scrambleBuff1: <Buffer 65 3f 48 2e 4d 78 38 49>,
  filler1: <Buffer 00>,
  serverCapabilities1: 63487,
  serverLanguage: 8,
  serverStatus: 2,
  serverCapabilities2: 32895,
  scrambleLength: 21,
  filler2: <Buffer 00 00 00 00 00 00 00 00 00 00>,
  scrambleBuff2: <Buffer 47 68 54 4e 6b 4e 25 6a 6a 54 46 42>,
  filler3: <Buffer 00>,
  pluginData: 'mysql_native_password',
  protocol41: true }

--> ClientAuthenticationPacket
ClientAuthenticationPacket {
  clientFlags: 455631,
  maxPacketSize: 0,
  charsetNumber: 33,
  filler: undefined,
  user: 'root',
  scrambleBuff: <Buffer 40 de 4c 94 3b c4 6f 15 0d 1c 49 12 04 46 af 64 d9 4d 41 76>,
  database: 'Coachletic',
  protocol41: true }

{ [Error: Handshake inactivity timeout]
  code: 'PROTOCOL_SEQUENCE_TIMEOUT',
  fatal: true,
  timeout: undefined }
Error connecting to MySQL Database.

Tried searching for solutions for this error but did not come up with anything useful.

2
  • I don't really know what is going on, but have you tried with pools? if you want I can copy the code I am using for my MySQL connections with pools trought the node-mysql module. Commented Oct 13, 2015 at 12:12
  • Using the pools appears to connect properly but when attempting a query, I receive a similar error: { [Error: Query inactivity timeout] code: 'PROTOCOL_SEQUENCE_TIMEOUT', fatal: true, timeout: undefined } undefined Commented Oct 13, 2015 at 12:20

3 Answers 3

1

After doing some more digging it appears that this is an issue associated with Node 4.2.0. This is the reference thread: https://github.com/felixge/node-mysql/issues/1236

The fix involved changing this._idleTimeout to -1 from undefined in the lib/protocol/sequences/Sequence.js file.

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

Comments

0

This is the code that is working for me:

Initialization

var mysql = require('mysql');
var pool = mysql.createPool({
    host: 'XXX.XXX.XXX.XX',
    user: 'xxxxx_xxx',
    password: 'xxxxxxxxx',
    database: 'xxxxxxx'
});

Usage

pool.getConnection(function (err, connection) {
    connection.query('MY SQL QUERY', function (err, result) {
        connection.release();
    });

Let me know if it works for you.

1 Comment

Thanks for replying and yes this code works. I ended up finding the problem which is with Node.js itself.
0

The issue is node 4.2.0 with node-mysql.

To mention a quick simple fix, for those who have this issue and who don't care about the node version and want a quick fix, just force the previous node version (4.1.2) for you machine so things won't break. For heroku users, for example, this can be achieved by adding to the package.json as as part of the main json object: "engines": { "node": "4.1.2" }

Comments

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.