Aral Code

Advanced MySQL Lesson 12 of 13

MySQL Backup and Restore — mysqldump, Export and Import

Back up MySQL databases with mysqldump and phpMyAdmin export, restore them with import, move a database to another computer or a web host, and import CSV data.

Hard drives fail, laptops get stolen, and someone eventually runs DELETE without a WHERE. A backup is the only real protection. MySQL makes backups easy: they're just text files full of SQL.

Backup with mysqldump

mysqldump -u root -p school > school-backup-2026-12-02.sql

This creates a .sql file containing the CREATE TABLE and INSERT statements needed to rebuild the whole database. In XAMPP on Windows, run it from C:\xampp\mysql\bin.

A peek inside the file:

CREATE TABLE `students` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(80) NOT NULL,
  ...
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `students` VALUES (1,'Ana Reyes',...),(2,'Ben Santos',...);

Useful options

CommandBacks up
mysqldump -u root -p school students > students.sqlOne table
mysqldump -u root -p --databases school store > two.sqlSeveral databases
mysqldump -u root -p --no-data school > structure.sqlStructure only, no rows
mysqldump -u root -p --all-databases > everything.sqlEverything on the server

Restore

mysql -u root -p -e "CREATE DATABASE school_restored CHARACTER SET utf8mb4"
mysql -u root -p school_restored < school-backup-2026-12-02.sql

With phpMyAdmin

  • Backup: select the database → Export → Quick → Format: SQL → Go. A .sql file downloads.
  • Restore: create or select a database → Import → choose the .sql file → Go.

Moving your project to another computer or host

  1. Export the database to a .sql file.
  2. Copy your PHP files and the .sql file to the new place (flash drive, Google Drive, or upload).
  3. On the new server, create an empty database and import the file.
  4. Update the database name, user and password in your app's connection file.

This is exactly how capstone projects are moved from a student's laptop to the panel's computer, or to a web host.

Importing a CSV file

Have a class list in Excel? Save it as CSV, then load it into a table:

LOAD DATA LOCAL INFILE 'C:/data/students.csv'
INTO TABLE students
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(lrn, first_name, last_name, grade_level);

IGNORE 1 LINES skips the header row. phpMyAdmin's Import tab can also read CSV files with a few clicks.

A backup routine

  • Automate: schedule mysqldump daily (Task Scheduler on Windows, cron on Linux).
  • Keep several copies: e.g., the last 7 daily backups.
  • Store them elsewhere: a backup on the same disk dies with that disk. Copy it to cloud storage or another drive.
  • Test restoring: a backup you've never restored might not work when you need it.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does mysqldump produce?
2. How do you restore school.sql into a database named school?
3. What does --no-data back up?
4. Why store backups on a different device?

By Sir Reyel · Last updated 2026-12-04