"Malformed communication packet" when using prepared statements
See original GitHub issueHello,
I get the following MySQL error when I invoke .execute()
with a prepared statement:
{
type: "Error",
message: "Malformed communication packet.",
stack:
"Error: Malformed communication packet.\n at PromiseConnection.execute (/path/to/project/node_modules/mysql2/promise.js:111:22)\n at UserRepository.<anonymous> (/path/to/project/src/lib/repository/UserRepository.ts:11:52)\n at step (/path/to/project/src/lib/repository/UserRepository.ts:26:23)\n at Object.next (/path/to/project/src/lib/repository/UserRepository.ts:7:53)\n at /path/to/project/src/lib/repository/UserRepository.ts:8:71\n at new Promise (<anonymous>)\n at __awaiter (/path/to/project/src/lib/repository/UserRepository.ts:4:12)\n at UserRepository.fetchUserById (/path/to/project/src/lib/repository/UserRepository.ts:51:16)",
code: "ER_MALFORMED_PACKET",
errno: 1835,
sql: "SELECT * FROM `User` WHERE `userId` = ?",
sqlState: "HY000",
sqlMessage: "Malformed communication packet.",
}
My code looks like this:
import { Connection } from "mysql2/promise";
import { User } from "./model";
export class UserRepository {
private mysqlConnection: Connection;
constructor(mysqlConnection: Connection) {
this.mysqlConnection = mysqlConnection;
}
async fetchUserById(userId: number): Promise<User> {
const params = [userId];
const [rows] = (await this.mysqlConnection.execute("SELECT * FROM `User` WHERE `userId` = ?"), params) as any;
return rows[0];
}
}
I’ve tried hard-coding the bound value, which produces the same result:
async fetchUserById(userId: number): Promise<User> {
const params = [2];
const [rows] = (await this.mysqlConnection.execute("SELECT * FROM `User` WHERE `userId` = ?"), params) as any;
return rows[0];
}
I also tried removing the params
array and hard-coding the value into the query, to eliminate the query itself as an issue. When I do this, the query executes properly and returns data.
async fetchUserById(userId: number): Promise<User> {
const [rows] = (await this.mysqlConnection.execute("SELECT * FROM `User` WHERE `userId` = 2")) as any;
return rows[0];
}
I am using MySQL 8:
bkotos@bkotos-pi:~$ mysql -V
mysql Ver 8.0.27-0ubuntu0.21.04.1 for Linux on aarch64 ((Ubuntu))
Am I doing something wrong? Or is there maybe an issue with my MySQL setup? Any help is greatly appreciated!
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
MariaDB Prepared Statement Returns Malformed ...
It seems to be happening only on prepared statements. This code has been on the server for years but started throwing errors today....
Read more >Malformed Communication Packet (8.0.23, 8.0.24, 8.0.25)
The C client library could produce a "Malformed communication packet" error if a prepared statement parameter was longer than 8KB and the ...
Read more >mysql embedded mysql_stmt_execute return "malformed ...
Description: When using prepared statement with libmysqld, if you call mysql_stmt_execute, it will return "malformed communication packet" ...
Read more >SQL Error (2027): Malformed packet - DBA Stack Exchange
This error only occurs if the result set is more than a certain number of rows (using LIMIT 14561 rows works, 14562 gives...
Read more >Database Engine events and errors - SQL Server
135, 15, No, Cannot use a BREAK statement outside the scope of a ... 672, 10, No, Failed to queue cleanup packets for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@bkotos hopefully this line can save some time debugging similar error in the future
I suspect root cause is the same as described in https://github.com/sidorares/node-mysql2/issues/1239#issuecomment-718827355
I’m working on a fix in https://github.com/sidorares/node-mysql2/pull/1407