question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Malformed communication packet" when using prepared statements

See original GitHub issue

Hello,

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:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sidorarescommented, Dec 4, 2021

@bkotos hopefully this line can save some time debugging similar error in the future

1reaction
sidorarescommented, Dec 1, 2021
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found