Categories
Uncategorized

Restore MySQL Database from the Command Line

In a previous article I described how to restore your website files from a backup using the LINUX command line, this time I’ll explain how to restore a database from backup using MySQL from the LINUX command line. The process is really quite simple and is achieved in just 2 main steps.

  1. Locate the backup archive file.
  2. Prepare the destination database
  3. Read the backup database file into MySQL

1. Locate the Backup File

  1. ssh host.server.com
    • (replace “host.server.com” with the address of the server where the backup file is located)
  2. cd ~/archive
    • (replace “~/archive” with the name of the folder containing the backup file)

2. Prepare the destination database

This step is only necessary if you are restoring the database to a server different to where it was originally backed up from, for instance if you are moving the database to a different hosting account.

  1. Create a database at the destination location using the same database name and database username defined in the backup
    (Different hosts may have different restrictions on naming databases and user names. Eg GoDaddy does not allow numbers in database names whereas Bluehost requires a autogenerated prefix which may include a number. If this is the case, you will not be able to use the same naming as the original database)

3. Import the database backup with MySQL

  1. mysql -u user_name -p database_name -h database_host_address < database_backup_filename
  • (replace “user_name” with the name of the database user that made the backup or the username defined in step 2)
  • (replace “database_name” with the name of the database that was backed up or the database name created in step 2)li>
  • (replace “database_host_address” with the ip address or the domain name of the server onto which the database will be restored to)
  • (replace “database_backup_filename” with the name of the database backup file e.g. db-backup.sql)

And there you have it, the database should be fully restored as per the original it was backed up from. If you have restored the database to a different server, you may need to use different parameters such as username and of course the database host address. If you have also moved config files from another server you must update these to reflect any changes you made to database parameters and server names etc.

Leave a Reply

Your email address will not be published. Required fields are marked *