A Node.js program to call mysqldump
for exporting MySQL databases.
Before using this package, make sure you have the MySQL client (mysql-client
) installed on your operating system.
Then, install this package using npm:
npm install mysqldump-wrapper
import mysqldump from 'mysqldump-wrapper';
const args = {
database: 'your_database_name',
resultFile: 'dump.sql',
host: 'localhost',
port: 3306,
user: 'your_username',
password: 'your_password',
// ...other options
};
(async () => {
try {
await mysqldump(args);
console.log('Database dumped successfully.');
} catch (error) {
console.error('Error dumping the database:', error);
}
})();
When using the mysqldump
function in your Node.js program, you can specify various options to customize the behavior of the MySQL database dump. Here's an explanation of each available option:
-
database
(string, required): The name of the database you want to dump. -
resultFile
(string, required): The path where the SQL dump will be saved. -
host
(string, required): The MySQL host where your database is located. -
port
(number, required): The MySQL port to use for the connection. -
user
(string, required): Your MySQL username for authentication. -
password
(string, required): Your MySQL password for authentication. -
skipExtendedInsert
(boolean, optional): If set totrue
, multi-row inserts are disabled. This can be useful for creating more human-readable SQL dumps. The default isfalse
. -
skipCreateOptions
(boolean, optional): If set totrue
, it skips adding MySQL create table options in the SQL dump. The default istrue
. -
skipAddDropTable
(boolean, optional): If set totrue
, it skips adding the "DROP TABLE" statements to the SQL dump. The default istrue
. -
skipLockTables
(boolean, optional): If set totrue
, it skips locking tables during the dump. The default behavior depends on your MySQL configuration. -
skipDisableKeys
(boolean, optional): If set totrue
, it skips disabling keys during the dump. The default behavior depends on your MySQL configuration. -
skipAddLocks
(boolean, optional): If set totrue
, it skips adding lock statements during the dump. The default behavior depends on your MySQL configuration. -
delayedInsert
(boolean, optional): If set totrue
, it uses delayed inserts in the SQL dump. The default isfalse
. -
addDropTrigger
(boolean, optional): If set totrue
, it includes "DROP TRIGGER" statements in the SQL dump. The default isfalse
. -
completeInsert
(boolean, optional): If set totrue
, it inserts data line by line, which can make the SQL dump more human-readable. The default isfalse
. -
compress
(boolean, optional): If set totrue
, the resulting SQL file is compressed using Gzip. This can save disk space. The default isfalse
. -
runDry
(boolean, optional): If set totrue
, the command will not be executed, but instead, it will be printed to the console. Useful for debugging. The default isfalse
. -
log
(boolean, optional): If set totrue
, the standard output and error streams of themysqldump
process will be logged to the console. The default isfalse
. -
tables
(string[], optional): An array of table names to be included in the dump. If not specified, all tables in the database are included. -
noData
(boolean, optional): If set totrue
, it only dumps the table structure without the data. The default isfalse
. -
routines
(boolean, optional): If set totrue
, it includes stored routines (procedures and functions) in the SQL dump. The default isfalse
. -
triggers
(boolean, optional): If set totrue
, it includes triggers in the SQL dump. The default isfalse
. -
events
(boolean, optional): If set totrue
, it includes events in the SQL dump. The default isfalse
. -
where
(string, optional): Allows you to specify a WHERE clause to filter rows during the dump. -
singleTransaction
(boolean, optional): If set totrue
, it dumps all tables in a single transaction, ensuring data consistency. The default isfalse
. -
hexBlob
(boolean, optional): If set totrue
, it dumps binary strings in hexadecimal format. The default isfalse
. -
ignoreTable
(string[], optional): An array of table names to be ignored during the dump. Tables specified here will not be included in the SQL dump. -
compact
(boolean, optional): If set totrue
, it generates a less verbose SQL output. The default isfalse
. -
skipComments
(boolean, optional): If set totrue
, it skips adding comments in the output SQL file. The default isfalse
.
Feel free to customize these options according to your specific database dump requirements when using the mysqldump
function in your Node.js application.
Dump a MySQL database with default options:
const args = {
database: 'mydb',
resultFile: 'dump.sql',
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
};
await mysqldump(args);
Dump a database with custom options:
const args = {
database: 'mydb',
resultFile: 'dump.sql',
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
skipCreateOptions: true,
skipAddDropTable: false,
completeInsert: true,
compress: true,
};
await mysqldump(args);
For more advanced usage and available options, refer to the official mysqldump
documentation.
This project is licensed under the MIT License - see the LICENSE file for details.