27 October 2026 · One command a day
How do I restore a MySQL database from a .sql file on Linux?
This command loads a .sql dump file back into a MySQL database. It replays every statement in the file, so the tables and rows come back as they were when the dump was made. You use it after a mistake, when moving a site to a new server, or when testing a backup.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
sudo mysql -u root dbname < dbname.sqlsudo is needed because MySQL on Ubuntu 24.04 lets the root account in through the system root user, not a password. mysql is the command-line client. -u root logs in as the MySQL root user. dbname is the database the data goes into, and it must already exist. If it does not, create it first with sudo mysql -e 'CREATE DATABASE dbname'. The < tells the shell to feed the contents of dbname.sql into the client, as if you had typed it all yourself. If you have not made a dump yet, see how to back up a MySQL database from the command line.
What you will see
When the restore works, you see nothing. The prompt just comes back. To check, list the tables:
$ sudo mysql -u root dbname < dbname.sql
$ sudo mysql -e 'SHOW TABLES' dbname
+------------------+
| Tables_in_dbname |
+------------------+
| orders |
| sessions |
| users |
+------------------+No output means success. If something goes wrong, MySQL prints an error and stops. For example, ERROR 1049 (42000): Unknown database 'dbname' means you need to create the database first. An error with a line number, such as ERROR 1064 (42000) at line 412, points to the line in the .sql file where it failed. Statements before that line have already run.
When to use it
- A plugin update or bad query has damaged a site's data and you need to go back to last night's dump.
- You are moving a site to a new VPS and have copied the .sql file across.
- You want to prove a backup actually works by restoring it into a spare database such as
dbname_test.
Watch out for
A restore overwrites what is there. Dumps made by mysqldump include DROP TABLE IF EXISTS before each table, so any table in the dump is wiped and replaced. Changes made since the dump, such as new orders or sign-ups, are lost. Before restoring over a live database, take a fresh dump of its current state so you can undo the restore if you picked the wrong file.
Questions people ask
How do I restore a .sql.gz file?
Decompress it on the fly and pipe it in with gunzip < dbname.sql.gz | sudo mysql -u root dbname. You do not need to unpack the file to disk first.
How long does a restore take?
A small site's database restores in a few seconds. Dumps of several gigabytes can take many minutes, and the command shows no progress while it runs, so leave it alone until the prompt returns.
Restoring a database straight from the command line is the kind of thing you can only do on a server you control. 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.
Affiliate link.