How can I Backup and Restore a MySQL database?

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) )

METHOD 2 – command line

This method works regardless of the size of your database. You must have SSH access to your server.

BACKUP:

  1. Log into your server via SSH and cd into a directory where your user has write access
  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
    • username is replaced by the MySQL username
    • Replace dbname with the name of the database to be backed up.
    • Replace dbname.sql with what you’d like to name the backup.
  3. Enter your MySQL password at the prompt.
    • 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
  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.
  3. Enter the following command:

    mysql -u Username -p dbname < dbname.sql

    • username is replaced by the MySQL username.
    • Replace dbname with the name of the database to be restored.
    • Replace dbname.sql with the name of the backup.

    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.
    • Replace dbname with the name of the database to be restored.
    • Replace dbname.gz with the name of the backup.
  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.
Bookmark and Share
One Comments

How can I Backup and Restore a MySQL database?…

How can I Backup and Restore a MySQL database?…

Leave a Reply

You must be logged in to post a comment.