← Back to Chapters

MySQL mysqldump Tool

?️ MySQL mysqldump Tool

? Quick Overview

mysqldump is a command-line utility provided by MySQL to create logical backups of databases. Instead of copying raw data files, it exports the database schema and data as SQL statements that can be executed later to recreate the database.

It is commonly used for backups, migrations between servers, version upgrades, and disaster recovery scenarios.

? Key Concepts

  • Produces logical backups using SQL text
  • Can back up a single database, multiple databases, or all databases
  • Supports table-level backups
  • Allows structure-only or data-only exports
  • Portable across MySQL versions and systems

? Syntax / Theory

The mysqldump utility connects to a running MySQL server using authentication credentials. It reads metadata and data from system tables and generates SQL commands such as CREATE DATABASE, CREATE TABLE, and INSERT.

Because the output is plain SQL, it can be edited, version-controlled, or partially restored if required.

? View Code Example
-- Basic mysqldump syntax
mysqldump -u username -p database_name > backup.sql

? Code Examples

? Backup a Single Database

This command creates a full backup of one database including both schema and data.

? View Code Example
-- Backup a single MySQL database
mysqldump -u root -p mydatabase > mydatabase_backup.sql

? Backup All Databases

Useful for full server backups before upgrades or migrations.

? View Code Example
-- Backup all databases from the server
mysqldump -u root -p --all-databases > alldb_backup.sql

? Backup Specific Tables

This option is helpful when only a subset of tables needs to be backed up.

? View Code Example
-- Backup selected tables from a database
mysqldump -u root -p mydatabase users orders > tables_backup.sql

? Structure-Only Backup

Creates a backup without table data, useful for schema replication.

? View Code Example
-- Backup only table structure (no data)
mysqldump -u root -p --no-data mydatabase > structure_only.sql

? Data-Only Backup

Exports only table data without CREATE TABLE statements.

? View Code Example
-- Backup only table data
mysqldump -u root -p --no-create-info mydatabase > data_only.sql

? Live Output / Explanation

What gets generated?

The resulting SQL file typically contains:

  • CREATE DATABASE statements
  • USE database_name
  • CREATE TABLE definitions
  • INSERT INTO statements for data

When executed using the MySQL client, these commands recreate the database exactly as it existed at the time of backup.

✅ Tips & Best Practices

  • Always verify backups by restoring them in a test environment
  • Use timestamps in backup filenames for easy tracking
  • Compress large backups to save storage space
  • Automate backups using cron or scheduled tasks
  • Secure backup files since they may contain sensitive data

? Try It Yourself

  • Create a structure-only backup and restore it to a new database
  • Compare data-only and full backups
  • Automate mysqldump using a scheduled job
  • Restore a backup using the MySQL client