Home How To: Backup and Restore Your MySQL Database

How To: Backup and Restore Your MySQL Database

Backing up your database is absolutely vital if the data is of any importance. This will save you in the event of an accidental deletion, either due to a slip of the finger or a major failure in your code.

If you’re familiar with phpMyAdmin, then you may be able to export and import your database from there. But this approach doesn’t work too well if you have a large database. For large databases, you’re better off using the command line.

Backup Your MySQL Database

Log in to the server you wish to work with. The command you’ll need to run is as follows:

mysqldump -u username -p database > file

Replace the username, database and file parameters with the values that match your database. For instance:

mysqldump -u root -p mydatabase > /home/backups/mybackup.sql

If all goes well, you’ll be prompted for the password, and then the backup file will be created.

Note that in the above example, the /home/backups folder must exist. If it doesn’t, you’ll need to create it first.

Restore Your MySQL Database

Be careful with this, because any existing data will be overwritten. You’ll need to do the following:

mysql -u username -p database < file

If we wanted to restore from the file we just created, here’s what we’d do:

mysql -u root -p mydatabase < /home/backups/mybackup.sql

Once again, just enter the password, and the database will be restored from the file you supplied.

Photo by Tim Morgan

About ReadWrite’s Editorial Process

The ReadWrite Editorial policy involves closely monitoring the gambling and blockchain industries for major developments, new product and brand launches, game releases and other newsworthy events. Editors assign relevant stories to in-house staff writers with expertise in each particular topic area. Before publication, articles go through a rigorous round of editing for accuracy, clarity, and to ensure adherence to ReadWrite's style guidelines.