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 install mysql2
yarn add mysql2
pnpm add mysql2
Import
import mysql from 'mysql2/promise';
Quick Example
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
| Package | mysql2 |
| Category | Database |
| Weekly Downloads | 5M+ |
| License | MIT |
| Install | npm install mysql2 |
Related Packages
pg (node-postgres) is the standard PostgreSQL client for Node.js, providing low-level access to Post…
Sequelize is a promise-based Node.js ORM that supports PostgreSQL, MySQL, MariaDB, SQLite, and Micro…
Knex.js is a batteries-included SQL query builder for Node.js that supports PostgreSQL, MySQL, Maria…
Prisma is a next-generation Node.js and TypeScript ORM that provides a declarative data modeling lan…
TypeORM is a full-featured ORM for TypeScript and JavaScript that supports both Active Record and Da…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.