Node.js MySQL SQL File Importer
mysql-import is a Node.js library designed to facilitate the programmatic import of `.sql` dump files into a MySQL database. Currently at version 5.0.26, the package provides a streamlined API for tasks such as database seeding, migration, or restoring backups. It differs from general ORMs or query builders by focusing solely on bulk SQL import operations. A key differentiator is its event-driven progress tracking (`onProgress`, `onDumpCompleted`), allowing developers to monitor long-running imports in real-time, which is crucial for large datasets. The library leverages Node.js streams and underlying MySQL client binaries to efficiently process SQL files of various sizes, making it a robust solution for environments requiring automated database provisioning or data synchronization.
Common errors
-
ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'localhost' (using password: YES)
cause Incorrect MySQL database credentials (username or password) or insufficient permissions for the user.fixDouble-check the `host`, `user`, and `password` parameters in the Importer constructor. Ensure the MySQL user has appropriate privileges to connect and write to the target database. -
ER_BAD_DB_ERROR: Unknown database 'mydb'
cause The specified `database` in the Importer constructor or `importer.use()` method does not exist on the MySQL server.fixCreate the database manually on your MySQL server before running the import, or ensure the SQL dump itself includes a `CREATE DATABASE` statement that executes successfully beforehand. -
Error: ENOENT: no such file or directory, open 'path/to/dump.sql'
cause The path provided to `importer.import()` is incorrect, or the SQL file does not exist at the specified location.fixVerify the absolute or relative path to your SQL dump file. Ensure file permissions allow Node.js to read the file.
Warnings
- gotcha When dealing with very large SQL dump files, ensure that your MySQL server's `max_allowed_packet` setting is sufficiently large to accommodate the largest single SQL statement. Insufficient packet size can lead to import failures.
- gotcha Character encoding mismatches between your SQL dump file and the target MySQL database or connection can lead to 'Incorrect string value' errors or corrupted data. Always ensure consistent encoding, typically UTF-8 or utf8mb4.
- gotcha If your SQL dump contains `CREATE DATABASE` or `USE database` statements, particularly when importing into an existing database or an environment with restricted permissions (e.g., shared hosting), these statements might cause 'Access denied' or 'Unknown database' errors.
- gotcha Providing dynamically generated or untrusted file paths to `importer.import()` without validation can pose a security risk, potentially allowing path traversal or the execution of arbitrary SQL files on your server.
Install
-
npm install mysql-import -
yarn add mysql-import -
pnpm add mysql-import
Imports
- Importer
const { Importer } = require('mysql-import');const Importer = require('mysql-import'); - Importer
import { Importer } from 'mysql-import';import Importer from 'mysql-import';
Quickstart
const host = 'localhost';
const user = 'root';
const password = 'password';
const database = 'mydb';
const Importer = require('mysql-import');
const importer = new Importer({host, user, password, database});
importer.onProgress(progress=>{
var percent = Math.floor(progress.bytes_processed / progress.total_bytes * 10000) / 100;
console.log(`${percent}% Completed`);
});
importer.import('path/to/dump.sql').then(()=>{
var files_imported = importer.getImported();
console.log(`${files_imported.length} SQL file(s) imported.`);
}).catch(err=>{
console.error(err);
});