Data is valuable to people and organizations. Imagine losing a company database with customer or payroll information. Backing up databases is vital as a safety net from possible data loss—and that’s not up for debate. Show
Table of Contents
An excellent way to backup MySQL databases and tables is the Ready? Read on and become a MySQL database backup pro!
PrerequisitesThis tutorial will be a hands-on demonstration. If you’d like to follow along, you must have a Linux computer with MySQL already installed. This tutorial uses an Ubuntu 20.04 computer with MySQL 8.0.29. Follow the How to Install MySQL on Ubuntu tutorial first if you haven’t installed MySQL yet. This tutorial also assumes that you have already opened a terminal session to the Linux computer. Creating the Test DatabaseBefore you can back up a database, you need to have at least one! In this section, you’ll create two sample databases with tables and data for testing.
1. First, connect to the mysql command shell as root. Type the password when prompted, and press Enter. Connect to MySQL as the root user.2. Next, copy the below code, paste them to the mysql prompt, and press Enter. Refer to the inline comments to understand what each statement does. This code will create two databases named menagerie1 and menagerie2, create a pets table inside each database, and insert a sample record inside each table.
3. Now, confirm that the sample data exists inside the tables in your databases by running the below statements.
You should see a result similar to the screenshot below. As you can see, the table in both databases has one record each. 4. Lastly, exit the mysql shell by typing quit or \q and press Enter. Exiting MySQLBacking Up Entire Databases using the mysqldump CommandYou now have MySQL databases, and it’s time to see the
There are two ways to back up entire MySQL databases; specific databases and all databases.
Specific Database BackupThis method creates a backup of one database at a time, which is useful when you want to dump each database into separate backup files. The basic syntax for this method is shown below.
For example, run the below commands to back up the
mysqldump command for single database backupSpecific Database BackupThe
For example, run the below command to create a single backup for the mysqldump command for multiple database backupAll Database BackupWhen you need to backup all databases at once without listing each database name, you must specify the
To back up all databases into a single backup file, run the below command in your terminal.
Inspecting the Database Backup FilesIf you followed all instructions, you now have two backup files with the As a result, you should see the four backup files on the list. Listing all MySQL database backup filesNow, open one of the backup files in your preferred text editor, such as nano or Visual Studio Code. Choose the file with the smallest size, which, in this case, is the Can you recognize the formatting in the below screenshot? Yes, those are SQL statements.
The Backing Up Tables using the mysqldump CommandApart from taking backups of entire databases, the Other benefits of table backups are portability and granularity of data restoration. A smaller backup file size is more portable, and you can restore table backups into a new or existing database. The syntax for backing up tables is as follows. After specifying the database name, you must append the list of tables to back up. If you do not provide the table names, the
Now that you know this syntax follow these steps to back up a database table. 1. Run the below command to back up only the pets table in the menagerie1 database. This command creates a file named menagerie1_pets_tb_backup.sql. Creating a backup of a specific table 2. Verify that the backup file creation is successful by listing the *.sql files. Verifying the backup file creation3. Optionally, open the backup file in a text editor or display the contents on the screen. Viewing
the database table backup fileRestoring from BackupHaving backups of your database or tables gives you peace of mind. Knowing that if your database becomes corrupted or someone accidentally deleted it, you have a point in time of the data you can restore. At this point, the Restoring Entire DatabaseEarlier in this tutorial, you created a backup of the 1. First, connect to the MySQL shell. 2. Next, drop the menagerie1 and menagerie2 databases to simulate losing multiple databases.
As you can see below, the database deletion was successful. Both databases no longer exist on the MySQL server. Deleting databases3. Now, restore the databases from the menagerie1_menagerie2_db_backup.sql file. To do so, execute the source or \. command followed by the backup filename (full path or relative path), and watch the magic happen!
You’ll see a similar output on the screen to the screenshot below. Restoring databases from backup4. Finally, confirm that the databases and their data were successfully restored.
You’ve now successfully restored your databases from backup. Querying data from the restored databasesRestoring Database TableIn this example, you’ll be restoring the 1. First, to simulate the loss of the table, run the below commands to delete the pets table from menagerie1.
The output below shows that no tables exist in the menagerie1 database after dropping the pets table. Deleting a table2. Now, execute the source or \. command followed by the table backup file. In this example, the backup file is menagerie1_pets_tb_backup.sql.
3. Finally, check that the pet table restoration was successful by running the below commands in the mysql shell.
As you can see below, the restoration recreated the pets table and data. Listing the table and querying dataConclusionBacking up your MySQL databases is an essential task that an administrator should ensure to
happen regularly. The This guide has only covered the basic options of using the In real-life usage, your database is hundred if not thousands of times bigger, and you don’t want your disk filled with SQL files. In that case, you would want to compress your dump file and save it on a network drive or, even better, to cloud storage like the Amazon S3. How can I get Mysqldump from all databases?To create a backup of all MySQL server databases, run the following command:. mysqldump --user root --password --all-databases > all-databases.sql. ... . mysql --user root --password mysql < all-databases.sql. ... . mysql --user root --password [db_name] < [db_name].sql. ... . select @@datadir;. How use Mysqldump commandTo dump/export a MySQL database, execute the following command in the Windows command prompt: mysqldump -u username -p dbname > filename. sql . After entering that command you will be prompted for your password. How do I view all MySQL databases?SHOW DATABASES lists the databases on the MySQL server host. SHOW SCHEMAS is a synonym for SHOW DATABASES . The LIKE clause, if present, indicates which database names to match. The WHERE clause can be given to select rows using more general conditions, as discussed in Section 26.8, “Extensions to SHOW Statements”. Does Mysqldump create database?Mysqldump is a command-line utility that is used to generate the logical backup of the MySQL database. It produces the SQL Statements that can be used to recreate the database objects and data. The command can also be used to generate the output in the XML, delimited text, or CSV format. |