Support / KnowledgeBase

 
Search the KnowledgeBase Search

How can I Backup and Restore a MySQL database?

  • Applies to: All Service Types

  • Difficulty: Easy

  • Time needed: 5 minutes

  • Tools needed: ssh

 

Details

METHOD 1 - PHPMyAdmin

You can use PHPMyAdmin to backup/restore your database if it is relatively small (~10MBs)
If your database is larger than that please skip to METHOD 2 - command line

BACKUP:

  1. Select the database you'd like to back up from the column on the left
  2. Click on "Export" from the top set of tabs
  3. Select the tables from the list that you would like to backup. If you want to backup the entire database hit "Select All"
  4. Select "Structure and data" from the bullet list
  5. Selection boxes:
    • Check the "Add 'drop table'" box if you are moving the database to a new location and don't want to merge the old table with an existing one
    • Click the "Save as file" box
    • Use the "Save as file zipped" if you want to compress the backup before downloading it from the server
  6. Click the "Go" button, when prompted save the file to your local computer

RESTORE:

  1. From the column on the left select the database that you want to restore to. If one doesn't exist you must first create it.
  2. Click on "Import" from the top set of tabs
  3. Click on the "Browse" button next to "Or Location of the textfile:" near the bottom
  4. Browse to the local backup and click "Open"
    • If you have the local backup in a non-text file format, e.g. you selected "Save as file: zipped" when you backed up the database, you'll have to unzip the file on your local computer before you can select it during this step
  5. Click the "Go" button
    • You should get a message like this:
      Your SQL-query has been executed successfully :
      The content of your file has been inserted. (X Instructions)
      • If you receive an error please try Method 2 below.

METHOD 2 - command line

This method works regardless of the size of your database. You must have SSH access to your server. On (ss) plans you can request that we enable this through the support system, on (dv) plans you must enable Shell Access through Plesk.


BACKUP:

  1. Log into your server via SSH and cd into a directory where your user has write access such as the following examples:.
    (ss) /var/www/html/
    (gs) /home/#####/domains/mt-example.com/html/ (replace ##### with your site number)
    (dv) /var/www/vhosts/mt-example.com/httpdocs/
  2. Enter the following command:
    mysqldump --add-drop-table -u Username -p dbname > dbname.sql
    • omit the '--add-drop-table' argument if you'll want to merge this backup with an existing database upon restore
    • Where username is replaced by the MySQL username. On (ss) plans this user is the same as the administrative FTP user. On (dv) plans this user can be found by logging into Plesk, clicking on the domain and going to databases and clicking on the database to be backed up.
    • Replace db##### with the name of the database to be backed up.
    • Replace dbname.sql with what you'd like to name the backup.
    • On (gs) Grid-Service Systems, you must also include a ' -h internal-db.s#####.gridserver.com ' flag in your command, where ##### are replaced by your site number.
  3. Enter your MySQL password at the prompt. If you don't know it you can reset it in your webcontrol or Plesk admin panel
    • If you get an error that looks like this:
      ERROR 1045: Access denied for user: 'Username@localhost' (Using password: YES)
      you have entered an incorrect password. Please retype it carefully, or reset your password via the WebControl Panel or Plesk Control Panel.
    • On (ss) plans if you have reset the password in the webcontrol panel and are sure you've entered it correctly on the command line this might be a permissions problem. Try renaming your database to something else in the webcontrol panel and then back to the original, this often fixes permissions problems.
  4. Use FTP to download the file to your backup location, probably your local computer.
    • Don't forget to delete the backup from your public html directory after you've found a safe place for it. You don't want to leave your backup lying around where anyone with a web browser can download a copy.

RESTORE:

  1. Use FTP to upload the file to your server, your public html directory will work for now
    • Don't forget to delete the backup from your public html directory after you've done the database restoral. You don't want to leave your backup lying around where anyone with a web browser can download a copy.
  2. Log into your server via SSH and cd to the directory where you've uploaded the file. If you uploaded the backup into your public html directory you would see the file in one of the following example directories:
    • (ss) /var/www/html/
    • (gs) /home/#####/domains/mt-example.com/html/ (replace ##### with your site number
    • (dv) /var/www/vhosts/my_domain.net/httpdocs/
  3. Enter the following command:

    mysql -u Username -p dbname < dbname.sql
    
    • Where username is replaced by the MySQL username. On (ss) plans this user is the same as the administrative FTP user. On (dv) plans this user can be found by logging into Plesk, clicking on the domain and going to databases and clicking on the database to be restored.
    • Replace db##### with the name of the database to be restored.
    • Replace dbname.sql with the name of the backup.
    • On (gs) Grid-Service Systems, you must also include a ' -h internal-db.s#####.gridserver.com ' flag in your command, where ##### are replaced by your site number.
    If you have a zipped backup of your database you can use this line instead:

    gunzip < dbname.gz | mysql -u username -p dbname
    • Where username is replaced by the MySQL username. On (ss) plans this user is the same as the administrative FTP user. On (dv) plans this user can be found by logging into Plesk, clicking on the domain and going to databases and clicking on the database to be restored.
    • Replace db##### with the name of the database to be restored.
    • Replace dbname.gz with the name of the backup.
    • On (gs) Grid-Service Systems, you must also include a -h internal-db.s#####.gridserver.com flag in your command, where ##### are replaced by your site number.
  4. Enter your MySQL password at the prompt. If you don't know it you can reset it in your webcontrol or Plesk admin panel
    • If you get an error that looks like this:
      ERROR 1045: Access denied for user: 'Username@localhost' (Using password: YES)
      you have entered an incorrect password. Please retype it carefully, or reset your password via the WebControl Panel or Plesk Control Panel.

Revisions:

12-23-2008: More Cosmetic changes.
06-03-2008: Cosmetic changes.

User Comments

No visitor comments posted. Post a comment

Fields marked with an asterisk(*) are required. Comment on this article

Fill out the form below if you would like to comment on this article.
 
 
 

(code is not case-sensitive)
 
Submit
 
 

Continue