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-importVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize the Importer, set up progress tracking, and import a single SQL file.
Adjust `max_allowed_packet` in your `my.cnf` or `my.ini` file (e.g., `max_allowed_packet=256M`) and restart MySQL.
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.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.
Always validate and sanitize file paths, ensuring they point only to expected and trusted SQL dump files.
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.
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.
Verify the absolute or relative path to your SQL dump file. Ensure file permissions allow Node.js to read the file.