How to Backup and Restore MySQL Databases With mysqldump And phpMyAdmin

restore mysql databases with mysqldump

Whether you’re a small eCommerce business or a giant corporation, loss of data is the worst thing that can happen to your business.

Even a temporary glitch in accessing the database can result in operational delays and revenue loss. You can imagine the impact of a major loss of data on the viability of your business. 

That’s why you will see database backup on every website security and maintenance checklist. 

This short tutorial will help you backup and restore your MySQL database. We’ll discuss two simple database backup methods. First, we’ll discuss mysqldump, a built-in MySQL utility. Next, we’ll show you how to backup and restore your MySQL database with phpMyAdmin, a popular database management tool. 

Let’s start with the prerequisites of the backup and restore process.

Before you begin, make sure you have the following ready:

  • A system running a Linux-based operating system
  • A user account with root or sudo privileges
  • Access to an active MySQL database on your machine
  • Access to mysqldump (typically comes pre-installed with MySQL) and phpMyAdmin

Backup and Restore a MySQL Database

We’ll now discuss two easy methods of backing up and restoring a MySQL database. 

Method #1: Backup and Restore a MySQL Database with mysqldump

mysqldump is a command-line utility for creating a full backup of your entire database, effectively taking a snapshot of your data.

With the mysqldump utility, you can initiate a backup from the command prompt, which is perfect for regular scheduling or moving data to a remote server. 

Should you ever need to restore your MySQL database, this utility simplifies the process by allowing you to revert your database to its previous state using the available database backup file.

How to Backup a MySQL Database With mysqldump

The mysqldump client utility can “dump” database tables and the SQL statements required to rebuild the database.

The syntax of the command is:

# sudo mysqldump -u [user] -p [database_name] > [filename].sql

 

mysqldump

Here,

  • [user] is your username. Enter the password when asked.
  • [database_name] represents the directory location and the file name for the database.
  • The > symbol designates where the output will be directed.
  • [filename] represents the desired file path and name of the database archive file.

 

The most common usage of the mysqldump utility is to take a full backup of the database.  The syntax of this command is:

# mysqldump –all-databases –single-transaction –quick –lock-tables=false > full-backup-$(date +%F).sql -u root -p

mysqldump

If you have several databases, you can back them up in a single archive file with the following command syntax:

# sudo mysqldump -u [user] -p [database_1] [database_2] [database_etc] > [filename].sql

sudo mysqldump

How to Restore a MySQL Database with mysqldump

You can easily restore a damaged MySQL database with mysqldump if you have a backup archive. This is a two-step process where you first create a new MySQL database and then restore the data from the archive. 

In the first step, create a new MySQL database on the target machine. Remember to name it the same as the original database. The mysqldump utility contains all statements for restoring the data in this blank database. 

When the new database is active, you can proceed to the second step. 

Start the database restoration with the following command:

# mysql -u [user] -p [database_name] < [filename].sql

database restoration command # mysql -u [user] -p [database_name] < [filename].sql

Remember to replace the database name and username with the actual values in your case.

On the host system, [database_name] might reside in a root directory, eliminating the need for a full path specification. However, it’s crucial to provide the precise path for the dump file you intend to restore because the file path may include the server name (if applicable).

Method #2: Backup and Restore a MySQL Database with phpMyAdmin

phpMyAdmin is a popular open source MySQL database management tool. You can use it to backup and restore your MySQL database. You can use the export feature to create a database backup, and the import feature to carry out a restore.

How to Backup a MySQL Database with phpMyAdmin

Launch phpMyAdmin in your web browser. 

From the left sidebar, select the database you wish to backup. The main right-hand pane will display the structure of the selected database, and all related objects will be highlighted in the left sidebar.

In the top navigation bar, click the Export tab.You can see a section titled Export Method. Opt for Quick to download the entire database, or Custom to pick specific tables and settings.

Backup mysql database with phpadmin

Keep the Format as SQL, which is the default option, unless you need a different format.

Hit the Go button. 

If you select the Quick method, the browser will prompt you to save the database archive file in the local downloads directory. Once the download finishes, move this file to a secure location for safekeeping.

How to Restore a MySQL Database With phpMyAdmin

Before you go ahead and restore the database data, you should clean the database and remove all existing data. This simple step will prevent operational issues (such as duplicate data items and tables) that can cause errors and conflicts later on.

Before proceeding with a restoration, it’s crucial to remove all previous data to prevent issues such as duplication of tables which can lead to errors and conflicts. 

For this step, access phpMyAdmin, and in the left navigation panel, select the database you intend to restore. At the bottom, click the Check All box. Next,  go to the With selected: drop-down menu, and choose Drop.

restore mysqldatabase with phpmyadmin

Select Yes in the confirmation dialog box will appear. Confirm by clicking ‘Yes’.

This step will eliminate all existing data in the database.

Now that you have a clean slate, you can go ahead and restore the database via phpMyAdmin. You can now use the Import functionality.

Navigate to the top menu and select Import.

In the File to import section, find the prompt Browse your computer. Tap Choose File. 

Locate the backup file you wish to restore. Keep the default settings unless your backup requires different options. Next, click the Go button to start the restore process. 

Also Read: 3 Simple Methods to Delete Duplicate Rows in MySQL Database

Conclusion

We covered two methods of backing up and restoring MySQL databases. How to backup and restore a MySQL database is a critical skill that every MySQL database admin should have in their skill set. 

mysqldump is a CLI utility that you can use if you prefer running commands in the terminal. The mysqldump utility with -u root is perfect for creating logical backups, regardless of database size.

For users who prefer a GUI, we recommend phpMyAdmin. The export feature is relatively straightforward. 

At RedSwitches, we encourage our customers to backup their databases to avoid any interruptions in business operations. We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. How do I back up a MySQL database on a Linux server?

To back up a MySQL database on a Linux server, you can use the mysqldump utility. Run the command mysqldump -u [username] -p [database_name] > backup_dump_file.sql in your terminal. This will prompt for the MySQL user’s password and create a backup dump file for the specified database.

Q. What is an incremental backup, and how do I perform it on MySQL?

Incremental backups involve backing up only the changes made to the database since the last backup, saving time and space. On MySQL, this can be achieved using binary logs or tools like Percona XtraBackup. To set this up, you need to enable binary logging on your MySQL server and perform backups using the –incremental option in your backup tool.

Q. How can I restore a single database from a backup dump file?

To restore a single database from a backup dump file, use the mysql command like so: mysql -u [username] -p [database_name] < backup_dump_file.sql. This assumes that you’ve already created the new database on the MySQL server and you have the necessary access to the database.

Q. Can I back up all databases into a single dump file?

Yes, you can back up all databases into a single dump file by using the –all-databases option with mysqldump. The command would be mysqldump -u [username] -p –all-databases > all_databases_dump.sql.

Q. What does the –opt flag do when backing up a database?

The –opt flag is a shortcut for several options that optimize the backup process, making it faster and resulting in a smaller backup dump file. It includes options like adding DROP TABLE and DISABLE KEYS statements, which help in faster restoration.

Q. How do I ensure I have the proper access to backup a database?

To backup a database, your MySQL user account must have the SELECT, SHOW VIEW, LOCK TABLES, and possibly the RELOAD privileges. You can grant these privileges by logging into your MySQL server as the root user and issuing the appropriate GRANT commands for the user.

Q. What does it mean to backup a database?

When you backup a database, you are creating a copy of all the data and structure within the database. This copy can be used to restore the database in case of data loss or corruption.

Q. What is a dump file?

A dump file is a text file that contains SQL statements to recreate the tables, data, and structure of a database. It can be used to restore the database.

Q. How can I back up a single database using mysqldump?

To back up a single database using mysqldump, you can use the following command: mysqldump -u [username] -p [database_name] > [dump_file.sql]

Q. How can I restore a MySQL database from a backup archive?

To restore a MySQL database from a backup archive, you can use the mysql command with the < operator. For example, mysql -u [username] -p [database_name] < [dump_file.sql]

Q. Can I restore a specific table from a database backup?

Yes, you can restore a specific table from a database backup by using the –tables option with mysqldump. For example, mysqldump -u [username] -p [database_name] –tables [table_name] > [dump_file.sql]

Q. How do I restore a MySQL database using PHPMyAdmin?

To restore a MySQL database using PHPMyAdmin, go to the Import tab, browse for the dump file, and click the Go button.

Q. Can I restore a MySQL database from the command line?

Yes, you can restore a MySQL database from the command line using the mysql command. Just specify the database name and the dump file you want to restore.

Q. How do I restore a MySQL database using MySQL Workbench?

To restore a MySQL database using MySQL Workbench, open the SQL Editor, and execute the SQL statements from the dump file.

Prem

Prem Patil is a full-time Linux System Administrator at RedSwitches. He is an engineer who has customer satisfaction as his priority. He provides solutions for clients via tickets and calls. He loves to read books and write poetry. Reach out to him at [email protected].

Related articles

Latest articles