MySQL database backups are made with the mysqldump utility: it turns the database into a text-based SQL file. To restore the database, this file is fed into the mysql client (mysql < dump.sql). A single table is exported the same way as the whole database, just by adding the table name after the database name. Below we'll walk through all three tasks step by step on Ubuntu and MySQL 8.0; the commands are the same for MariaDB, with differences noted separately.

What a SQL dump is and when it's the right choice

A SQL dump is a text file containing CREATE TABLE and INSERT statements that, when executed, let the server rebuild the database with the same tables and data.

This type of backup is called a logical backup. It differs from copying the raw data files from the MySQL directory: a dump doesn't depend on the internal storage format, so it can be opened in a text editor, moved to another server, and even to a different MySQL version. The downside is just as clear: on very large databases (tens of gigabytes), creating and restoring a dump takes a long time, and physical backups are a better fit in that case. For websites, online stores, and most VPS-based projects, a dump is more than enough.

Preparation: access and privileges

Before you start, check a few things:

  • Server access. Commands are run in the VPS terminal, usually over SSH. If you haven't connected yet, start with the article What Is SSH.

  • MySQL user. For a dump you need a user with SELECT privileges on the database; --routines and --events require additional privileges. If you don't have a database or user yet, see How to Create a MySQL Database on Your Server.

  • Keep the password out of the command line. Writing -pPASSWORD is unsafe: the password ends up in your command history and is visible in the process list. Use password prompting (-p with no value) or a ~/.my.cnf file instead:

[client]
user=backup_user
password=REPLACE_WITH_YOUR_PASSWORD

After creating the file, lock it down: chmod 600 ~/.my.cnf. In the examples below, the password isn't shown — the client will either prompt for it or pick it up from this file.

How to back up a database

The basic command looks like this (replace shop_db and backup_user with your own names):

mysqldump -u backup_user -p shop_db > shop_db.sql

Note: the > symbol redirects the output to a file. If no path is specified, the file will appear in the current directory.

Compression and dating the filename

Since a dump is plain text, it compresses well — often 5–10x. Add the date to the filename so backups don't overwrite each other:

mysqldump -u backup_user -p shop_db | gzip > shop_db_$(date +%F).sql.gz
mysqldump -u backup_user -p \
  --single-transaction \
  --routines --triggers --events \
  --default-character-set=utf8mb4 \
  shop_db | gzip > shop_db_$(date +%F).sql.gz

Where to store the backup

A backup file sitting on the same server as the database won't help you in a disk failure or a breach. Download a copy to another machine (scp, rsync) or send it to external storage: another VPS, object storage, or an FTP server.

How to restore a database from a SQL file

Important: restoring a dump overwrites tables with the same names. If the database already holds data you want to keep, back it up first.

Restoring into a new database (the safest option):

Open the MySQL console:

mysql -u root -p.

Create a database with the right character set:

CREATE DATABASE shop_db_restored CHARACTER SET utf8mb4;
EXIT;

Load the dump:

mysql -u backup_user -p shop_db_restored < shop_db.sql

For a compressed file, unpack it on the fly:

gunzip < shop_db_2026-12-03.sql.gz | mysql -u backup_user -p shop_db_restored

Restoring into an existing database uses the same command, just with that database's name; tables from the dump will replace any with matching names. If the dump was made with the --databases flag, you don't need to specify a database name in the command — it will be created automatically.

There's also a second method: using the source command from the MySQL console.

USE shop_db_restored;
SOURCE /home/user/shop_db.sql;

Use the full path to the file. This method is convenient when you're already in the console, but for compressed files and scripts, the gunzip | mysql combination is simpler.

How to export a single table (or several)

Table names are listed after the database name, separated by spaces:

# One table
mysqldump -u backup_user -p shop_db orders > orders.sql
# Several tables
mysqldump -u backup_user -p shop_db orders customers > orders_customers.sql

When you only need part of the data, these additional options come in handy:

Task

Command

Table structure only

mysqldump -u backup_user -p --no-data shop_db orders > orders_structure.sql

Data only, no CREATE TABLE

mysqldump -u backup_user -p --no-create-info shop_db orders > orders_data.sql

Rows matching a condition

mysqldump -u backup_user -p --no-tablespaces --where="created_at >= '2026-01-01'" shop_db orders > orders_2026.sql

Compressed table export

mysqldump -u backup_user -p shop_db orders | gzip > orders.sql.gz

Always wrap the --where condition in quotes, otherwise the shell will split it into parts. A file like this is imported the same way as a regular dump: mysql -u backup_user -p shop_db < orders.sql.

Common errors and how to fix them

Error

Cause

Solution

Access denied for user (1045)

Wrong password, or the user lacks privileges on the database

Check the login and password, and verify privileges with SHOW GRANTS FOR 'backup_user'@'localhost';

Unknown database (1049)

No database with that name exists

Create the database with CREATE DATABASE, or check the name with SHOW DATABASES;

MySQL server has gone away, Got a packet bigger than 'max_allowed_packet'

A row in the dump is too large

Increase max_allowed_packet in the server config, for example to 256M, and restart MySQL; you can also set --max-allowed-packet during export

Garbled characters instead of proper text, Incorrect string value error

The dump's character set doesn't match the database's

Export with --default-character-set=utf8mb4, and create the database with CHARACTER SET utf8mb4

Unknown collation: 'utf8mb4_0900_ai_ci' (1273)

A dump from MySQL 8.0 is being loaded into MariaDB or an older MySQL version

Replace the collation in the file with one suitable for the target version, for example utf8mb4_general_ci

Import breaks off on a large file

The SSH session closed, or the disk ran out of space

Run the import inside screen or tmux, and check free space with df -h

How to verify that a backup actually works

A backup that has never been restored is just an assumption. Verify it:

  • File size. An empty or suspiciously small file (a few kilobytes for a large database) signals a failure. Check the end of the dump: a complete dump ends with the comment -- Dump completed on ....

  • Test restore. Deploy the copy into a separate database (shop_db_test) and confirm everything loads without errors.

  • Table count. Compare: SHOW TABLES; should return the same list in the production and test databases.

  • Spot-check the data. Compare row counts in key tables: SELECT COUNT(*) FROM orders;.

Conclusion

Three commands cover three tasks: mysqldump creates a backup, mysql < dump.sql restores the database, and adding a table name after the database name exports just that table. But a backup is only valuable once you've confirmed you can actually restore from it. Keep copies off the main server and practice restoring on a separate machine — an inexpensive VPS is perfect for these tests. You can find a suitable plan on the Leneex VPS pricing page.

If you prefer a graphical interface, check out how to work with a database through phpMyAdmin, and for panel-level website backups, see our general article on backups.

FAQ

How is mysqldump different from copying the data directory?

mysqldump creates a logical copy in the form of SQL text, which is portable across versions and servers. Copying files from the data directory is a physical backup: faster on large databases, but only reliable with the server stopped or with special tools, and it's tied to the MySQL version.

Can you create a dump without locking tables?

Yes, for InnoDB tables: use --single-transaction. It reads data from a single consistent snapshot while the site keeps running. For MyISAM, this mode doesn't guarantee consistency.

How do you automate backups?

Put the mysqldump command in a script and schedule it with Cron; pull the password from ~/.my.cnf. See the article How to Create a Cron Job for details.

How do you restore a single table from a full dump?

You can extract the relevant block for that table from the file, for example with the command sed -n '/^-- Table structure for table orders/,/^-- Table structure for table /p' dump.sql > orders_only.sql`, and load it the usual way. It's simpler and more reliable to create separate dumps of important tables in advance. Before loading, test the result on a separate database.

How do you migrate a database to another server?

Create a dump, copy the file to the new server (scp dump.sql user@new-server:/tmp/), create the database and user there, then load the dump with the mysql command. Before migrating, make sure the MySQL versions are compatible.