Skip to content
All findings

mysql2 3.26: PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED with MySQL 8 caching_sha2_password, only after a server restart; plus RangeError 'Too many bind parameters' from execute()

Published 30 Nov 2026

mysql2 3.26.0 (2026-11-30) on a non-TLS MySQL 8 connection: PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED, but only while the server auth cache is cold (after a restart or FLUSH PRIVILEGES). A single TLS login makes it pass. Fix: allowPublicKeyRetrieval: true, ?allowPublicKeyRetrieval=true, serverPublicKey or ssl. TypeORM ignores the URL param (use extra). Also: execute() with extra values throws RangeError, and numeric flags throw TypeError.

From mysql2 3.26.0 (2026-11-30), a connection without TLS no longer asks a MySQL 8 server for its RSA public key. A caching_sha2_password user therefore fails with PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED whenever the server's auth cache is cold, for example after a restart, but connects fine once something has logged in over TLS. To fix it, set allowPublicKeyRetrieval: true (or ?allowPublicKeyRetrieval=true in the URL), pin the key with serverPublicKey, or connect with ssl. The same upgrade (3.25.0) makes execute() throw on extra bind values.

log
Error: Server requested authentication using caching_sha2_password, which needs the server's RSA public key. Retrieving that key over a connection without TLS lets anyone on the network path substitute their own key and recover the password, so it is disabled by default. Connect over TLS (`ssl` option), provide the trusted key with the `serverPublicKey` option, or set `allowPublicKeyRetrieval` to `true` to accept this risk.  code: 'PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED',  fatal: true

Environment

  • mysql2 3.24.6 → 3.26.0, Node v22.23.3 (node:22)
  • MySQL 8.0.46 (Ubuntu 24.04 mysql-server-8.0), user app with the default caching_sha2_password. The server has auto-generated TLS certs, and the app connects over plain TCP.
  • App: a small order service (pool from DATABASE_URL, namedPlaceholders: true, execute() writes, a report query).
  • Reproduced 2026-12-01

1. mysql2 3.26: PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED, but only on a cold auth cache

On mysql2 3.24.6, against a freshly started server, every step passed. With 3.26.0 against a freshly started server, node migrate.js died on the first query with the error above. The surprising part is that it does not fail every time:

log
# 3.26.0, fresh server                       -> ERROR PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED$ mysql -h 127.0.0.1 -uapp -psecret ...      -> one login (CLI uses TLS: TLS_AES_256_GCM_SHA384)# 3.26.0 again, no code change               -> migrated, app runs$ mysql -uroot -e "FLUSH PRIVILEGES"# 3.26.0 again                               -> ERROR PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED

Why: caching_sha2_password has a fast path when the server has the user's hash cached. On a miss, the password must go over TLS or be RSA-encrypted with the server's key. Older mysql2 fetched that key over the plain connection. 3.26.0 throws instead, unless ssl, socketPath, serverPublicKey or allowPublicKeyRetrieval is set. So a laptop where someone ran the mysql CLI works, while CI, fresh containers and the first deploy after a DB restart fail.

2. Fix for mysql2 3.26 connections (each tested on a cold cache)

| config (3.26.0) | result | |---|---| | allowPublicKeyRetrieval: true | OK | | mysql://…/shop?allowPublicKeyRetrieval=true | OK | | serverPublicKey: fs.readFileSync('server_public_key.pem','utf8') | OK, no key fetch | | ssl: { rejectUnauthorized: false } | OK over TLS (no cert check) | | ssl: {} | HANDSHAKE_SSL_ERROR self-signed certificate in certificate chain (same on 3.24.6) | | allowPublicKeyRetrieval: 1 | TypeError: The \allowPublicKeyRetrieval\ option must be a boolean or the string 'true' or 'false', received number |

On a private network, the smallest change is allowPublicKeyRetrieval: true. Otherwise, copy the server's public_key.pem (in the datadir) into serverPublicKey, or use ssl with the server's CA. On 3.24.6, both allowPublicKeyRetrieval and top-level serverPublicKey only print Ignoring invalid configuration option passed to Connection, so pin mysql2@^3.26.0 when you add them.

enableCleartextPlugin: 1 now throws the same TypeError (3.24.6 accepted it). Pass real booleans, not 1 or "1" from env parsing.

3. mysql2 3.25: RangeError "Too many bind parameters" from execute()

Once connected (warm cache), three calls that passed on 3.24.6 now reject before anything is sent:

log
update status (extra value) -> ERROR RangeError  Too many bind parameters: expected at most 2, received 3page (extra value) -> ERROR RangeError  Too many bind parameters: expected at most 1, received 2select ? with 2 values -> ERROR RangeError  Too many bind parameters: expected at most 1, received 2

3.24.6 silently ignored the extra values in my runs. Issue #4589 reports misaligned values on another setup, which is why the check was added. The error has no code, so handlers that switch on err.code miss it. In my app the swallowed UPDATE error silently changed the report (open: 1, paid: 2 instead of paid: 3). Fix: pass exactly as many values as there are ?. query() (client-side formatting) is not affected.

4. mysql2 3.25: named placeholders now skip comments and identifiers

With namedPlaceholders: true, 3.24.6 replaced :name inside comments and backticks. SELECT /* :y */ :x returned y's value (9 instead of 4), and ` col:x came back as col?. 3.26.0 returns 4 and col:x`. This is a fix, but results can change if you relied on the old behaviour.

5. Where to set allowPublicKeyRetrieval in Sequelize / TypeORM / Knex / Drizzle (tested)

Each ORM was tested with mysql2 3.26.0 on a cold cache (Sequelize 6.37.8, TypeORM 1.1.3, Knex 3.3.0, Drizzle 0.45.4). Not every place you can put the option actually reaches mysql2:

| ORM | works | silently ignored (still fails) | |---|---|---| | Sequelize | dialectOptions: { allowPublicKeyRetrieval: true }, or ?allowPublicKeyRetrieval=true in the URL | top-level allowPublicKeyRetrieval: true | | TypeORM | extra: { allowPublicKeyRetrieval: true } | top-level option, and ?allowPublicKeyRetrieval=true in url | | Knex (client: 'mysql2') | in the connection object, or as a URL param | none | | Drizzle (drizzle-orm/mysql2) | URL param, drizzle({ connection: { uri, allowPublicKeyRetrieval: true } }), or your own pool | none |

The TypeORM case is the trap: the URL param that fixes Sequelize, Knex and Drizzle does nothing in TypeORM, with no warning. Drizzle wraps the failure as DrizzleQueryError: Failed query: ..., so the PUBLIC_KEY_RETRIEVAL_NOT_ALLOWED code only appears in err.cause.code.

What didn't work

  • ?allowPublicKeyRetrieval=true in a TypeORM url, and the top-level option in TypeORM or Sequelize (see section 5).
  • ssl: {} against MySQL's auto-generated self-signed certs fails the TLS handshake.
  • Switching the user to mysql_native_password works on MySQL 8.0, but that plugin is deprecated in 8.0 and disabled by default from 8.4.

How to detect

npm ls mysql2: any 3.26.x on a non-TLS connection to a caching_sha2_password user is affected. Test it after FLUSH PRIVILEGES (or a server restart), not just against a warm dev DB.

Sources

  • mysql2 3.26.0 release / commit cdf1b82 (opt-in key retrieval): https://github.com/sidorares/node-mysql2/releases/tag/v3.26.0
  • mysql2 3.25.0 release, #4591 (excess bind params), #4719 (named placeholders): https://github.com/sidorares/node-mysql2/releases/tag/v3.25.0
  • Issue #4589: https://github.com/sidorares/node-mysql2/issues/4589
  • MySQL caching_sha2_password cache behaviour: https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html