• /
  • /

mysqldump: Backup, Restore and Practical Command Examples

Run mysqldump in your terminal to write a MySQL database to a SQL file. To restore it, create an empty database on your test server and load the file with the mysql client.

Back up one database and test the restore

This example copies dump_demo from the source server to a new database called restore_demo on an isolated test server. For your own database, substitute its name for dump_demo and choose an unused target name. To follow along with sample data, use the three sample tables on a separate test source.

Prepare the connections and backup directory

Use Oracle MySQL 8.4 clients and a POSIX-style shell. Set up one login path for the source and another for the test server. Replace the example hosts and usernames with yours. Each command prompts for the password.
mysql_config_editor set --login-path=app-source --host=db-source.example --user=backup_reader --password
mysql_config_editor set --login-path=restore-test --host=db-test.example --user=restore_user --password
The source account needs SELECT on the tables. The restore account needs to create the new database and execute the SQL in the file, which can include CREATE, DROP, ALTER, INSERT, and LOCK TABLES. MySQL dump and restore privileges
Check the destination before importing anything. Compare the returned server UUID, host, port, and account with the expected identity of your isolated test server. Stop if they differ.
mysql --login-path=restore-test -e 'SELECT @@hostname, @@port, @@server_uuid, CURRENT_USER()'
Dumps contain your data. In a directory you control, create a private folder for this run. Keep using this terminal for the examples. umask 077 restricts new files to your account, and set -C makes > refuse to overwrite an existing regular file.
umask 077
set -C
mkdir -m 700 mysqldump-practice && cd mysqldump-practice
Continue only if the new directory was created and opened. If the name already exists, choose a new directory name.

Export the source database

Use this command for ordinary InnoDB tables with no custom tablespaces or stored objects. Keep schema changes such as ALTER TABLE outside the export. --single-transaction reads the InnoDB rows from one snapshot while normal writes can continue. --quick streams the rows. --no-tablespaces skips tablespace definitions, and --set-gtid-purged=OFF omits the source's GTID history from this test copy. MySQL dump options
mysqldump --login-path=app-source --single-transaction --quick \
  --no-tablespaces --set-gtid-purged=OFF dump_demo > full.sql

Restore the file on the test server

Continue only if the export succeeded and you confirmed the target server's identity. An error can leave a nonempty, partial full.sql. If the shell refused to overwrite a file, the old copy is still there. After either error, stop and correct the cause. Create and enter a fresh private directory, then repeat the export there using the same filename, full.sql. Import that file only after the new export succeeds.
Use your own single-database dump made as shown. A file made with --databases can contain USE dump_demo, which switches the active database during import. Schema-qualified names inside SQL still point to the named schema. MySQL manual on reloading SQL dumps
The sample source uses utf8mb4 and utf8mb4_0900_ai_ci. For your own database, use its character set and collation as reported by SHOW CREATE DATABASE on the source. The && below starts the import only if MySQL creates the new, empty target. Keep that check because the SQL file can contain statements that drop and recreate tables. Run without --force so MySQL stops at an error.
mysql --login-path=restore-test -e 'CREATE DATABASE restore_demo CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci' &&
mysql --login-path=restore-test restore_demo < full.sql
If the import fails partway through, read the first error and retry in a new empty database.
After a successful import, check a record your application uses. In the sample, order 1 was paid and its amount was 19.50.
mysql --login-path=restore-test restore_demo -e 'SELECT status, amount FROM orders WHERE id = 1'
For your own data, choose a closed order with a known final amount, or compare with a value recorded at the time of the backup snapshot. A later query on a busy source can include new writes.

Choose what to dump

Each example below uses the same connection profiles and protected directory. Replace the database and table names with yours. Run each export with an unused filename. Use its file only after the command succeeds.

Dump one table

Put the table name after the database name. This writes the definition and rows of orders to orders.sql.
mysqldump --login-path=app-source --single-transaction --quick \
  --no-tablespaces --set-gtid-purged=OFF dump_demo orders > orders.sql

Dump several tables

List the tables after the database name, separated by spaces. Here the order headers and their line items travel together. Include any other tables your application needs explicitly.
mysqldump --login-path=app-source --single-transaction --quick \
  --no-tablespaces --set-gtid-purged=OFF dump_demo orders order_items > sales.sql

Dump the schema only

Use --no-data when you want the table definitions, including indexes, with no rows. Restoring this file from the sample gives you three empty tables.
mysqldump --login-path=app-source --single-transaction --quick \
  --no-tablespaces --set-gtid-purged=OFF --no-data dump_demo > schema.sql

Dump data only and load it into matching tables

Use --no-create-info to leave out table creation and --skip-triggers to leave out trigger definitions. If you are starting with a fresh target, export its matching structure into a separate file first. Keep the source definitions unchanged through both exports. Stop if either command fails.
mysqldump --login-path=app-source --single-transaction --quick \
  --no-tablespaces --set-gtid-purged=OFF --no-data dump_demo > data-schema.sql
mysqldump --login-path=app-source --single-transaction --quick \
  --no-tablespaces --set-gtid-purged=OFF --no-create-info --skip-triggers \
  dump_demo > data-rows.sql
After both exports succeed, use the verified test connection to create a new database, load the structure, then load the rows. Each command must succeed for the next to run.
mysql --login-path=restore-test -e 'CREATE DATABASE restore_rows CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci' &&
mysql --login-path=restore-test restore_rows < data-schema.sql &&
mysql --login-path=restore-test restore_rows < data-rows.sql
Loading a rows file into a database before the tables exist gives ERROR 1146. The sample restored in this order has three tables and six rows.

Exclude one table

To keep the application tables and omit the audit log, give --ignore-table the full database.table name. Repeat that option for additional exclusions.
mysqldump --login-path=app-source --single-transaction --quick \
  --no-tablespaces --set-gtid-purged=OFF --ignore-table=dump_demo.audit_log \
  dump_demo > no-audit.sql
In the sample, no-audit.sql restores orders and order_items.

Dump rows that match a condition

To copy paid orders, apply --where to the orders table. This exports its definition plus the matching rows. You must select related line items separately if you need them.
mysqldump --login-path=app-source --single-transaction --quick \
  --no-tablespaces --set-gtid-purged=OFF --where="status = 'paid'" \
  dump_demo orders > paid.sql
The sample restores order IDs 1 and 3. Order 2 is pending.

Verify the restored objects and data

For a source that stayed unchanged through the drill, run the same checks against both databases. Compare the object names and types, the table definition, and the rows in primary-key order.
mysql --login-path=app-source dump_demo -e 'SHOW FULL TABLES; SHOW CREATE TABLE order_items; SELECT id, status, amount FROM orders ORDER BY id'
mysql --login-path=restore-test restore_demo -e 'SHOW FULL TABLES; SHOW CREATE TABLE order_items; SELECT id, status, amount FROM orders ORDER BY id'
In both outputs, look for audit_log, order_items, and orders, each listed as BASE TABLE. Check that the order_items definition includes the primary key on id and the idx_order_id index on order_id. The order rows should agree column by column.
Releem tested the backup and restore on two isolated Oracle MySQL 8.4.7 instances. All three table definitions and all six rows matched, including both order items and the audit entry. If your check differs, find the first missing object or changed value and inspect the export and import errors before relying on the copy.

Optional: Create the three sample tables

To reproduce the drill, point app-source at a disposable test source and use an account that can create and populate dump_demo there. Run this once in your terminal. The first statement fails if the database already exists. Use the default batch behavior without --force. Once setup succeeds, follow the full-database example above, keeping the sample unchanged through verification.
mysql --login-path=app-source <<'SQL'
CREATE DATABASE dump_demo CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
USE dump_demo;

CREATE TABLE orders (
  id INT PRIMARY KEY,
  status VARCHAR(12) NOT NULL,
  amount DECIMAL(8,2) NOT NULL
) ENGINE=InnoDB;

CREATE TABLE order_items (
  id INT PRIMARY KEY,
  order_id INT NOT NULL,
  sku VARCHAR(32) NOT NULL,
  KEY idx_order_id (order_id)
) ENGINE=InnoDB;

CREATE TABLE audit_log (
  id INT PRIMARY KEY,
  message VARCHAR(64) NOT NULL
) ENGINE=InnoDB;

INSERT INTO orders VALUES (1, 'paid', 19.50), (2, 'pending', 7.25), (3, 'paid', 32.00);
INSERT INTO order_items VALUES (1, 1, 'starter'), (2, 3, 'pro');
INSERT INTO audit_log VALUES (1, 'fixture loaded');
SQL

Troubleshoot a dump or restore

Connection refused or socket error. Check the profile used by the failing client. For the source, run mysql --login-path=app-source -e 'SELECT 1'. For the destination, run mysql --login-path=restore-test -e 'SELECT 1'. A result of 1 confirms that the profile can connect and run a query. If the check fails, verify the host, port, or socket in the error message. On Unix, localhost can use a socket even when a port is supplied. 127.0.0.1 selects TCP for a local server. How to identify the MySQL port
Packet too large. Check the size of the row, BLOB, or extended INSERT that failed. Compare the packet limit of the client doing the transfer (mysqldump for export, mysql for import) with the server's max_allowed_packet. Either limit can prevent the transfer. How max_allowed_packet works

When you need more than a SQL dump

Different engines or objects. MyISAM and MEMORY rows can change during a single-transaction dump. For custom tablespaces, plan how the destination will provide them. Triggers are included by default and require TRIGGER to dump. Views need SHOW VIEW. Stored procedures and functions require --routines, and scheduled events require --events. Review schema-qualified references and DEFINER accounts before moving these objects. A replica needs its own GTID and binary log setup. MySQL dump options
A short recovery window. The restore runs SQL statements, inserts rows, and builds indexes. Identify your largest tables and include them in a timed restore. If that is too slow, test a physical backup or a parallel restore against the same time budget. How to check MySQL table and database size
Recovery to a specific moment. To recover to just before an accidental change, you need a base backup, its corresponding starting position, and a continuous sequence of binary logs through the target time. MySQL point-in-time recovery
Restore your own copy on an isolated server. Time the import and check a value your application depends on against its value at backup time. To measure the entire recovery, start before preparing the target and stop after verification.