sajad torkamani

mysqldump is a handy little tool that helps us easily create database backups by creating a .sql dump file that replicates the database schema and content.

Creating the dump file

With authentication:

mysqldump -u <username> -p <database> > <dump_file>

Without authentication

If you've used something like mysql_config_editor to configure passwordless authentication, you can simply run:

mysqldump <database> > <dump_file>

With CREATE DATABASE statement

By default, mysqldump will not add a CREATE DATABASE IF NOT EXISTS statement but we can tell it do so by passing the -B or --databases command. See man mysqldump for more info.

mysqldump -B <database> > <dump_file>

Importing the dump file

Once we've created the dump file, we can apply it to an existing database by running:

mysql <database> < <dumpfile>


Tagged: MySQL