This website use cookies to help you have a superior and more admissible browsing experience on the website.
Loading...
mysqldump is a command-line utility that exports MySQL databases as plain-text .sql files. These files contain SQL statements — such as CREATE TABLE and INSERT INTO — that recreate your database structure and data on any compatible MySQL server.
Why Use mysqldump
.sql output is plain text, so it moves easily across different operating systems and server environments.Before diving into more advanced options, it helps to understand the core syntax first. The command follows a simple structure: you specify your MySQL user, name the database you want to dump, and tell the terminal where to save the output file.
The basic command looks like this:
mysqldump -u root -p mydb > backup.sql
Breakdown of each component:
root with your actual username if different.Follow these steps to export a MySQL database from the command line. This process works on Windows, macOS, and Linux, though the way you open the terminal varies by system.
If mysqldump isn’t recognized as a command, your system may not have MySQL added to its PATH. In that case, navigate to the MySQL installation folder first.
On Windows, the default path is usually:
cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"
mysqldump --version from your terminal. If it returns a version number, MySQL is already in your PATH and you can skip this step.Run the following command, replacing root and my_database with your actual username and database name:
mysqldump -u root -p my_database > backup.sql
You can also modify the command depending on what you need to export:
| Goal | Command |
|---|---|
| Export structure only | mysqldump -u root -p my_database --no-data > backup.sql |
| Export data only | mysqldump -u root -p my_database --no-create-info > backup.sql |
| Export multiple databases | mysqldump -u root -p --databases db1 db2 > backup.sql |
| Export all databases | mysqldump -u root -p --all-databases > full_backup.sql |
| Export specific tables | mysqldump -u root -p my_database table1 table2 > backup.sql |
The terminal will prompt you to enter your MySQL password. Type it and press Enter.
The export is complete when the cursor returns to a blank line. To confirm the file was created successfully:
.sql file in your current folder.Once you’ve exported a database with mysqldump, you’ll eventually need to restore it. Note that while mysqldump creates the export file, you use the mysql client — not mysqldump — to import it.
To import a .sql file into a database, run:
mysql -u root -p my_database < backup.sql
Replace root, my_database, and backup.sql with your actual username, target database name, and file path.
The key difference from the export command is the direction of the operator. The < sign feeds the contents of the .sql file into the mysql client, whereas > writes output to a file.
Unless you exported with --all-databases, the .sql file typically doesn’t include a CREATE DATABASE statement. You’ll need to create the database manually before importing:
CREATE DATABASE my_new_database;
If your export used a specific character set (such as UTF-8), make sure the target database is configured to match. A mismatch can result in garbled or unreadable text.
Common causes include insufficient user permissions (Access Denied) or a file size that exceeds the server’s max_allowed_packet limit. Increase this value in your MySQL configuration file if needed.
mysqldump is a reliable tool for one-off exports and manual backups. But for teams that need scheduled, automated, and centrally managed database protection, running commands manually doesn’t scale. That’s where a dedicated backup solution like i2Backup comes in.
Whether you’re protecting a single MySQL instance or managing backups across multiple servers, i2Backup gives your team consistent, automated coverage without the manual overhead of running mysqldump exports.
Q1: How do I export an entire MySQL database?
Run the following command, replacing root and my_database with your actual username and database name:
mysqldump -u root -p my_database > backup.sql
To export every database on the server at once, use the --all-databases flag instead:
mysqldump -u root -p --all-databases > full_backup.sql
Q2: How do I use mysqldump to back up a database?
Open your terminal, navigate to the MySQL bin directory if needed, and run the mysqldump command with your username, database name, and a > operator pointing to your output file. Make sure your MySQL user has the necessary SELECT and LOCK TABLES privileges on the target database.
Q3: How do I export a specific table from a MySQL database?
Add the table name after the database name in your command:
mysqldump -u root -p my_database my_table > table_backup.sql
To export multiple tables, list them one after another separated by spaces.
Q4: How do I export only the database structure without data?
Use the --no-data flag to export table definitions without any rows:
mysqldump -u root -p my_database --no-data > structure_only.sql
This is useful when setting up a new environment that needs the same schema but not the existing data.
Q5:Can I use mysqldump on Windows?
Yes. Open Command Prompt and run the same mysqldump commands as on Linux or macOS. If the command isn’t recognized, navigate to your MySQL bin directory first (e.g., cd "C:\Program Files\MySQL\MySQL Server 8.0\bin") or add MySQL to your system PATH…
mysqldump is a straightforward and flexible tool for exporting MySQL databases from the command line. Whether you’re backing up a single table, migrating an entire server, or exporting just the schema, the commands and options covered in this guide should handle most common scenarios.
That said, a backup is only useful if it can be restored successfully. Always verify your .sql files after exporting. For production environments where consistency and automation matter, pairing mysqldump with a dedicated solution like i2Backup helps reduce manual overhead and the risk of human error.