Skip to content
Guide · Back up and restore

mysqldump: How to Back Up & Restore MySQL Databases

mysqldump is the logical backup program that ships with MySQL. It writes SQL statements that recreate your tables and data, which makes it the usual way to back up a development or small production database, copy a database to another server, or keep a schema-only snapshot. This guide covers the documented commands for MySQL 8.4 and 9.7: full, partial and structure-only dumps, remote hosts, keeping passwords off the command line, restoring, large databases, scheduling and the common errors.

Steps checked 8 October 2026 against the official documentation for each product. Versions covered: MySQL 8.4 LTS (8.4.11) and 9.7 LTS (9.7.2) client programs; MySQL Shell 8.4 dump utilities; notes for MariaDB mariadb-dump. Next review due April 2027. Installers, versions and download pages change; follow the official page if a step differs.
Short answer
  • Basic mysqldump command: mysqldump -u user -p mydb --result-file=mydb.sql; restore with mysql -u user -p mydb < mydb.sql after creating the database.
  • For InnoDB tables add --single-transaction to get a consistent dump without blocking applications, and --routines --events to include stored routines and events (triggers are included by default).
  • Since MySQL 8.4, --all-databases no longer carries routines and events through the mysql system tables, so pass --routines and --events explicitly.
  • Keep passwords out of scripts with mysql_config_editor login paths; on Windows use --result-file rather than > redirection, which can create an unloadable UTF-16 file.
  • MySQL documents mysqldump as not intended for large data volumes; for those it points to MySQL Shell's parallel dump utilities or physical backups.
How we know: Research-based: options, examples and limits were checked against the MySQL 8.4 and 9.7 Reference Manuals, the MySQL Shell 8.4 manual, the official mysql image documentation, MariaDB's mariadb-dump documentation and the Microsoft schtasks and Ubuntu crontab references on 8 October 2026. We have not run these commands for this guide; if your version differs, follow the official page.

What mysqldump does, and when to use something else

mysqldump performs logical backups: it produces SQL statements that reproduce the original object definitions and table data, and can also write CSV, other delimited text or XML. It is a client program, so it runs wherever the MySQL client tools are installed (with MySQL Server, MySQL Shell installs or the mysql Docker image) and connects to the server over the network like any other client. It is documented in both current LTS manuals, MySQL 8.4 and 9.7.

MySQL is clear about its limits. Its strengths are that you can read or edit the output before restoring and use it to clone databases for development, but it is not intended as a fast or scalable way to back up substantial amounts of data, and restoring a large dump can be very slow because every statement is replayed. For large-scale backup and restore the manual recommends a physical backup, and for logical dumps it suggests the MySQL Shell dump utilities, which add parallel threads, compression and progress output.

MariaDB users: the equivalent client is mariadb-dump. It was previously called mysqldump and the old name still works through a symlink on Linux, but from MariaDB 11.0 that symlink is deprecated and removed from the mariadb Docker image.

mysqldump command syntax and examples

There are three invocation forms: a database with optional table names, --databases followed by one or more database names, or --all-databases. The difference matters on restore. With --databases or --all-databases, the dump includes CREATE DATABASE and USE statements, so it reloads into the same database names. Without them, the dump has no database statements, so you must create the target database first and can load it under a different name.

Common mysqldump examples (bash; use --result-file on Windows)
# one database, consistent InnoDB snapshot, with routines and events
mysqldump -u backup -p --single-transaction --routines --events mydb --result-file=mydb.sql

# several databases, with CREATE DATABASE and USE statements
mysqldump -u backup -p --single-transaction --databases db1 db2 --result-file=db1-db2.sql

# all databases (routines and events must be requested explicitly in 8.4+)
mysqldump -u root -p --single-transaction --routines --events --all-databases --result-file=all-databases.sql

# selected tables only
mysqldump -u backup -p mydb customers orders --result-file=mydb-tables.sql

# structure only, then data only
mysqldump -u backup -p --no-data --routines --events mydb --result-file=mydb-schema.sql
mysqldump -u backup -p --no-create-info mydb --result-file=mydb-data.sql
Options that matter most for backups
OptionWhat it does
--single-transactionSets REPEATABLE READ and starts a transaction before dumping, giving a consistent snapshot of InnoDB tables without blocking applications. MyISAM or MEMORY tables can still change during the dump.
--routines (-R)Includes stored procedures and functions. Requires the global SELECT privilege.
--events (-E)Includes Event Scheduler events. Requires the EVENT privilege.
--triggersIncludes triggers. On by default; use --skip-triggers to leave them out.
--no-data (-d)Structure only: CREATE statements without rows.
--no-create-info (-t)Data only: no CREATE TABLE statements.
--add-drop-databaseAdds DROP DATABASE before each CREATE DATABASE (only with --databases or --all-databases).
--result-file (-r)Writes to a file; recommended on Windows to avoid newline and encoding changes from redirection.
--quick / --optEnabled by default: rows are retrieved one at a time instead of buffering whole tables in memory.
--set-gtid-purgedAUTO by default. Controls the SET @@GLOBAL.gtid_purged statement on GTID-enabled servers.

mysqldump with a remote host, and keeping passwords safe

Because mysqldump is a client, dumping a remote server only needs connection options: --host (-h, default localhost), --port (-P, default 3306) and --user (-u). The account needs read access to everything it dumps, plus the privileges listed above for routines and events.

Do not put the password on the command line: the manual calls that insecure. Use -p with no value and mysqldump prompts for it, or store the credentials once with mysql_config_editor, which writes an obfuscated .mylogin.cnf file (in %APPDATA%\MySQL on Windows, the home directory elsewhere). The manual notes the obfuscation will not stop a determined attacker with access to your files, so protect the account and the file.

Store a login path once (prompts for the password), then dump the remote host with it
mysql_config_editor set --login-path=backup --host=db.example.com --user=backup --password
mysqldump --login-path=backup --port=3306 --single-transaction --routines --events mydb --result-file=mydb.sql
Database running in Docker: run mysqldump inside the container
docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > all-databases.sql

How to restore a mysqldump file

A SQL-format dump is restored by feeding it to the mysql client. If the dump was made with --databases or --all-databases, no target database is needed. Otherwise, create the database and name it on the command line, or use source from inside the client.

On Windows PowerShell, < is reserved for future use, so MySQL documents running the restore through cmd.exe /c "mysql < dump.sql" or using source. To copy a database under a new name on the same server, dump it without --databases (which would add USE db1 and override the new name) and load it into the new database.

To move a database to another server, the manual describes the same two routes: dump with --databases and run mysql < dump.sql on the second server, which recreates the database under its original name, or dump without it, create the database on the target with mysqladmin create, and load the file into it. The second route is the one to use when the target database must have a different name.

Restore examples (bash or cmd)
# dump made with --databases or --all-databases
mysql -u root -p < all-databases.sql

# single-database dump into an existing or new database
mysqladmin -u root -p create mydb_copy
mysql -u root -p mydb_copy < mydb.sql
Restore from inside the mysql client
CREATE DATABASE IF NOT EXISTS mydb;
USE mydb;
source mydb.sql

Large databases, compression and scheduled backups

Large databases. --quick is already on through --opt, so mysqldump streams rows instead of buffering tables. Beyond that, mysqldump runs single-threaded and has no built-in file compression. MySQL Shell's util.dumpInstance(), util.dumpSchemas() and util.dumpTables() provide parallel dumping with multiple threads and file compression (zstd by default), and load back with util.loadDump(). Compression of a mysqldump file is therefore done outside MySQL, for example by piping the output through gzip; the official mysql Docker image can load .sql.gz files placed in its init folder.

Linux and macOS (cron). A crontab line has five time fields followed by the command. In a crontab, an unescaped % becomes a newline, so escape it as \% if you add a date to the file name. Use a login path so the job needs no password.

Windows (Task Scheduler). Put the command in a .cmd file and register it with schtasks /create; /sc daily with /st sets a daily start time in 24-hour HH:mm format. Run the task as the Windows user whose mysql_config_editor login path you created.

Compressed dump through gzip (bash)
mysqldump --login-path=backup --single-transaction --routines --events mydb | gzip > mydb.sql.gz
crontab entry: every day at 02:30
30 2 * * * mysqldump --login-path=backup --single-transaction --routines --events mydb --result-file=/var/backups/mysql/mydb.sql
Windows: register C:\scripts\mysql-backup.cmd to run daily at 02:30
schtasks /create /tn "MySQL backup" /tr C:\scripts\mysql-backup.cmd /sc daily /st 02:30

Common mysqldump errors and how to fix them

These are the problems the MySQL manual documents for dumping and reloading, with the fix it gives.

mysqldump problems and documented fixes
SymptomCauseFix
ERROR 1045 (28000): Access denied for userWrong user, password or host for the account; option files may supply an old passwordCheck the account and host in the error text and any option files or login paths in use.
ERROR 2003: Can't connect to MySQL serverServer not reachable on that host and portCheck --host, --port, firewalls and that the server is running.
Dump file made in PowerShell will not loadRedirection with > created a UTF-16 file, which is not a permitted connection character setDump again with --result-file=dump.sql.
Routines or events missing after restoring an --all-databases dumpFrom MySQL 8.4 their definitions live in data dictionary tables that are not dumpedAdd --routines --events to the dump.
Second partial dump fails on SET @@GLOBAL.gtid_purgedBoth dumps carry the source server's full GTID setDump with --set-gtid-purged=OFF (or COMMENTED), or remove the statement.

Frequently asked questions

How do I mysqldump all databases?

Use mysqldump --all-databases, and on MySQL 8.4 and later add --routines --events, because routine and event definitions are no longer dumped as part of the mysql system tables. Restore it with mysql < dump.sql; the file creates each database itself.

How do I dump a MySQL database to a file without the data?

Use --no-data for table definitions only, and add --routines --events to include stored routine and event definitions, for example mysqldump --no-data --routines --events mydb --result-file=mydb-schema.sql.

Does mysqldump lock tables?

With --single-transaction, InnoDB tables are dumped from a consistent snapshot without blocking applications. The manual notes that non-transactional tables such as MyISAM or MEMORY can still change during such a dump.

Is mysqldump still available in MySQL 9.7?

Yes. mysqldump is documented in the MySQL 9.7 Reference Manual as well as 8.4. MySQL recommends the MySQL Shell dump utilities when you need parallelism, compression or progress reporting.

How do I run mysqldump against a remote host from Windows?

You need the MySQL client programs on the Windows machine (they are part of the MySQL Community Server MSI and ZIP downloads). Open Command Prompt and run mysqldump --host=db.example.com --port=3306 -u backup -p mydb --result-file=mydb.sql. Use --result-file rather than >, because PowerShell redirection can produce a UTF-16 file that will not reload.

Can I use mysqldump with MariaDB?

MariaDB ships the same tool as mariadb-dump. The mysqldump name still works on many installs, but it is deprecated and has been removed from the MariaDB Docker image since 11.0, so new scripts should call mariadb-dump.

Sources

Checked 8 October 2026.

How we research guides: our editorial method. We link only to official downloads and never host installers.

Database installed?

Write your first queries with the free beginner course, then practise on real problems.