🗄️
Database5M+/wkMIT

mysql2

mysql2 is a high-performance MySQL client for Node.js that provides prepared statements, non-UTF-8 encodings, binary log protocol, compression, SSL, and compati

Installation

npm
npm install mysql2
yarn
yarn add mysql2
pnpm
pnpm add mysql2

Import

ESM
import mysql from 'mysql2/promise';

Quick Example

usage
import mysql from 'mysql2/promise';

const connection = await mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'mydb',
});

const [rows] = await connection.execute(
  'SELECT * FROM users WHERE id = ?',
  [1]
);

About mysql2

mysql2 is a high-performance MySQL client for Node.js that provides prepared statements, non-UTF-8 encodings, binary log protocol, compression, SSL, and compatibility with the original mysql package API. mysql2 is significantly faster than the original mysql package due to its focus on performance — it uses a custom parser written in C++ for the MySQL binary protocol and supports prepared statements that are compiled once and executed multiple times with different parameters. The library provides both callback-based and promise-based APIs (mysql2/promise), connection pooling with configurable limits, multiple statement execution, streaming for large result sets, and automatic JSON parsing. mysql2 handles authentication plugins including the caching_sha2_password method required by MySQL 8+ default configurations. Connection configuration supports all MySQL server options including SSL certificates, connection timeouts, and connection limits. mysql2 is the MySQL driver used internally by Sequelize, Knex, TypeORM, Drizzle, and most other Node.js MySQL ORMs and query builders. The package maintains API compatibility with the original mysql package, making migration straightforward while providing substantial performance improvements.

Quick Facts

Packagemysql2
CategoryDatabase
Weekly Downloads5M+
LicenseMIT
Installnpm install mysql2

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.