我正在开发一个使用 MySQL 作为数据库的 Next.js 项目。当我运行应用程序时,几次运行后遇到以下错误:“连接到数据库时出错:错误:数据包乱序。得到:1 预期:0”。但是,构建应用程序后,并不会出现该错误,并且仅在本地开发环境中发生。
这是完整的错误堆栈跟踪:
Error connecting to database: Error: Packets out of order. Got: 1 Expected: 0
at Parser._tryReadPacketHeader (Parser.js:470:15)
at Parser.write (Parser.js:33:29)
at Protocol.write (Protocol.js:38:16)
at Socket.<anonymous> (Connection.js:88:28)
at Socket.<anonymous> (Connection.js:526:10)
at Socket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
--------------------
at Protocol._enqueue (Protocol.js:144:48)
at Protocol.handshake (Protocol.js:51:23)
at PoolConnection.connect (Connection.js:116:18)
at Pool.getConnection (Pool.js:48:16)
at eval (db.js:20:12)
at Object.(api)/./src/utils/db.js (getAllMonthlySession.js:42:1)
at __webpack_require__ (webpack-api-runtime.js:33:42)
at eval (getAllMonthlySession.js:5:67)
at Object.(api)/./src/pages/api/customer_panel/appointment/getAllMonthlySession.js (getAllMonthlySession.js:32:1)
at __webpack_require__ (webpack-api-runtime.js:33:42)
at __webpack_exec__ (getAllMonthlySession.js:52:39)
at getAllMonthlySession.js:53:28
at Object.<anonymous> (getAllMonthlySession.js:56:3)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at async DevServer.runImpl (base-server.js:432:29)
at async DevServer.run (next-dev-server.js:814:20)
at async DevServer.handleRequestImpl (base-server.js:375:20)
at async base-server.js:157:99 {
code: 'PROTOCOL_PACKETS_OUT_OF_ORDER',
fatal: true
}
如果您能了解此错误发生的原因以及如何修复它,我将不胜感激。谢谢!
这是我的 db.js
import mysql from 'mysql';
// Keeping the variable name connection instead of pool :') NVM, The deadline is tight
//Updating DB Connection:
const connection = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
multipleStatements: true,
connectTimeout: 60000,
});
connection.getConnection((err) => {
if (err) {
console.error('Error connecting to database:', err);
} else {
console.log('Connected to database');
}
});
export default connection;
我尝试过调整connectionLimit和其他配置,但都没有解决问题。
我正在使用 dotnet 8.0,unitofwork 和repositorypattern 遇到相同的错误
问题是工作单元提交是异步的
我更改为同步并且成功了