MySQL - restore database from backup using Linux Command Line

23 points
Created by:

In this short article we would like to show how to make MySQL database baskup using Linux Terminal (command line).

Quick solution:

cat /path/to/backup.sql | mysql -u db_username -pdb_password db_name


cat /path/to/backup.sql | mysql -u db_username -pdb_password -h db_hostname db_name



  • do not use space beetween -p and db_password,
  • -h db_hostname is optional (lack means localhost connection).


Practical examples

Example 1:

cat ./2020.09.12_17.21_gallery.sql | mysql -u root -pSecretPassword -h gallery

Example 2:

cat '/path/to/backup/db/2020.09.12_17.21_gallery.sql' | mysql -u 'root' -p'SecretPa$$word' -h '' 'gallery'


Secure command

Calling command with password as parameter is not save. It makes the password visible on processes list and stays in command line history.

So, it is good to use --password option and type password when mysql command requests it, by:

cat /path/to/backup.sql | mysql -u db_username --password db_name


See also

  1. MySQL - database backup from Linux Command Line
  2. MySQL - database schema backup using Linux Command Line (without data)

Alternative titles

  1. MySQL - load database from backup SQL file
  2. MySQL - import database from backup SQL file
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL - Problems

MySQL - restore database from backup using Linux Command Line
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂