{"id":62,"date":"2012-11-28T10:48:12","date_gmt":"2012-11-28T10:48:12","guid":{"rendered":"http:\/\/blog.nicovs.be\/?p=62"},"modified":"2012-11-28T10:49:18","modified_gmt":"2012-11-28T10:49:18","slug":"migrating-mysql-to-a-new-server","status":"publish","type":"post","link":"https:\/\/www.nicovs.be\/?p=62","title":{"rendered":"Migrating MySQL to a new server"},"content":{"rendered":"<p>When you need to migrate a MySQL server instance to a new server for any reason, eg upgrade of hardware, distri upgrade,&#8230; you can easily do the following:<\/p>\n<p><strong>Migrating the databases<\/strong><br \/>\nOn the &#8220;old&#8221; mysql server, you need to export all databases by:<\/p>\n<pre class=\"lang:default decode:true\">mysqldump -uroot -p --all-databases &gt; \/tmp\/dumpofalldb.sql<\/pre>\n<p>Copy your sql dump file with eg rsync<\/p>\n<pre class=\"lang:default decode:true\">rsync -avh \/tmp\/dumpofalldb.sql username@192.168.10.11:\/tmp<\/pre>\n<p>On the new server, import your dumps:<\/p>\n<pre class=\"lang:default decode:true crayon-selected\">mysql -uroot -p &lt; \/tmp\/dumpofalldb.sql<\/pre>\n<p><strong>Migrating the users<\/strong><\/p>\n<p>If you are migrating to the <em>same<\/em> MySQL version you can export all users and settings by dumping the mysql database itself.<\/p>\n<pre>mysqldump -uroot -p mysql &gt; \/tmp\/dumpmysqldb.sql<\/pre>\n<p>and just copy &amp; import of that file on the new server<\/p>\n<p>However, when doing a MySQL version upgrade, we need to export all the Users and their privileges.<\/p>\n<p>Here&#8217;s an easy bash script to do this.<\/p>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\r\n# addapted from http:\/\/serverfault.com\/questions\/105612\/how-to-copy-user-priviledges-with-mysql\r\nread -p \"Enter your MySQL admin user: \" User\r\nread -sp \"Enter password\" Password\r\necho \"\"\r\necho \"-----------------------------------------------------------------------------------\"\r\n( mysql -u$User -p$Password --batch --skip-column-names -e \"SELECT user, host FROM user\" mysql ) | while read user host\r\ndo\r\n        CreateUser=`mysql -u$User -p$Password --batch --skip-column-names -e\"SHOW GRANTS FOR '$user'@'$host'\"`\r\n        echo \"$CreateUser;\"\r\n        echo \"flush privileges;\"\r\ndone\r\necho \"-----------------------------------------------------------------------------------\"<\/pre>\n<p>Copy the lines between the &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>Login on your new server, go to the mysql shell via the admin user<\/p>\n<pre class=\"lang:default decode:true\">mysql -uroot -p<\/pre>\n<p>And paste the output from our previous script.<\/p>\n<p>Et Voila, your databases and users are migrated!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you need to migrate a MySQL server instance to a new server for any reason, eg upgrade of hardware, distri upgrade,&#8230; you can easily do the following: Migrating the databases On the &#8220;old&#8221; mysql server, you need to export all databases by: mysqldump -uroot -p &#8211;all-databases &gt; \/tmp\/dumpofalldb.sql Copy your sql dump file with [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[4,3,6],"tags":[16,17,12],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-debian","category-mysql","category-ubuntu","tag-dump","tag-migrate","tag-mysql-2"],"_links":{"self":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=62"}],"version-history":[{"count":4,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}