{"id":4469,"date":"2021-02-15T09:37:30","date_gmt":"2021-02-15T09:37:30","guid":{"rendered":"https:\/\/linuxways.net\/?p=4469"},"modified":"2021-02-15T09:37:34","modified_gmt":"2021-02-15T09:37:34","slug":"how-to-backup-and-restore-mysql-databases-on-red-hat-enterprise-linux-8","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/red-hat\/how-to-backup-and-restore-mysql-databases-on-red-hat-enterprise-linux-8\/","title":{"rendered":"How to Backup and Restore MySQL Databases on Red Hat Enterprise Linux 8"},"content":{"rendered":"<p>The ability to restore databases when data failures occur is very important. Data failures can be as a result of an accidental or malicious modification, deletion or corruption of data.<\/p>\n<p>In this guide, you will learn how to backup an existing MySQL database on Red Hat Enterprise Linux (RHEL) 8 by using the mysqldump utility. Also, you will learn how to restore a MySQL database in order to recover from data failures.<\/p>\n<h2>Backup a MySQL Database<\/h2>\n<p>You can use the <strong>mysqldump<\/strong> utility to backup your MySQL database. Firstly, run the command below to confirm that the mysql service is active (running.)<\/p>\n<pre>$ <strong>sudo systemctl status mysqld<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"667\" height=\"277\" class=\"wp-image-4470\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-256.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-256.png 667w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-256-300x125.png 300w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/strong><\/p>\n<p>Press <strong>q<\/strong> to return to the terminal prompt.<\/p>\n<p>If you see a message which indicates that the mysql service is disabled or inactive (dead,) then run the next command to start the mysql service.<\/p>\n<pre>$<strong> sudo systemctl start mysqld<\/strong><\/pre>\n<p>Once the mysql service is active (running,) you may login.<\/p>\n<pre>$<strong> sudo mysql -u root -p<\/strong><\/pre>\n<p><strong>Note: <\/strong>You may replace <strong>root<\/strong> with another mysql user as appropriate.<\/p>\n<h3>Create a sample database and table<\/h3>\n<p>For the purpose of this tutorial, I have created a sample database and table by running the following queries. You may do the same.<\/p>\n<pre>mysql&gt;<strong> CREATE DATABASE cars;<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"537\" height=\"51\" class=\"wp-image-4471\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-257.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-257.png 537w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-257-300x28.png 300w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><\/strong><\/p>\n<pre>mysql&gt;<strong> USE cars;<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"461\" height=\"59\" class=\"wp-image-4472\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-258.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-258.png 461w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-258-300x38.png 300w\" sizes=\"auto, (max-width: 461px) 100vw, 461px\" \/><\/strong><\/p>\n<pre>mysql&gt;<strong> CREATE TABLE suvs (make varchar(25), model varchar(25), VIN varchar(17));<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"673\" height=\"51\" class=\"wp-image-4473\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-259.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-259.png 673w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-259-300x23.png 300w\" sizes=\"auto, (max-width: 673px) 100vw, 673px\" \/><\/strong><\/p>\n<pre>mysql&gt;<strong> INSERT INTO suvs VALUES ('Toyota', 'Rav4', '12345678901234567'), ('Honda', 'Crv', '09876543211234567'), ('Nissan', 'Pathfinder', '25162435453647567');<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"87\" class=\"wp-image-4474\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-260.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-260.png 739w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-260-300x35.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/strong><\/p>\n<pre>mysql&gt;<strong> SELECT * FROM suvs;<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"165\" class=\"wp-image-4475\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-261.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-261.png 543w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-261-300x91.png 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/strong><\/p>\n<p>Run the next command to quit mysql.<\/p>\n<pre>mysql&gt;<strong> quit<\/strong><\/pre>\n<h3>Export database with mysqldump<\/h3>\n<p>The syntax of the mysqldump command is:<\/p>\n<pre>mysqldump -u username -p database_to_backup &gt; backup_file_name.sql<\/pre>\n<p>Replace:<\/p>\n<ol>\n<li><strong>username<\/strong> with an actual username that has appropriate permissions on the database<\/li>\n<li><strong>database_to_backup<\/strong> with the actual name of the database you intend to backup<\/li>\n<li><strong>backup_file_name<\/strong> with your desired name for the backup file<\/li>\n<\/ol>\n<p>Using the sample cars database as an example, the following command will backup the database to the database_backups folder in my home directory.<\/p>\n<pre>$ <strong>mysqldump -u root -p cars &gt; \/home\/shola\/database_backups\/cars_bk.sql<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"53\" class=\"wp-image-4476\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-262.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-262.png 705w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-262-300x23.png 300w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/strong><\/p>\n<p>If no errors are displayed, then the operation completed successfully.<\/p>\n<h3>Verify database backup<\/h3>\n<p>Next, you can quickly examine the content of the database backup file by running the command below. Remember to replace backup_file_name accordingly.<\/p>\n<pre>$ <strong>head backup_file_name.sql<\/strong><\/pre>\n<p>See the figure below for a sample output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"605\" height=\"179\" class=\"wp-image-4477\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-263.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-263.png 605w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-263-300x89.png 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/p>\n<h2>Restore a MySQL Database<\/h2>\n<p>The only thing that is more important than taking backups is ensuring that you can actually restore those backups in the event of any data failure.<\/p>\n<p>Imagine that the sample cars database which we created earlier has just been maliciously corrupted. Luckily, we have a backup of the cars database and we can restore it as follows.<\/p>\n<h3>Create a new database<\/h3>\n<p>We would need to login to mysql and create a new database for the restore operation.<\/p>\n<p>Login to mysql as follows.<\/p>\n<pre>$ <strong>mysql -u root -p<\/strong><\/pre>\n<p>Next, run the query below to create a new database.<\/p>\n<pre>mysql&gt; <strong>CREATE DATABASE cars2;<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"473\" height=\"57\" class=\"wp-image-4478\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-264.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-264.png 473w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-264-300x36.png 300w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/strong><\/p>\n<p>Once the query executes successfully (OK), we may quit mysql.<\/p>\n<pre>mysql&gt; <strong>quit<\/strong><\/pre>\n<h3>Import database from backup<\/h3>\n<p>The basic mysql syntax for restoring a mysql database is:<\/p>\n<pre>mysql -u username -p new_database_name &lt; backup_file_name.sql<\/pre>\n<p>Replace:<\/p>\n<ol>\n<li><strong>username<\/strong> with an actual username that has appropriate permissions on the database<\/li>\n<li><strong>new_database_name<\/strong> with the name of the new database created for the restore operation<\/li>\n<li><strong>backup_file_name<\/strong> with the name of the database backup file<\/li>\n<\/ol>\n<p>For example, the following command restores the cars database from backup.<\/p>\n<pre>$ <strong>mysql -u root -p cars2 &lt; \/home\/shola\/database_backups\/cars_bk.sql<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"691\" height=\"53\" class=\"wp-image-4479\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-265.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-265.png 691w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-265-300x23.png 300w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><\/strong><\/p>\n<p>If no errors are displayed, then the restore operation completed successfully.<\/p>\n<h3>Verify database restore<\/h3>\n<p>We could login to mysql and verify the database restore operation as follows.<\/p>\n<pre>$ <strong>mysql -u root -p<\/strong><\/pre>\n<p>Once you are logged in, run the query below to show available databases.<\/p>\n<pre>mysql&gt; <strong>SHOW DATABASES;<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"465\" height=\"203\" class=\"wp-image-4480\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-266.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-266.png 465w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-266-300x131.png 300w\" sizes=\"auto, (max-width: 465px) 100vw, 465px\" \/><\/strong><\/p>\n<p>The next query selects the cars2 database.<\/p>\n<pre>mysql&gt; <strong>USE cars2;<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"89\" class=\"wp-image-4481\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-267.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-267.png 557w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-267-300x48.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/strong><\/p>\n<p>Display tables in the cars2 database with the query below.<\/p>\n<pre>mysql&gt; <strong>SHOW TABLES;<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"475\" height=\"159\" class=\"wp-image-4482\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-268.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-268.png 475w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-268-300x100.png 300w\" sizes=\"auto, (max-width: 475px) 100vw, 475px\" \/><\/strong><\/p>\n<p>Finally, select all records from the suvs table as follows.<\/p>\n<pre>mysql&gt; <strong>SELECT * FROM suvs;<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"159\" class=\"wp-image-4483\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-269.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-269.png 499w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-269-300x96.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/strong><\/p>\n<h2>Conclusion<\/h2>\n<p>In this guide, we successfully backed up and restored a MySQL database on Red Hat Enterprise Linux 8. Do let us know if you have any questions or comments.<\/p>","protected":false},"excerpt":{"rendered":"<p>The ability to restore databases when data failures occur is very important. Data failures can be as a result of an accidental or malicious modification, deletion or corruption&hellip;<\/p>","protected":false},"author":28,"featured_media":4484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[165],"tags":[194,195],"class_list":["post-4469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-red-hat","tag-backup","tag-restore"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=4469"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4469\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/4484"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=4469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=4469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=4469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}