{"id":2032,"date":"2012-02-19T17:29:10","date_gmt":"2012-02-19T09:29:10","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=2032"},"modified":"2023-06-24T18:29:48","modified_gmt":"2023-06-24T18:29:48","slug":"how-to-backup-and-restore-mysql-database-on-centosrhel","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-backup-and-restore-mysql-database-on-centosrhel\/","title":{"rendered":"How to Backup and Restore MySQL Database on CentOS\/RHEL"},"content":{"rendered":"<p>In this guide, we will delve into the process of backing up and restoring a MySQL database on a Linux server, specifically focusing on CentOS and RHEL servers. This tutorial is based on practical experience and thorough testing, ensuring that the steps provided are reliable and effective.<\/p>\n<p>The central tool we will be using is MySQLDump, a database backup program originally developed by Igor Romanenko. MySQLDump is a versatile utility that can be used to dump a database or a collection of databases for backup or transfer purposes. While it&#8217;s primarily used with MySQL servers, it can also work with other SQL servers. The dump typically contains SQL statements to create the table, populate it, or both. However, MySQLDump can also generate files in CSV, other delimited text, or XML format. You can learn more about MySQLDump on the <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/mysqldump.html\">official documentation page<\/a>.<\/p>\n<p>To use MySQLDump, you need to log on to the system running the MySQL Database. This can be done remotely using Telnet if you don&#8217;t have physical access to the machine. In this tutorial, we will assume that the MySQL server has been installed with the following version\/release:<\/p>\n<ul>\n<li>Version: 5.1.52<\/li>\n<li>Release     : 1.el6_0.1<\/li>\n<\/ul>\n<pre>\r\nName        : mysql\r\nArch        : i686\r\nVersion     : 5.1.52\r\nRelease     : 1.el6_0.1\r\n<\/pre>\n<p>Before we dive into the steps, it&#8217;s important to understand the terminology we will be using:<\/p>\n<p><databasename>: This refers to the existing database that you want to back up.<br \/>\n<databasebackupfiles>: This is the name of the backup file you will create. This is optional and can be defined by the user.<br \/>\nNow, let&#8217;s move on to the steps for backing up and restoring your MySQL database.<\/p>\n<h2>Backing Up Your MySQL Database<\/h2>\n<p>The first step is to log in to MySQL to check the existing databases. You can do this by entering the following command:<\/p>\n<pre>\r\nmysql -u root -p\r\n<\/pre>\n<p>You will be prompted to enter your password. After logging in, you can view the available databases by entering the following command:<\/p>\n<pre>\r\nshow databases;\r\n<\/pre>\n<p>Example:<\/p>\n<pre>mysql&gt; show databases;\r\n+--------------------+\r\n| Database           |\r\n+--------------------+\r\n| information_schema |\r\n| mysql              |\r\n| phpmyadmin         |\r\n+--------------------+\r\n3 rows in set (0.00 sec)\r\n\r\nmysql&gt; exit\r\nBye<\/pre>\n<p>To back up a specific database to a .sql file, use the following command:<\/p>\n<pre>\r\nmysqldump -u root -p &lt;databasename&gt; &gt; &lt;databasebackupfiles&gt;.sql\r\n<\/pre>\n<p>You will be prompted to enter your MySQL password. For example, if you want to back up a database named &#8220;mysql&#8221; to a file named &#8220;mysql19022012.sql&#8221;, you would use the following command:<\/p>\n<pre>\r\nmysqldump -u root -p mysql &gt; mysql19022012.sql\r\n<\/pre>\n<p>You can view the backed-up file by using the ls command:<\/p>\n<pre>\r\nls | grep mysql\r\n<\/pre>\n<p>You can also view the content of the mysql19022012.sql file by using the more command:<\/p>\n<pre>\r\nmore mysql19022012.sql\r\n<\/pre>\n<h2>Restoring Your MySQL Database<\/h2>\n<p>To restore the backed-up database to a new database, use the following command:<\/p>\n<pre>\r\nmysql -u root -p &lt;mysqlpassword&gt; &lt;databasename&gt; &lt; &lt;databasebackupfiles&gt;.sql\r\n<\/pre>\n<p>In this command, <mysqlpassword> is your MySQL password, <databasename> is the name of the database that will be restored, and <databasebackupfiles> is the backed-up database file, which is normally a .sql file.<\/p>\n<p>First, create a new database. Forexample, if you want to create a new database named &#8220;mysqlnew&#8221;, you would use the following command:<\/p>\n<pre>\r\nCREATE DATABASE mysqlnew;\r\n<\/pre>\n<p>To restore the mysql19022012.sql backed-up file to the database named &#8220;mysqlnew&#8221;, you would use the following command:<\/p>\n<pre>\r\nmysql -u root -p password mysqlnew &lt; mysql19022012.sql\r\n<\/pre>\n<p>Or, if you prefer to be prompted for your password, you can use the following command:<\/p>\n<pre>\r\nmysql -u root -p mysqlnew &lt; mysql19022012.sql\r\n<\/pre>\n<h2>Commands Mentioned<\/h2>\n<ul>\n<li><span class=\"fw-bold\">mysql -u root -p<\/span> \u2013 This command is used to log in to your MySQL server.<\/li>\n<li><span class=\"fw-bold\">show databases;<\/span> \u2013 This command is used to display the databases available on your MySQL server.<\/li>\n<li><span class=\"fw-bold\">mysqldump -u root -p &lt;databasename&gt; &gt; &lt;databasebackupfiles&gt;.sql<\/span> \u2013 This command is used to back up a specific database to a `.sql` file.<\/li>\n<li><span class=\"fw-bold\">ls | grep mysql<\/span> \u2013 This command is used to display the backed-up file.<\/li>\n<li><span class=\"fw-bold\">more mysql19022012.sql<\/span> \u2013 This command is used to view the content of the backed-up file.<\/li>\n<li><span class=\"fw-bold\">CREATE DATABASE mysqlnew;<\/span> \u2013 This command is used to create a new database.<\/li>\n<li><span class=\"fw-bold\">mysql -u root -p password mysqlnew &lt; mysql19022012.sql<\/span> \u2013 This command is used to restore the backed-up database to the new database.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Backing up and restoring a MySQL database on a Linux server is a crucial task for any webmaster or website administrator. By using the MySQLDump utility and following the steps outlined in this guide, you can ensure that your data is safely backed up and can be restored when needed.<\/p>\n<p>Remember, regular backups are an essential part of maintaining a robust and reliable database system. Whether you&#8217;re running a small blog or a large e-commerce site, having up-to-date backups can save you from potential data loss and downtime.<\/p>\n<p>For more in-depth information about MySQL and other web server technologies, check out our guides on the <a href=\"https:\/\/webhostinggeeks.com\/best\/web-server\/\">best web servers<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/apache-http-server-explained\/\">Apache<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/nginx-server-explained\/\">Nginx<\/a>, and <a href=\"https:\/\/webhostinggeeks.com\/blog\/litespeed-web-server-explained\/\">LiteSpeed<\/a>. If you&#8217;re interested in different hosting options, we also have articles on <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated server<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS server<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-cloud-hosting\/\">cloud hosting<\/a>, and <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-shared-hosting\/\">shared hosting<\/a>.<\/p>\n<h2>FAQ<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is MySQLDump?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">MySQLDump is a database backup program originally developed by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer purposes. It can work with MySQL servers and other SQL servers, and it can generate files in CSV, other delimited text, or XML format.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I back up a MySQL database?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can back up a MySQL database using the MySQLDump utility. The command for this is &#8216;mysqldump -u root -p &lt;databasename&gt; &gt; &lt;databasebackupfiles&gt;.sql&#8217;, where &lt;databasename&gt; is the name of the database you want to back up, and &lt;databasebackupfiles&gt; is the name of the backup file you want to create.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I restore a MySQL database?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<span itemprop=\"text\">To restore a MySQL database, you first need to create a new database using the &#8216;CREATE DATABASE &lt;databasename&gt;&#8217; command. Then, you can restore the backed-up database to the new database using the &#8216;mysql -u root -p &lt;mysqlpassword&gt; &lt;databasename&gt; &lt; &lt;databasebackupfiles&gt;.sql&#8217; command, where &lt;mysqlpassword&gt; is your MySQL password, &lt;databasename&gt; is the name of the new database, and &lt;databasebackupfiles&gt; is the backed-up database file.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the purpose of the &#8216;show databases&#8217; command?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The &#8216;show databases&#8217; command is used to display the databases available on your MySQL server. This can be useful to confirm the existence of a specific database before attempting to back it up or restore it.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What does the &#8216;ls | grep mysql&#8217; command do?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The &#8216;ls | grep mysql&#8217; command is used to display the backed-up file. The &#8216;ls&#8217; command lists the files in the current directory, and the &#8216;grep mysql&#8217; part filters the output to only show files that contain the word &#8216;mysql&#8217;.<\/span>\n            <\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we will delve into the process of backing up and restoring a MySQL database on a Linux server, specifically focusing on CentOS and RHEL servers. This tutorial&#8230;<\/p>\n","protected":false},"author":6,"featured_media":343,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1109,1008],"tags":[1217,1244,1246,1248,1253,1322,1536,1585,1709,1713,1715],"class_list":["post-2032","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backup-and-restore-method","category-mysql","tag-backup","tag-centos","tag-centos-5-5","tag-centos-5-7","tag-centos-6-2","tag-database","tag-linux","tag-mysql","tag-restore","tag-rhel","tag-rhel-6"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/2032","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=2032"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/2032\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/343"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=2032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=2032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=2032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}