MySQL
errorcore instruments the mysql2 client library when IO recording is active. Both single connections and pooled connections are supported.
Setup
Initialize errorcore before creating your MySQL connection or pool. If the pool is created before init(), queries made on that pool may not appear in the timeline.
import errorcore from "errorcore";
errorcore.init({
service: "user-api",
transport: { type: "file", path: "./errors.ndjson" },
allowUnencrypted: true,
});
// create the pool after init()
import mysql from "mysql2/promise";
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: "users",
});What gets recorded
Each query in the IO timeline includes:
- the SQL statement text
- query duration in milliseconds
- row count (when available)
- position in the ordered timeline
6ms SELECT * FROM users WHERE email = ?
12ms SELECT * FROM sessions WHERE user_id = ?
18ms Error thrown: account suspendedBind parameters
Bound values are not included by default. They are often more sensitive than the statement shape itself. Enable with captureDbBindParams: true only when needed, and pair it with encryption.
Connection pools
If you use a connection pool, make sure the pool is created after errorcore.init(). Pools created before initialization will not have their query methods instrumented. If your application architecture requires early pool creation, restart the pool after initialization.
let pool;
export function getPool() {
if (!pool) {
pool = mysql.createPool({ host: process.env.DB_HOST, database: "app" });
}
return pool;
}
// call getPool() after errorcore.init() has run