{"library":"mysql-memory-server","title":"MySQL Memory Server","description":"mysql-memory-server is a JavaScript/TypeScript library designed to spin up ephemeral MySQL databases programmatically, primarily for testing, continuous integration, and local development. It automatically downloads the necessary MySQL binaries from MySQL's CDN if a specified version is not already installed on the system, supporting a wide range of MySQL versions from 5.7.19 up to 9.6.0. The package supports Linux, macOS, and Windows environments, including Alpine Linux. It is actively maintained with frequent releases, often introducing support for the latest MySQL versions and fixing platform-specific issues. The current stable version is 1.14.1. Key differentiators include its ability to manage multiple concurrent database instances, automatic shutdown upon process exit, and robust cross-platform compatibility.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mysql-memory-server"],"cli":null},"imports":["import { createDB } from 'mysql-memory-server'","import type { MySQLDB } from 'mysql-memory-server'","import type { ServerOptions } from 'mysql-memory-server'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { createDB } from 'mysql-memory-server';\nimport sql from 'mysql2/promise'; // Remember to `npm install mysql2`\n\nasync function runEphemeralMySQL() {\n  // Create a new database with default options, or specify a version\n  const db = await createDB({\n    version: '8.4.x' // Example: specify a MySQL 8.4 version\n  });\n\n  console.log(`MySQL server started on port: ${db.port}, database: ${db.dbName}`);\n\n  // Connect to the new database\n  const connection = await sql.createConnection({\n    host: '127.0.0.1',\n    user: db.username,\n    port: db.port,\n    database: db.dbName,\n    password: '' // Default password is an empty string\n  });\n\n  try {\n    // Run your queries here\n    const [rows] = await connection.execute('SELECT 1 + 1 AS solution');\n    console.log('Query result:', rows);\n\n    await connection.execute('CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255))');\n    console.log('Table \"users\" created.');\n\n  } finally {\n    // Once done, disconnect from the database\n    await connection.end();\n    console.log('Database connection closed.');\n\n    // Then stop the database instance\n    await db.stop();\n    console.log('MySQL server stopped.');\n  }\n}\n\nrunEphemeralMySQL().catch(console.error);\n","lang":"typescript","description":"Demonstrates how to spin up an ephemeral MySQL instance, connect to it using `mysql2/promise`, run a simple query, and then properly shut down the database. Requires `mysql2` as a separate dependency.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}