1. Introduction
MySQL is one of the most widely used relational database management systems (RDBMS) for web services and business applications. Databases built with MySQL often serve as the core of daily operations and services. If data is lost due to system failures or human error, it can have a significant impact on business continuity.
This is where “backups” become critical. MySQL provides an official command-line tool called “mysqldump,” which allows you to easily export database contents and save them as backup files.
By leveraging this tool, you can handle various operational needs such as disaster recovery, migration to another environment, and periodic archival storage.
In this article, we will explain how to export MySQL databases using “mysqldump,” covering everything from basic usage to advanced configurations. Even beginners can follow along, as we provide detailed explanations with practical command examples.
2. Basic Usage of mysqldump
mysqldump is a command-line tool used to export the contents of a MySQL database into an SQL file. The basic syntax is very simple, and backups can be created with just a few lines of commands. In this section, we explain commonly used export methods.
Exporting a Single Database
The most common use case is exporting an entire single database.
mysqldump -u username -p database_name > backup.sqlAfter execution, you will be prompted to enter the password, and the contents of the specified database will be output to a file named backup.sql.
Exporting Multiple Databases
If you want to back up multiple databases at once, use the --databases option.
mysqldump -u username -p --databases db1 db2 db3 > multi_backup.sqlIn this format, a CREATE DATABASE statement is included for each database, which makes restoration more reliable and convenient.
Exporting All Databases
To back up all databases on the server, use the --all-databases option.
mysqldump -u username -p --all-databases > all_backup.sqlThis command exports all databases present on the MySQL server (including mysql, information_schema, performance_schema, and others).
Example: Including the Date in the Output File Name
For scheduled backups, including the date in the output file name makes management easier. Below is an example using a UNIX shell:
mysqldump -u username -p database_name > backup_$(date +%F).sqlWith this approach, a file such as backup_2025-04-13.sql is automatically generated, making backup management more organized.
3. Export Variations
mysqldump can do more than simply export an entire database. It also supports flexible export options depending on your needs. In this section, we introduce advanced techniques such as exporting specific tables, exporting only schema or data, and exporting filtered data using conditions.
Exporting Specific Tables
If you want to back up only specific tables within a database, you can specify the table names explicitly.
mysqldump -u username -p database_name table1 table2 > selected_tables.sqlExample:
mysqldump -u root -p mydb users orders > users_orders.sqlThis command exports only the users and orders tables from the mydb database.
Exporting Only Data or Only Schema
mysqldump provides options to export only schema definitions or only data.
- Export schema (structure) only:
mysqldump -u username -p --no-data database_name > schema_only.sql- Export data only (exclude CREATE TABLE statements):
mysqldump -u username -p --no-create-info database_name > data_only.sqlThese options are useful when sharing only the schema between development and production environments, or when importing incremental data.
Exporting Data with Conditions (–where)
To export only a subset of data, use the --where option. It uses the same syntax as an SQL WHERE clause.
mysqldump -u username -p database_name table_name --where="condition" > filtered_data.sqlExample:
mysqldump -u root -p mydb users --where="created_at >= '2025-01-01'" > users_2025.sqlIn this example, only records created in 2025 or later from the users table are exported.
4. Frequently Used Options and Their Explanations
Although mysqldump is simple, combining options enables safer and more efficient backups. In this section, we explain commonly used options in practical environments.
–single-transaction: Maintain Transaction Consistency
mysqldump -u username -p --single-transaction database_name > backup.sqlThis option is effective when using transaction-supported storage engines such as InnoDB. It runs the dump process within a single transaction, ensuring consistency during export without applying read locks. This is especially useful when you need to keep services running during backup.
Note: This option has no effect on non-transactional engines such as MyISAM.
–quick: Reduce Memory Usage
mysqldump -u username -p --quick database_name > backup.sqlWith this option, mysqldump retrieves rows one by one instead of loading all data into memory at once. This reduces memory consumption and improves stability when exporting large tables.
–routines and –events: Include Stored Procedures and Events
By default, stored procedures and events are not included in exports. Use the following options to include them:
mysqldump -u username -p --routines --events database_name > backup_with_logic.sql--routines: Include stored procedures and functions--events: Include scheduled events
If business logic is heavily implemented at the database level, do not forget these options.
–add-drop-table: Useful for Overwriting Tables
mysqldump -u username -p --add-drop-table database_name > backup.sqlThis option adds a DROP TABLE IF EXISTS statement before each table definition. If tables with the same name already exist in the target environment, they will be dropped before being recreated.
–lock-tables: Effective for MyISAM
mysqldump -u username -p --lock-tables database_name > backup.sqlWhile typically unnecessary for InnoDB, this option locks tables to prevent writes during export when using MyISAM. It is useful when consistency is critical.

5. How to Import an Export File
SQL files exported with mysqldump can be restored (imported) using MySQL’s standard import methods. In this section, we explain the basics of importing from a backup file, practical restore examples, and key precautions.
Basic Import Command
An exported SQL file can be imported easily using the mysql command. The basic syntax is as follows:
mysql -u username -p database_name < backup.sqlExample:
mysql -u root -p mydb < backup.sqlWhen you run this command, the SQL statements contained in backup.sql are executed in order, and the database is restored to its original state.
Importing into a New Database
Because a backup file may not include a CREATE DATABASE statement, if you want to import into a database with a different name, you must create the new database beforehand.
Example: Create a New Database “mydb_restore” and Import
CREATE DATABASE mydb_restore;mysql -u root -p mydb_restore < backup.sqlNote: SQL exported with the --databases option includes CREATE DATABASE statements, so be aware that the procedure differs in that case.
Importing a Compressed File (.gz)
If your backup file is compressed with gzip or similar, you can import it directly while decompressing:
gunzip < backup.sql.gz | mysql -u username -p database_nameThis method allows you to restore quickly while saving disk space.
Common Import Errors and How to Fix Them
| Error | Cause | Solution |
|---|---|---|
ERROR 1049 (42000): Unknown database | The target database does not exist | Create it in advance with CREATE DATABASE |
Access denied | Insufficient permissions or incorrect credentials | Recheck the username, password, and privileges |
ERROR 1064 (42000): You have an error in your SQL syntax | SQL format incompatibility between versions | Verify the dump matches the MySQL version you are using |
Summary: Treat Import as Part of the Backup Process
A backup file created with mysqldump is not valuable if you only generate it. The real value of a backup is ensuring you can restore it when you need it. For that reason, it’s recommended to regularly test imports and confirm that the file loads correctly.
6. Practical Tips and Precautions
While mysqldump is easy to use, large databases and production environments sometimes require careful operation and additional strategies. In this section, we introduce useful practical techniques and precautions to help prevent issues.
Compress Large Databases with gzip
Because mysqldump outputs plain-text SQL files, they can become very large. For large databases that exceed several gigabytes, it’s common to combine mysqldump with gzip to compress the output.
mysqldump -u username -p database_name | gzip > backup.sql.gzThis method can significantly reduce disk usage and also decreases the load during remote transfers.
Make Export and Import Verification a Habit
A backup is useless if you can’t import it when needed. We recommend operations like the following:
- Regularly restore backups in a separate environment to test
- Verify file integrity with md5sum or sha256sum
- Keep multiple backup generations for critical databases
Watch Out for Version Differences
If the MySQL version differs between the export source and import target, differences in syntax and internal behavior may prevent the SQL file from executing correctly.
- If possible, operate on the same version
- If you must cross versions, control behavior with options (e.g.,
--set-gtid-purged=OFF) - Before and after upgrades, always confirm schema definition compatibility
Use cron and Scripts for Automation
If you want to run automatic daily or weekly backups, using shell scripts and cron makes management efficient.
#!/bin/bash
DATE=$(date +%F)
mysqldump -u root -p[password] mydb | gzip > /backup/mydb_$DATE.sql.gzBy placing a script like this under /etc/cron.daily/, you can collect backups automatically every day.
Note: For security reasons, avoid writing passwords directly. Managing them safely with a
.my.cnffile is recommended.
Consider Security as Well
Backup files may contain sensitive information. Implement the following measures:
- Set appropriate access restrictions on the storage location
- Encrypt backups for storage and transfer (e.g., using GPG or SFTP)
- When storing in the cloud, consider automatic backup settings and lifecycle management
7. Frequently Asked Questions (FAQ)
In this section, we summarize common questions and frequently encountered issues when using mysqldump in a Q&A format.
Q1. Why do I get an “Access denied” error during export?
A. The specified MySQL user may lack required privileges such as “SELECT” or “LOCK TABLES” on the target database. Verify the necessary permissions and ask your administrator to grant them if needed. If table locking fails, using the --single-transaction option may help in some cases.
Q2. The backup file size is extremely large. Is there a way to reduce it?
A. If you have large tables or a high volume of data, the SQL file may reach gigabyte size. You can reduce the size using the following methods:
- Compress with
gzip(example:mysqldump ... | gzip > backup.sql.gz) - Use
--no-dataor--no-create-infoto export only what you need - Use the
--whereoption to filter exported data (e.g., specific date ranges)
Q3. How can I export only specific tables?
A. List the table names at the end of the command to export only selected tables.
mysqldump -u root -p mydb users orders > selected.sqlThis is convenient when you want to back up only certain tables instead of the entire database.
Q4. Stored procedures and events are not included in the export. Why?
A. By default, mysqldump does not include stored procedures (routines) or scheduled events. To include them, add the following options:
--routines --eventsAlso confirm that the user has sufficient privileges to access these objects.
Q5. What should I watch out for when transferring a backup file to another server?
A. Key points to consider:
- Character encoding: If encoding differs between servers, it may cause garbled text. Explicitly specify
--default-character-set=utf8if necessary. - Version differences: Ensure compatibility between MySQL versions on the source and target.
- Secure file transfer: Use
scp,rsync, orSFTPfor secure transfers. - File integrity checks: Verify integrity after transfer using
md5sumorsha256sum.
Q6. Are there differences between Windows and Mac/Linux commands?
A. The basic command syntax is the same, but differences exist in shell behavior, batch processing, and date command usage. For example, when generating file names with dates, Windows may use PowerShell or the %DATE% variable, while Linux and macOS use the date command.
8. Conclusion
In this article, we covered the MySQL backup and migration tool “mysqldump,” from basic usage to advanced techniques and troubleshooting.
Although mysqldump uses a simple syntax, choosing the correct options and command configurations based on your purpose makes a significant difference in backup quality and restoration reliability.
✅ Key Takeaways from This Article
- Basic mysqldump syntax and three export methods (single, multiple, and all databases)
- Flexible export variations such as schema-only, data-only, and selected tables
- Important options for real-world use including
--single-transactionand--routines - Basic restore commands and how to handle import errors
- Practical tips such as gzip compression and cron automation
- Useful troubleshooting knowledge provided in the FAQ section
🛡 Best Practices for Using mysqldump
- Do not just create backups—verify that they can be restored
- Prepare for issues caused by version differences and character encoding
- Design backup operations with compression, automation, and proper access control
- Include stored procedures and events to match your production configuration
A properly designed and operated backup system using mysqldump enables rapid recovery in case of unexpected failures and contributes to reliable system operations. Especially for enterprise systems and WordPress sites where data loss can be critical, understanding and effectively using mysqldump is essential.
Use this guide as a reference to build the optimal backup strategy for your environment.


