mysqldump examples you can copy, with what each flag changes
Copy a mysqldump command for one table, data only, structure only or a whole database, and see which flags silently leave things out of the file.

On this page
For a normal application database on InnoDB, this is the command to keep:
mysqldump --single-transaction --routines --events --no-tablespaces shop > shop.sqlIt reads one consistent snapshot without blocking writers, and it includes the stored procedures and events that a bare mysqldump shop leaves out. Load it into an empty database like this:
mysql -e "CREATE DATABASE shop_test"
mysql shop_test < shop.sqlEvery other example below is a variation on that pair. They assume your login sits in a protected option file, so no password appears in the command:
[client]
user=backup
password="the password"Save that as ~/.my.cnf and run chmod 600 ~/.my.cnf. The MySQL manual on password security recommends either this or the -p prompt, because a password typed on the command line can be visible to other users of the machine.
Which flag for which job
| You want | Use |
|---|---|
| One table | mysqldump shop orders |
| Several tables | mysqldump shop customers orders |
| Everything except a table | --ignore-table=shop.sessions |
| Structure only | --no-data |
| Data only | --no-create-info --skip-triggers |
| Some rows | --where="created >= '2024-01-01'" |
| Procedures and functions | --routines |
| Scheduled events | --events |
| No blocking of writers (InnoDB) | --single-transaction |
| Readable, safe binary columns | --hex-blob |
Several databases, with CREATE DATABASE | --databases shop blog |
The outputs further down come from mysqldump 8.0.33 reading a small database built with this script:
CREATE TABLE customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(60) NOT NULL
);
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
total DECIMAL(8,2) NOT NULL,
created DATE NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers (id)
);
CREATE TABLE sessions (
id CHAR(8) PRIMARY KEY,
payload BLOB
);
INSERT INTO customers (name) VALUES ('Ana'), ('Zoë'), ('Bram');
INSERT INTO orders (customer_id, total, created) VALUES
(1, 10.00, '2023-01-05'), (1, 20.00, '2024-03-01'),
(2, 30.00, '2024-06-09'), (3, 5.50, '2025-02-02');
INSERT INTO sessions VALUES ('aa', 0x00FF10), ('bb', 0xC3A9);
CREATE VIEW order_totals AS
SELECT customer_id, SUM(total) AS total FROM orders GROUP BY customer_id;
CREATE TRIGGER orders_bi BEFORE INSERT ON orders
FOR EACH ROW SET NEW.total = ROUND(NEW.total, 2);
CREATE PROCEDURE top_customer()
SELECT * FROM order_totals ORDER BY total DESC LIMIT 1;
CREATE EVENT purge_sessions ON SCHEDULE EVERY 1 DAY DISABLE
DO DELETE FROM sessions;One table, several tables, all but one
The first name after the options is the database. Names after it are tables:
mysqldump shop orders > orders.sql
mysqldump shop customers orders > two-tables.sql
mysqldump --ignore-table=shop.sessions shop > no-sessions.sql--ignore-table needs the database prefix, and you repeat the whole option for each table you skip. A misspelt table name fails loudly, which is what you want:
mysqldump shop nope > nope.sqlmysqldump: Couldn't find table: "nope"One caution before loading a single-table file: by default it starts with DROP TABLE IF EXISTS. Restoring orders.sql over a live database throws away the rows added since the dump. Load it into a scratch database first, or write the file with --skip-add-drop-table so that it stops on an existing table.
Structure only and data only
--no-data writes the CREATE TABLE statements and no rows. It's the fastest way to get an empty copy of a schema:
mysqldump --no-data --routines --events shop > schema.sqlFor data only, the obvious flag is --no-create-info. On its own it isn't enough: the triggers still end up in the file, because --triggers is on by default. Loading that file into a schema which already has those triggers inserts the rows of that table and then stops with ERROR 1359 (HY000): Trigger already exists. Add --skip-triggers:
mysqldump --no-create-info --skip-triggers --compact shop customers ordersINSERT INTO `customers` VALUES (1,'Ana'),(2,'Zoë'),(3,'Bram');
INSERT INTO `orders` VALUES (1,1,10.00,'2023-01-05'),(2,1,20.00,'2024-03-01'),(3,2,30.00,'2024-06-09'),(4,3,5.50,'2025-02-02');--compact strips the comments and the SET lines to make the output readable here. Leave it out of real backups. It also removes the lines that turn off foreign key checks, so a compact file can fail on table order: dumping orders before customers ended in ERROR 1824 (HY000): Failed to open the referenced table 'customers' on restore.
Three more switches change the shape of those INSERT lines:
--complete-insertadds the column list, so the file survives a reordered or extended table.--skip-extended-insertwrites one row per statement. Diffs become readable, restores become slow.--insert-ignoreand--replacewriteINSERT IGNOREorREPLACEfor loading into a table that already has rows.REPLACEoverwrites matching rows, so copy the table first.
Some rows: --where
mysqldump --no-create-info --skip-triggers --compact \
--where="created >= '2024-01-01'" shop ordersINSERT INTO `orders` VALUES (2,1,20.00,'2024-03-01'),(3,2,30.00,'2024-06-09'),(4,3,5.50,'2025-02-02');The condition is pasted into a SELECT, which has two consequences. A trailing limit works, so --where="1 LIMIT 2" gives you a two-row sample. And the same condition is applied to every table in the dump, so naming the whole database usually ends in an unknown column:
mysqldump --where="created > '2024-01-01'" shop > some.sqlmysqldump: Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM `customers` WHERE created > '2024-01-01'': Unknown column 'created' in 'where clause' (1054)Subqueries have a quirk of their own. A condition such as id IN (SELECT id FROM orders WHERE total > 15) failed in the default locking mode with Table 'orders' was not locked with LOCK TABLES (1100), even though orders was the table being dumped. The manual's page on LOCK TABLES explains it: a locked table can't be named twice in one query unless an alias has its own lock. Use a plain condition, or add --single-transaction, which takes no table locks. With it, the same subquery returned the two matching rows.
What a plain dump leaves out
Counting the objects in the file shows the default behaviour. Both runs are on the database above:
| Object | mysqldump shop | with --routines --events |
|---|---|---|
| Tables | 3 | 3 |
| View | 1 | 1 |
| Trigger | 1 | 1 |
| Procedure | 0 | 1 |
| Event | 0 | 1 |
That matches the manual page on dumping stored programs: triggers are on by default, routines and events are off.
There's a quieter trap with a limited backup user. According to the mysqldump reference, --routines needs the global SELECT privilege. With a user that had SELECT on the shop database only, the dump finished with exit code 0, printed nothing, and contained no procedure. So count the procedures once after you set up the account, logged in as that account:
mysqldump --routines shop | grep -c 'PROCEDURE `'1Each routine, trigger, event and view also carries a DEFINER, normally the account that created it. If that account doesn't exist on the target server, creating it first is simpler than editing the dump.
Does mysqldump lock tables?
Yes, by default. --opt is enabled unless you turn it off, and it includes --lock-tables: all tables of the database get a read lock for the whole dump. On a database with one table of a million short rows, an INSERT started during a plain dump waited 1.4 seconds, until the dump had finished. With --single-transaction the same INSERT took 0.03 seconds.
--single-transaction opens a REPEATABLE READ transaction and reads everything from that snapshot. Two limits are in the manual. Only InnoDB tables are consistent this way, so a MyISAM or MEMORY table may still change while it's being read. And no other session should run ALTER TABLE, CREATE TABLE, DROP TABLE, RENAME TABLE or TRUNCATE TABLE during the dump.
Don't confuse this with the LOCK TABLES ... WRITE lines inside the file. Those come from --add-locks, take effect at restore time, and are there in both modes.
If the backup user has no LOCK TABLES privilege, the plain form stops:
mysqldump: Got error: 1044: Access denied for user 'lab_dump_x'@'localhost' to database 'shop' when using LOCK TABLESAdding --single-transaction is usually the better answer than adding the privilege.
Compress on the way out
set -o pipefail
mysqldump --single-transaction --routines --events --no-tablespaces shop | gzip > shop.sql.gz
gunzip -c shop.sql.gz | tail -n 1-- Dump completed on 2026-11-25 2:40:25The pipefail line matters in bash scripts. Dumping a database that doesn't exist through gzip gave an overall exit code of 0 without it, and a small valid .gz with only a header inside. The final "Dump completed" comment is the cheap check that the file is whole.
Restoring from the compressed file:
mysql -e "CREATE DATABASE shop_copy"
gunzip -c shop.sql.gz | mysql shop_copy
mysql -e "CHECKSUM TABLE shop.orders, shop_copy.orders"+------------------+------------+
| Table | Checksum |
+------------------+------------+
| shop.orders | 2526316017 |
| shop_copy.orders | 2526316017 |
+------------------+------------+Equal checksums prove the rows made the trip. If the restore says ERROR 1046 (3D000): No database selected, the file was made without --databases and you have to name the target, as above. Files made with --databases contain CREATE DATABASE and USE, so they always go back to the original name. The manual spells out the difference in its page on dumping in SQL format.
If a big import dies halfway with a lost connection, the packet limits are covered in MySQL server has gone away.
The messages you meet between versions
PROCESS privilege. "Access denied; you need (at least one of) the PROCESS privilege(s) for this operation" when trying to dump tablespaces: the manual lists PROCESS as a requirement since 8.0.21, unless --no-tablespaces is given. In the run here the dump carried on and completed with exit code 0. The flag removes the message.
Unknown table 'COLUMN_STATISTICS'. The 8.0 program can write histogram statistics into the file, and it looks for them in information_schema.COLUMN_STATISTICS. Histograms are a feature of MySQL 8.0 servers, so a message that names this table points at an older or different server on the other end. Pass --column-statistics=0. The manual lists this option as off by default, but the 8.0.33 binary reports it as on, so check your own:
mysqldump --help | grep '^column-statistics'column-statistics TRUEGTIDs. On a server with gtid_mode=ON, the default --set-gtid-purged=AUTO adds a SET @@GLOBAL.gtid_purged statement. That's right for building a replica and wrong for copying one database to another server. Use --set-gtid-purged=OFF for copies.
MariaDB files. The mariadb-dump page says its dumps begin with a command that enables sandbox mode, and that versions of mysql or the MariaDB command line which don't support it throw an error. The same page says other ways of importing the data aren't affected. If you must use an old mysql, look at the first line of the file before blaming the rest of it.
Character sets and binary columns
mysqldump 8.0 uses utf8mb4 by default, and the file says so in a SET NAMES utf8mb4 line. Keep it. Two things still damage text. Rows that were inserted through a latin1 connection by a program sending UTF-8 are already double-encoded in the table, and the dump shows Zoë where you expect Zoë. And on Windows, PowerShell redirection writes UTF-16, which the server can't accept as a connection character set. The manual's fix is --result-file=shop.sql in place of >.
Binary columns are written raw by default, which makes the file awkward to open or to copy through other tools. --hex-blob writes them as hexadecimal:
mysqldump --hex-blob --no-create-info --skip-triggers --compact shop sessionsINSERT INTO `sessions` VALUES ('aa',0x00FF10),('bb',0xC3A9);A nightly file worth trusting
#!/bin/bash
set -euo pipefail
dir="backups" # an absolute path outside the web root
mkdir -p "$dir"
out="$dir/shop-$(date +%F).sql.gz"
mysqldump --single-transaction --routines --events --no-tablespaces --hex-blob shop | gzip > "$out.part"
gunzip -c "$out.part" | tail -n 1 | grep '^-- Dump completed' > /dev/null
mv "$out.part" "$out"
ls "$dir" | wc -l | tr -d ' '1For an import page that accepts only small files, cut the finished dump with the splitter and keep the original. The file only gets its final name if the dump, the compression and the last line are all in order. Anything that monitors the folder can then treat a missing file as a failed backup. The last line of the script is only there to show the result.
Same subject
MySQL EXPLAIN in practice: from a slow query to the right index
Put EXPLAIN in front of the query and read four columns: type, key, rows and Extra. They show whether an index is used, how well, and what is done after it.
MySQL server has gone away (2006) and lost connection (2013): the causes
MySQL error 2006 means the connection was closed before your query arrived: check idle timeout, packet size and server restarts, in that order, then reconnect.
MySQL error 1064: how to read the message and find the broken part
MySQL error 1064 quotes the statement from the first token the parser could not accept, so look at that word and the one just before it for the cause.
PHPSolvedThe fix first, then the reason