MySQL2 SQL File Importer
mysql2-import, currently at version 5.0.22, is a Node.js library specifically designed for importing `.sql` dump files into a MySQL database. It's an adaptation of the `mysql-import` project, updated to seamlessly integrate with the `mysql2` driver. This integration leverages `mysql2`'s enhanced performance and modern features, including prepared statements, which are crucial for secure and efficient database interactions. The library provides an intuitive API for connecting to a database, specifying multiple `.sql` files or directories containing dumps, and executing their commands asynchronously. A key differentiator introduced in version 5.0 is its robust event system, featuring `onProgress` and `onDumpCompleted` callbacks. These allow developers to monitor the import process in real-time, receiving updates on bytes processed and file completion status, making it highly suitable for handling large SQL datasets. While no explicit release cadence is documented, it generally aligns with the versioning of its upstream `mysql-import` project, delivering stable and feature-rich releases.
Common errors
-
Error: Cannot find module 'mysql-import'
cause This error occurs because the example in the `mysql2-import` README uses `require('mysql-import')` which refers to the original package, not `mysql2-import`.fixChange the import statement to `const Importer = require('mysql2-import');` or `import Importer from 'mysql2-import';` -
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'localhost' (using password: YES/NO)
cause The provided username or password for the MySQL connection is incorrect, or the user lacks permissions from the specified host.fixVerify your MySQL `user`, `password`, and `host` credentials. Ensure the user has `GRANT` privileges for the target database and can connect from `localhost` or the specified `host`.
Warnings
- gotcha The README examples for `require` syntax incorrectly use `require('mysql-import')` instead of the package's actual name, `mysql2-import`. This will lead to a 'Cannot find module' error.
- breaking The `onProgress` and `onDumpCompleted` methods were introduced in version 5.0. Code relying on these callbacks will break if upgrading from a version older than 5.0.
- gotcha Providing incorrect database connection parameters (host, user, password, database) is a very common source of errors. Ensure your MySQL server is running, the user has appropriate permissions, and network access is configured correctly.
Install
-
npm install mysql2-import -
yarn add mysql2-import -
pnpm add mysql2-import
Imports
- Importer
const Importer = require('mysql-import');import Importer from 'mysql2-import';
- Importer
import { Importer } from 'mysql2-import';const Importer = require('mysql2-import'); - Importer.prototype.import
importer.import(['path/to/dump.sql']);
importer.import('path/to/dump.sql').then(() => { /* ... */ });
Quickstart
import Importer from 'mysql2-import';
import path from 'path';
// Ensure .env or similar is set up for production
const host = process.env.MYSQL_HOST ?? 'localhost';
const user = process.env.MYSQL_USER ?? 'root';
const password = process.env.MYSQL_PASSWORD ?? 'password';
const database = process.env.MYSQL_DATABASE ?? 'mydb';
const importer = new Importer({ host, user, password, database });
// Create a dummy SQL file for demonstration if it doesn't exist
import fs from 'fs';
const dummySqlFilePath = path.join(process.cwd(), 'dummy_data.sql');
if (!fs.existsSync(dummySqlFilePath)) {
fs.writeFileSync(dummySqlFilePath, `
DROP TABLE IF EXISTS \`users\`;
CREATE TABLE \`users\` (
\`id\` INT AUTO_INCREMENT PRIMARY KEY,
\`name\` VARCHAR(255) NOT NULL
);
INSERT INTO \`users\` (name) VALUES ('Alice'), ('Bob');
`);
console.log('Created dummy_data.sql for quickstart.');
}
importer.onProgress(progress => {
const percent = Math.floor(progress.bytes_processed / progress.total_bytes * 10000) / 100;
console.log(`File ${progress.file_no} of ${progress.total_files}: ${percent}% Completed (${progress.file_path})`);
});
importer.onDumpCompleted(data => {
if (data.error) {
console.error(`Error completing dump for ${data.file_path}:`, data.error);
} else {
console.log(`Dump completed for ${data.file_path}`);
}
});
importer.import(dummySqlFilePath).then(() => {
const files_imported = importer.getImported();
console.log(`${files_imported.length} SQL file(s) imported successfully.`);
}).catch(err => {
console.error('An error occurred during import:', err);
}).finally(() => {
importer.disconnect();
console.log('Importer disconnected.');
// Clean up dummy file if it was created
if (fs.existsSync(dummySqlFilePath)) {
fs.unlinkSync(dummySqlFilePath);
console.log('Removed dummy_data.sql.');
}
});