3

I'm using the MySQL module for NodeJS, however I keep getting the following error after approximately 10 requests to the database. I'm using the following code to connect to the DB:

var con = mysql.createConnection({
    host: "10.37.100.15",
    user: process.env.MYSQLUSR,
    password: process.env.MYSQLPASSWD,
    database: process.env.MYSQLDB
});

Then I use con.query(sqlStatement, function (err, result, fields) {<function>}). I already tried changing the max_allowed_packet to 500M on the server but this didn't fix the issue. This is the full error:

events.js:377
      throw er; // Unhandled 'error' event
      ^

Error: Packets out of order. Got: 0 Expected: 3
    at Parser._tryReadPacketHeader (/app/node_modules/mysql/lib/protocol/Parser.js:470:15)
    at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:33:29)
    at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:88:28)
    at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:293:12)
    at readableAddChunk (internal/streams/readable.js:267:9)
    at Socket.Readable.push (internal/streams/readable.js:206:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:423:8)
    at Protocol.emit (events.js:400:28)
    at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10)
    at Protocol.handleParserError (/app/node_modules/mysql/lib/protocol/Protocol.js:380:10)
    at Parser._tryReadPacketHeader (/app/node_modules/mysql/lib/protocol/Parser.js:478:10)
    at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:33:29)
    [... lines matching original stack trace ...]
    at Socket.emit (events.js:400:28) {
  code: 'PROTOCOL_PACKETS_OUT_OF_ORDER',
  fatal: true
}
3
  • This is looking like a server error (though there is chance its still in the mysql-nodejs connector). Which MySQL version? Which mysql-nodejs version? What are the 10 queries? If you repeat the queries in connector or a client program is the same error generated?
    – danblack
    Commented Jan 9, 2022 at 22:58
  • @danblack MySQL version: mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL) mysql-nodejs version: 2.18.1 There was actually only 1 query this time it was: SELECT privateKey FROM keyPairs WHERE publicKey='website';, I dont get any errors when executing this query in mysql workbench. One other thing to note is that all queries are done over a single connection that is opened as soon as the code starts running, I then get his error after arround 10 hours.
    – AwiJol
    Commented Jan 10, 2022 at 21:50
  • My guess is that OP used 1 connection to run multiple queries in parallel.
    – Evert
    Commented Nov 6, 2022 at 17:37

3 Answers 3

2

default wait_timeout is 8 hrs.

I'd suspect the mysql-nodejs version isn't resetting the expected packet count on re-connection.

In a test environment you can reduce the wait_timeout.

It quite possibly is:

Last comment on #2534 is "I change my connection from mysql.createConnection to mysql.createPool, the error never happen again"

6
  • I'm having the exact issue when using createPool. Commented Feb 12, 2023 at 16:03
  • 1
    I suspect in hindsight that @Evert was correct in the comment, You're using the same connection in multiple threads. As the connection has state, the query needs to be complete before another query is pushed through the same connection.
    – danblack
    Commented Feb 13, 2023 at 2:56
  • Although I'm sure, to do the queries one by one using the inner callbacks, I'll check if I did this anywhere. Commented Feb 13, 2023 at 14:09
  • Do multiple application threads use the same connection object at the same time?
    – danblack
    Commented Feb 13, 2023 at 20:49
  • I'm working without the cluster configuration of node for now, so basically no possible other threads that can use the connection in between. Commented Feb 14, 2023 at 10:25
0

I had the same issue with my backend strapi & frontend MySQL server. What I did is put wait_timeout on the MySQL cnf file to 5 seconds.

I removed that wait_timeout and the issue was resolved.

0

I had similar error. It seems the SQL connection is dropped by SQL-server when idle for some (e.g. 8h) time.

I have solved it by adding 'pings' in express server:

const con = sql.createConnection(
    {...}

// 'ping' SQL server every hour to keep connection alive
setInterval(() => {
    con.query('SELECT 1');
}, 3600000);

...
app.use();

Not the answer you're looking for? Browse other questions tagged or ask your own question.