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.
npm install mysql2-importVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the Importer, set up progress and completion callbacks, import a SQL file, and handle potential errors, including creating a dummy SQL file for a runnable example.
Always use `require('mysql2-import')` or `import Importer from 'mysql2-import';`.Review the API documentation for version 5.0+ and update any callback handling accordingly. For older versions, alternative progress tracking methods may be necessary or an upgrade to `mysql2-import@5.x`.
Double-check all connection parameters. Use environment variables for sensitive credentials. Verify database server accessibility and user privileges independently using a MySQL client.
Change the import statement to `const Importer = require('mysql2-import');` or `import Importer from 'mysql2-import';`Verify 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`.