Ask Steves · Daily notes · VPS, 20% off · AI Profit Lab · SEO · Money Hub

26 October 2026 · One command a day

How do I back up a MySQL database from the command line?

mysqldump writes a whole MySQL database out to a plain text file of SQL statements. You can use that file to rebuild the database later. Run it before an upgrade, a migration or any change you might need to undo.

I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.

The command

sudo mysqldump -u root dbname > dbname.sql

sudo is there because MySQL on Ubuntu 24.04 lets the system root user log in as the MySQL root user without a password. mysqldump is the backup tool that comes with the MySQL client. -u root tells it which MySQL user to connect as. dbname is the name of the database you want to back up. > dbname.sql sends the output into a file in your current directory. Your own shell does that redirect, not sudo, so the file belongs to you.

What you will see

If it works, the command prints nothing. To check the file, look at its size and its first and last lines:

$ ls -lh dbname.sql
-rw-rw-r-- 1 user user 14M Oct 26 10:15 dbname.sql
$ head -n 5 dbname.sql
-- MySQL dump 10.13  Distrib 8.0.39, for Linux (x86_64)
--
-- Host: localhost    Database: dbname
-- ------------------------------------------------------
-- Server version	8.0.39-0ubuntu0.24.04.2
$ tail -n 1 dbname.sql
-- Dump completed on 2026-10-26 10:15:02

The header shows the MySQL version and the database that was dumped. The line that matters most is the last one. If the file ends with Dump completed, the backup finished. If that line is missing, the dump stopped part way, perhaps because the disk filled up or the connection dropped, and you should not trust the file. A file of only a few kilobytes for a database you know is large is another warning sign.

When to use it

Watch out for

By default, mysqldump locks each table while it reads it. On a busy site, writes can stall until the dump finishes. Most modern databases use the InnoDB engine, and for those you can add --single-transaction. That takes a consistent snapshot without blocking writes: sudo mysqldump -u root --single-transaction dbname > dbname.sql. Also keep in mind that the file holds every row in plain text, including user emails and password hashes. Store it somewhere only you can read, and not inside a public web folder.

Questions people ask

How do I restore a MySQL database from a .sql file?

Create an empty database with the same name if it does not exist yet. Then run sudo mysql -u root dbname < dbname.sql to load the file back in.

How do I back up every MySQL database at once?

Replace the database name with --all-databases, as in sudo mysqldump -u root --all-databases > all.sql. The resulting file also includes the statements that create each database.

On shared hosting you usually only get a backup button. On your own server you can dump, check and schedule backups yourself. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.

Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.

Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.

GET A VPS, 20% OFF →

Affiliate link.

Related server notes: How do I back up a Postgres database from the command line? · How do I restore a MySQL database from a .sql file on Linux? · How do I extract a tar.gz file on Linux? · All daily notes