Back up and restore a MySQL database

Intermediate  Nov 20, 2026

Create a reliable backup with mysqldump, restore it, and set up a routine so you never lose data.

Back up and restore a MySQL database
The steps of this tutorial at a glance.
The steps of this tutorial at a glance.

Why

Servers fail, updates break things, and people delete the wrong rows. A backup you have tested is the only real protection.

Back up with mysqldump

mysqldump -u root -p --single-transaction --routines --triggers mydb > mydb_backup.sql
  • --single-transaction gives a consistent copy of InnoDB tables without locking them.
  • Compress large dumps: mysqldump ... mydb | gzip > mydb_backup.sql.gz

On WampServer the program is in the MySQL bin folder; run it from a command prompt opened in that folder.

How the code in this section fits into the page.
How the code in this section fits into the page.

Restore

mysql -u root -p mydb < mydb_backup.sql

Create the empty database first if it doesn’t exist:

CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Restore
Restore

Test your backup

Once in a while, restore it into a different database (mydb_test) and open the site against it. A backup that has never been restored is only a hope.

Make it a routine

  • Back up before every update or big change.
  • Automate it: a scheduled task (Windows Task Scheduler) or cron job on a server.
  • Keep several copies (daily for a week, weekly for a month) and store at least one away from the server.
  • Do not leave backups in a web-accessible folder. Anyone who guesses the address could download your data.
  • Back up uploaded files too, not just the database.
The main points of this section.
The main points of this section.

phpMyAdmin option

For small sites: Export → Custom → SQL, tick *Add DROP TABLE* if you want to replace existing tables, and save the file.

More tutorials