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 tech industry for major developments, new product launches, AI breakthroughs, video game releases and other newsworthy events. Editors assign relevant stories to staff writers or freelance contributors 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.

Get the biggest tech headlines of the day delivered to your inbox

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.

    Tech News

    Explore the latest in tech with our Tech News. We cut through the noise for concise, relevant updates, keeping you informed about the rapidly evolving tech landscape with curated content that separates signal from noise.

    In-Depth Tech Stories

    Explore tech impact in In-Depth Stories. Narrative data journalism offers comprehensive analyses, revealing stories behind data. Understand industry trends for a deeper perspective on tech's intricate relationships with society.

    Expert Reviews

    Empower decisions with Expert Reviews, merging industry expertise and insightful analysis. Delve into tech intricacies, get the best deals, and stay ahead with our trustworthy guide to navigating the ever-changing tech market.