{"library":"mysql2","title":"MySQL2 Node.js Client","description":"mysql2 is a high-performance, native JavaScript MySQL client for Node.js, currently stable at version 3.22.1. It provides a robust and efficient way to interact with MySQL databases, emphasizing speed through a re-written protocol parser. The library maintains broad API compatibility with the popular 'Node MySQL' package while introducing advanced features such as comprehensive prepared statement support, binary log protocol, SSL/TLS encryption, and data compression. It also includes a first-class promise-based API wrapper for modern async/await patterns. mysql2 is under active development with a rapid release cadence, frequently pushing out bug fixes, performance improvements, and new features, including recent security enhancements like disabling the `mysql_clear_password` plugin by default and supporting `Symbol.dispose` for resource management.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mysql2"],"cli":null},"imports":["import { createConnection } from 'mysql2';","import { createPool } from 'mysql2';","import mysql from 'mysql2/promise';","import type { Connection } from 'mysql2';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import mysql from 'mysql2/promise';\nimport { RowDataPacket, OkPacket, ResultSetHeader } from 'mysql2';\n\nasync function runExample() {\n  const pool = mysql.createPool({\n    host: process.env.DB_HOST ?? 'localhost',\n    user: process.env.DB_USER ?? 'root',\n    password: process.env.DB_PASSWORD ?? 'password',\n    database: process.env.DB_DATABASE ?? 'test_db',\n    waitForConnections: true,\n    connectionLimit: 10,\n    queueLimit: 0\n  });\n\n  try {\n    // Create a table if it doesn't exist\n    await pool.execute<ResultSetHeader>(`\n      CREATE TABLE IF NOT EXISTS users (\n        id INT AUTO_INCREMENT PRIMARY KEY,\n        name VARCHAR(255) NOT NULL,\n        email VARCHAR(255) UNIQUE NOT NULL\n      )\n    `);\n    console.log('Table \"users\" ensured.');\n\n    // Insert a new user using a prepared statement\n    const name = 'Alice';\n    const email = 'alice@example.com';\n    const [insertResult] = await pool.execute<OkPacket>(\n      'INSERT INTO users (name, email) VALUES (?, ?)',\n      [name, email]\n    );\n    console.log(`Inserted user with ID: ${insertResult.insertId}`);\n\n    // Select users\n    const [rows] = await pool.execute<RowDataPacket[]>('SELECT id, name, email FROM users WHERE name = ?', [name]);\n    if (rows.length > 0) {\n      console.log('Found users:');\n      rows.forEach(row => {\n        console.log(`- ID: ${row.id}, Name: ${row.name}, Email: ${row.email}`);\n      });\n    } else {\n      console.log(`No user found with name: ${name}`);\n    }\n\n  } catch (error) {\n    console.error('Database operation failed:', error);\n  } finally {\n    // Ensure the pool is closed when done\n    await pool.end();\n    console.log('Database pool closed.');\n  }\n}\n\nrunExample();","lang":"typescript","description":"This quickstart demonstrates how to establish a connection pool, execute a prepared statement for inserting data, and query for data using the `mysql2/promise` API with async/await, and proper resource management.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}