{"id":818,"date":"2018-02-19T16:51:42","date_gmt":"2018-02-19T08:51:42","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=818"},"modified":"2018-09-05T11:11:14","modified_gmt":"2018-09-05T03:11:14","slug":"how-to-migrate-your-mysql-mariadb-database-with-mysqldump-tar-and-scp","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-migrate-your-mysql-mariadb-database-with-mysqldump-tar-and-scp\/","title":{"rendered":"How to migrate your MySQL \/ MariaDB database with mysqldump, tar and scp"},"content":{"rendered":"<p>If you had been building the content for your new <a href=\"https:\/\/www.techcoil.com\/blog\/setting-up-wordpress-on-raspberry-pi-3-with-raspbian-stretch-lite-nginx-mariadb-and-php-7-as-the-lemp-stack\/\" rel=\"noopener\" target=\"_blank\">WordPress site on a Raspberry Pi 3<\/a>, there will come a time when you need to migrate the underlying MySQL database to another machine, for eg. a <a href=\"https:\/\/m.do.co\/c\/68b7d9b94341\" rel=\"noopener\" target=\"_blank\">DigitalOcean<\/a> droplet, for production usage.<\/p>\n<p>MySQL provides us with the mongodump client utility to produce a set of SQL statements that can be executed to reproduce the original database object definitions and table data. <\/p>\n<p>A simple MySQL database migration can be performed with the following steps:<\/p>\n<ol>\n<li>Use <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/mysqldump.html\" rel=\"noopener\" target=\"_blank\">mysqldump<\/a> to get the set of SQL statements that can be executed to reproduce the original database object definitions and table data.<\/li>\n<li>If the mysqldump output is too big, use the <code>tar<\/code> command to compress it.<\/li>\n<li>Use the <code>scp<\/code> command to send the mysqldump output from the source server to the destination server.<\/li>\n<li>If you had used the <code>tar<\/code> command to compress the mysqldump output, use the tar command at the destination server to decompress it.<\/li>\n<li>Execute the set of SQL commands in the <code>mysqldump<\/code> output at the destination server.<\/li>\n<\/ol>\n<p>This post documents how you can migrate your MySQL \/ MariaDB database with utilities provided by MySQL and most Linux servers.<\/p>\n<h2>Exporting data from MySQL \/ MariaDB database with mysqldump<\/h2>\n<p>The way you export data from a MySQL \/ MariaDB database depends on whether you have a default user that is set to use the <a href=\"https:\/\/mariadb.com\/kb\/en\/library\/authentication-plugin-unix-socket\/\" rel=\"noopener\" target=\"_blank\">Authentication Plugin<\/a>.<\/p>\n<h3>Exporting data from MySQL \/ MariaDB server with a regular user without the Authentication Plugin<\/h3>\n<p>To export data from a MySQL \/ MariaDB database instance with a regular user without the Authentication Plugin, run the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo mysqldump -u root -p db_to_migrate &gt; db_to_migrate.sql\r\n<\/pre>\n<p>The command will prompt for the password for the root database user. Once the command completes, you will find a text file, <code>db_to_migrate.sql<\/code>, in your current directory. This file will contain all the SQL statements that can be run to reconstruct the db_to_migrate database instance.<\/p>\n<h3>Exporting data from MySQL \/ MariaDB server with a default user set to use the Authentication Plugin<\/h3>\n<p>To export data from a MySQL \/ MariaDB database instance with a default user set to use the Authentication Plugin, run the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo mysqldump db_to_migrate &gt; db_to_migrate.sql\r\n<\/pre>\n<p>If you had not supplied your system user password via previous <code>sudo<\/code> command executions, the command will prompt for the password of your current system user. Once the command completes, you will find a text file, <code>db_to_migrate.sql<\/code>, in your current directory. This file will contain all the SQL statements that can be run to reconstruct the db_to_migrate database instance.<\/p>\n<h2>Using the tar command to compress the output from mysqldump<\/h2>\n<p>If your database instance is large, you can use the tar command to compress the output from <code>mysqldump<\/code> to reduce the time needed to send the output file from the source server to a destination server.<\/p>\n<p>To compress the output from the <code>mysqldump<\/code> command mentioned earlier, we can run the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ntar -cvzf db_to_migrate.tar.gz db_to_migrate.sql \r\n<\/pre>\n<p>When the command complete, you should get the file, <code>db_to_migrate.tar.gz<\/code> in your current working directory. This file should be significantly smaller than the original output file that we got from running the <code>mysqldump<\/code> command mentioned earlier.<\/p>\n<h2>Sending the output from mysqldump from the source server to the destination server<\/h2>\n<p>If there is a SSH server running at your destination server, you can use the <code>scp<\/code> command to send the <code>db_to_migrate.tar.gz<\/code> file from your current working directory to the destination server. <\/p>\n<p>Suppose your destination server is reachable by the domain <code>example.com<\/code>, has a system user account with the username <code>root<\/code> and has a <code>\/var\/receiving<\/code> directory, you can run the following command to send your <code>db_to_migrate.tar.gz<\/code> file to the destination server:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nscp db_to_migrate.tar.gz root@example.com:\/var\/receiving\/db_to_migrate.tar.gz\r\n<\/pre>\n<h2>Using the tar command to decompress the output from mysqldump<\/h2>\n<p>Once the file transfer is done, you can use the <code>ssh<\/code> command to get into your destination server to extract the contents of <code>db_to_migrate.tar.gz<\/code>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nssh root@example.com\r\n<\/pre>\n<p>Once you got into your destination server, run the following commands to extract the original output file from running <code>mysqldump<\/code> at the source server:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd \/var\/receiving\r\ntar -zxvf db_to_migrate.tar.gz\r\n<\/pre>\n<p>Once the commands complete, you should be able to get back the original output file from running <code>mysqldump<\/code> at the source server.<\/p>\n<h2>Importing data into the destination MySQL \/ MariaDB server<\/h2>\n<p>The last step to migrating your MySQL \/ MariaDB database is to import the data to the destination MySQL \/ MariaDB server.<\/p>\n<p>As with the case of exporting data from a MySQL \/ MariaDB database, importing data into the destination MySQL \/ MariaDB server depends on whether you have a default user that is set to use the Authentication Plugin.<\/p>\n<h3>Importing data into MySQL \/ MariaDB server with a regular user without the Authentication Plugin<\/h3>\n<p>Assuming that the <code>a_database<\/code> database instance was created in the destination MySQL \/ MariaDB server, run the following command to import the data from the source server:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo mysql -u root -p a_database &lt; db_to_migrate.sql\r\n<\/pre>\n<p>The command will prompt for the password for the root user. Once the command completes, the database content from the source MySQL \/ MariaDB server will be reconstructed at the destination server.<\/p>\n<h3>Importing data into MySQL \/ MariaDB server with a default user set to use the Authentication Plugin<\/h3>\n<p>To import data into a MySQL \/ MariaDB database instance with a default user set to use the Authentication Plugin, run the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo mysql a_database &lt; db_to_migrate.sql \r\n<\/pre>\n<p>If you had not supplied your system user password via previous <code>sudo<\/code> command executions, the command will prompt for the password of your current system user. Once the command completes, the database contents from the source MySQL \/ MariaDB server will be reconstructed at the destination server.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-dc\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-dc&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-dc&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F818&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>If you had been building the content for your new <a href=\"https:\/\/www.techcoil.com\/blog\/setting-up-wordpress-on-raspberry-pi-3-with-raspbian-stretch-lite-nginx-mariadb-and-php-7-as-the-lemp-stack\/\" rel=\"noopener\" target=\"_blank\">WordPress site on a Raspberry Pi 3<\/a>, there will come a time when you need to migrate the underlying MySQL database to another machine, for eg. a <a href=\"https:\/\/m.do.co\/c\/68b7d9b94341\" rel=\"noopener\" target=\"_blank\">DigitalOcean<\/a> droplet, for production usage.<\/p>\n<p>MySQL provides us with the mongodump client utility to produce a set of SQL statements that can be executed to reproduce the original database object definitions and table data. <\/p>\n<p>A simple MySQL database migration can be performed with the following steps:<\/p>\n<ol>\n<li>Use <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/mysqldump.html\" rel=\"noopener\" target=\"_blank\">mysqldump<\/a> to get the set of SQL statements that can be executed to reproduce the original database object definitions and table data.<\/li>\n<li>If the mysqldump output is too big, use the <code>tar<\/code> command to compress it.<\/li>\n<li>Use the <code>scp<\/code> command to send the mysqldump output from the source server to the destination server.<\/li>\n<li>If you had used the <code>tar<\/code> command to compress the mysqldump output, use the tar command at the destination server to decompress it.<\/li>\n<li>Execute the set of SQL commands in the <code>mysqldump<\/code> output at the destination server.<\/li>\n<\/ol>\n<p>This post documents how you can migrate your MySQL \/ MariaDB database with utilities provided by MySQL and most Linux servers.<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[446,9,505,503,438,498],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/mongoDB-logo.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-dc","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/818"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}