Nodejs Error: Packets out of order. Got: 0 Expected: 244

Nodejs Error: Packets out of order. Got: 0 Expected: 244

过去 10 天我一直在努力解决这个问题。我尝试了在 google.

上找到的所有内容

我搜索并尝试了很多方法,但没有得到任何合适的解决方案。

晚上在服务器 运行“npm start”后,我在早上发现了这个错误。

throw er; // Unhandled 'error' event
    ^

Error: Packets out of order. Got: 0 Expected: 244
    at Parser.write (D:\people_hub\backend\node_modules\mysql\lib\protocol\Parser.js:42:19)
    at Protocol.write (D:\people_hub\backend\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    at Socket.<anonymous> (D:\people_hub\backend\node_modules\mysql\lib\Connection.js:103:28)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (D:\people_hub\backend\node_modules\mysql\lib\Connection.js:433:8)
    at Protocol.emit (events.js:314:20)
    at Protocol._delegateError (D:\people_hub\backend\node_modules\mysql\lib\protocol\Protocol.js:392:10)
    at Protocol.handleParserError (D:\people_hub\backend\node_modules\mysql\lib\protocol\Protocol.js:374:10)
    at Parser.write (D:\people_hub\backend\node_modules\mysql\lib\protocol\Parser.js:50:14)
    at Protocol.write (D:\people_hub\backend\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    [... lines matching original stack trace ...]
    at readableAddChunk (_stream_readable.js:272:9) {
  code: 'PROTOCOL_PACKETS_OUT_OF_ORDER',
  fatal: true
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! hrproject@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the hrproject@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\era\AppData\Roaming\npm-cache\_logs21-11-11T12_13_12_697Z-debug.log

我的Sql连接文件:

const Sequelize = require('sequelize')
const dotenv = require('dotenv');
dotenv.config();

const sequelize =
  new Sequelize(process.env.DBNAME, process.env.DBUSER, process.env.DBPASS,
    {
      host: process.env.HOST,
      port: process.env.HOST_PORT,
      dialect: 'mysql',
      operatorsAliases: 0, //false,
      timezone: "+06:00",

      pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000,
      }
    }
  )

module.exports = sequelize;

我的 app.js 文件:

const express = require('express');
var cookieParser = require("cookie-parser");
const cors = require('cors');
const app = express();

app.use(cors());
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: false /*, parameterLimit: 50000*/ }));
app.use(cookieParser());

const Login = require('./routes/mysql-login');

app.use('', Login);

module.exports = app;

我的最终连接文件:

var app = require("../server");
var debug = require("debug") // ("rest-api-nodejs-mongodb:server");
const db = require("../database/db");
var http = require("http");
const fs = require('fs');
var port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
var server = http.createServer(app);

setConnection();

server.listen(port);
server.on("error", onError);
server.on("listening", onListening);


function setConnection() {
  db.sync()
    .then((result) => {
      if (result) {
        console.log("DB Connected");
        console.log(`Nodejs Server started on port: ` + port);
      }
    })
    .catch((err) => {
      console.log("Database Connection Error: ", err);
  })
};


function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}


function onError(error) {
  if (error.syscall !== "listen") {
    console.error(`MySQL Connection Error: ${error}`);    
    throw error;    
  }

  var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    case "ELIFECYCLE":
      console.error("MySQL Connection Lost");
      setConnection();
    default:
      console.error(`MySQL Database Error: ${error}`);
      throw error;
  }
}


function onListening() {
  var addr = server.address();
  var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
  debug("Listening on " + bind);
}

我修改了 mysql 数据库:

set global net_buffer_length=1000000; 
set global max_allowed_packet=1000000000;

我在 google 上找到的所有内容我都试过了。但最后失败了。那现在怎么办?

用最新版本更新 Nodejs 端的“sequelize”和“mysql2”npm 包暂时解决了这个问题。