Registry / database / mysql-import

mysql-import

JSON →
library5.0.26jsnpmunverified

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.

npm install mysql-import
INSTALL
IMPORT
SIG · MYSQL-IMPORT
M
mysql-import
databasejavascriptv5.0.26
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

Importer
const Importer = require('mysql-import');
const { Importer } = require('mysql-import');
For CommonJS, the library exports the Importer class directly as the module's default export.
Importer
import Importer from 'mysql-import';
import { Importer } from 'mysql-import';
For ES Modules, the Importer class is the default export. Avoid named import syntax.

Demonstrates how to initialize the Importer, set up progress tracking, and import a single SQL file.

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); });
Debug
Known issues
gotchaWhen 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.
fix
Adjust `max_allowed_packet` in your `my.cnf` or `my.ini` file (e.g., `max_allowed_packet=256M`) and restart MySQL.
affects: >=1.0.0
gotchaCharacter 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.
fix
Use `importer.setEncoding('utf8mb4')` if your dump is UTF-8 based, and verify your MySQL database and table collations are also `utf8mb4_unicode_ci` or similar.
affects: >=1.0.0
gotchaIf 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.
fix
Pre-create the database if it doesn't exist, and consider stripping `CREATE DATABASE` and `USE` statements from your SQL dump if you intend to import into a specific pre-selected database.
affects: >=1.0.0
gotchaProviding 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.
fix
Always validate and sanitize file paths, ensuring they point only to expected and trusted SQL dump files.
affects: >=1.0.0
Errors
Common errors & fixes
ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'localhost' (using password: YES)
Incorrect MySQL database credentials (username or password) or insufficient permissions for the user.
fix
Double-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'
The specified `database` in the Importer constructor or `importer.use()` method does not exist on the MySQL server.
fix
Create 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'
The path provided to `importer.import()` is incorrect, or the SQL file does not exist at the specified location.
fix
Verify the absolute or relative path to your SQL dump file. Ensure file permissions allow Node.js to read the file.
Upgrade
Version history
5.0.26latest on npm
Audit
Dependencies
mysqlrequiredProvides the underlying MySQL connection functionality; the constructor accepts options compatible with the 'mysqljs/mysql' library.
Agent activity
4 hits · last 30 days
node
4
Resources
mysql-import — npm install mysql-import · libregistry