How to Backup and Restore MySQL Databases

It is a good idea to backup your MySQL data occasionally as a precautionary measure. It is also a good idea to create a backup before making any major changes, incase something goes wrong and you need to revert to the unmodified version. Database backups can also be used to transfer your database from one server to another if you change web hosts.

From a command prompt, you can backup your entire database using this line:

mysqldump -u user_name -p your_password database_name > File_name.sql

Example:
Let’s assume that:
Username: bobbyjoe
Password: happy234
Database Name: BobsData

mysqldump -u bobbyjoe -p happy234 BobsData > BobBackup.sql This will backup the database to a file called BobBackup.sql

If you are moving your data to a new server, or you have removed the old database completely you can restore it using the code below. This will only work if the database does not already exist:

mysql - u user_name -p your_password database_name < file_name.sql
Or using our example from the previous page:

mysql - u bobbyjoe -p happy234 BobsData < BobBackup.sql
If your database already exists and you are just restoring it, try this line instead:

mysqlimport -u user_name -p your_password database_name file_name.sql
Or using our example again:

mysqlimport -u bobbyjoe -p happy234 BobsData BobBackup.sql

Backup Database From the phpMyAdmin

image

  1. Login to phpMyAdmin
  2. Click on your database name
  3. Click on the tab labeled EXPORT
  4. Select all tables you want to backup (usually all)
  5. Default settings usually work, just make sure SQL is checked
  6. Check the SAVE FILE AS box
  7. Hit GO

Restore Database From the phpMyAdmin

image

  1. Login to phpMyAdmin
  2. Click on the tab labeled SQL
  3. Unclick the ‘show query here again’ box
  4. Choose your backup file
  5. Hit GO