Registry / database / db-migrate-mysql2

db-migrate-mysql2

JSON →
library1.0.0jsnpmunverified

A database driver for db-migrate that uses mysql2 instead of the deprecated mysql module. Version 1.0.0 is the current stable release, with occasional updates. It provides a drop-in replacement for the original db-migrate mysql driver, supporting modern MySQL features like prepared statements, async/await, and promises through mysql2. Unlike the original mysql driver, this version is actively maintained and compatible with MySQL 8+. It is ideal for projects that need reliable database migrations in Node.js with MySQL.

npm install db-migrate-mysql2
INSTALL
IMPORT
SIG · DB-MIGRATE-MYSQL2
D
db-migrate-mysql2
databasejavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (driver)
const dbmigrate = require('db-migrate'); const driver = dbmigrate.getInstance({ driver: 'mysql2' });
const driver = require('db-migrate-mysql2');
Do not require the driver directly. Use db-migrate API with driver field set to 'mysql2'.
driver instance with options
const dbmigrate = require('db-migrate'); const instance = dbmigrate.getInstance(true, { driver: 'mysql2', config: { host: 'localhost', user: 'root', password: '', database: 'test' } });
const DBMigrate = require('db-migrate'); const instance = new DBMigrate({ driver: 'mysql2' });
Always use getInstance() with the driver option. Do not use constructor.
type imports (TypeScript)
import type { Driver } from 'db-migrate-mysql2';
import { Driver } from 'db-migrate-mysql2';
This package doesn't export runtime symbols; only types are available if using TypeScript.

Shows how to install, configure, and run a migration using db-migrate with the mysql2 driver.

// Install: npm install db-migrate db-migrate-mysql2 // database.json { "dev": { "driver": "mysql2", "user": "root", "password": "", "host": "localhost", "database": "myapp_dev" } } // Run migration: // npx db-migrate up // Programmatic usage: const dbmigrate = require('db-migrate'); const instance = dbmigrate.getInstance(true, { driver: 'mysql2', config: { host: 'localhost', user: 'root', password: '', database: 'myapp_dev' } }); // Create migration file with up and down functions // Up: create table // Down: drop table instance.create('add-users-table', (err, migration) => { if (err) throw err; console.log('Created:', migration.name); });
Debug
Known issues
gotchaThe driver name must be exactly 'mysql2' in the configuration, but the original db-migrate mysql driver uses 'mysql'. Mixing them up will cause a 'No such driver' error.
fix
Set driver to 'mysql2' in database.json or programmatic config.
affects: all
deprecatedThe original 'mysql' driver is deprecated and may stop working. This package is the recommended replacement for MySQL.
fix
Switch to 'db-migrate-mysql2' and set driver to 'mysql2' in config.
affects: all
gotchaIf you have both 'db-migrate-mysql' and 'db-migrate-mysql2' installed, db-migrate may load the wrong driver. Uninstall the old driver to avoid conflicts.
fix
npm uninstall db-migrate-mysql
affects: all
breakingVersion 1.0.0 uses mysql2's callback API by default, but promises are available via mysql2's promise API. Ensure your migration functions handle callbacks correctly or use async/await with the promise wrapper.
fix
Use async migration functions: async function up(db) { await db.runSql('...'); }
affects: 1.0.0
Errors
Common errors & fixes
Error: No such driver found. Add it by installing db-migrate-<driver>
Using driver name 'mysql' instead of 'mysql2' in config, or driver package not installed.
fix
Install db-migrate-mysql2 and set driver: 'mysql2' in database.json
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: NO)
Missing or incorrect credentials in database configuration.
fix
Update database.json with correct user, password, host, and database.
TypeError: dbmigrate.getInstance is not a function
Using an older version of db-migrate that doesn't export getInstance, or incorrect import style.
fix
Ensure db-migrate version >= 0.11.0 and use const dbmigrate = require('db-migrate'); then call dbmigrate.getInstance()
Error: Cannot find module 'db-migrate-base'
Missing peer dependency or incorrect npm install (npm < 7 auto-installs peers).
fix
Run: npm install db-migrate-base
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
db-migrate-baserequiredrequired by db-migrate to load driver plugins
mysql2requiredactual MySQL connection and query execution
Agent activity
7 hits · last 30 days
node
6
Resources
db-migrate-mysql2 — npm install db-migrate-mysql2 · libregistry