{"id":99,"date":"2013-01-04T14:31:37","date_gmt":"2013-01-04T14:31:37","guid":{"rendered":"https:\/\/gosqeng.test\/?p=99"},"modified":"2019-11-28T12:37:16","modified_gmt":"2019-11-28T12:37:16","slug":"migrating-mysql-to-amazon-rds","status":"publish","type":"post","link":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds","title":{"rendered":"Migrating a production MySQL Database to Amazon RDS with minimal downtime"},"content":{"rendered":"<p>At GoSquared, we recently migrated our MySQL databases over to Amazon RDS, here&#8217;s how and why we&#8217;ve done it.<\/p>\n<h2>Why migrate to RDS?<\/h2>\n<p>They&#8217;re easy to maintain and scale whilst being pretty good value. The Multi-AZ deployment also means there should be minimal downtime should anything go wrong. We also didn&#8217;t like having to maintain multiple MySQL servers and handle replication between them. It was therefore a good time to change MySQL to a different server, and RDS is a great option.<\/p>\n<h2>Configuration and Booting<\/h2>\n<p>This is pretty easy, select whatever basic preferences you&#8217;d like, a DB Instance Identifier (<span class=\"code\">mysql-1<\/span> is probably descriptive enough) and a master username and password (which annoyingly can only be a max of 16 chars).<\/p>\n<p>I&#8217;d recommend enabling Multi-AZ Deployment to help with uptime and redundancy, but it does cost more so it&#8217;s all about your needs.<\/p>\n<p>In management options, enable automatic backups (losing data if things go monumentally wrong is not cool) and keep them for as long as you&#8217;d like. Select a backup window and maintenance window where your servers are at lowest load as the backup can use a fair amount of CPU and the maintenance window can have a restart within it (another reason to use Multi-AZ Deployment &#8211; you <em>shouldn&#8217;t<\/em> have any interruption).<\/p>\n<p>After you&#8217;ve booted your server it should show as a DB Instance. You&#8217;ll need to add whatever IP&#8217;s\/Security Groups you want to be able to access the server to DB Security Groups, or you won&#8217;t be able to connect.<\/p>\n<p>By clicking on the arrow for the instance and looking for the Endpoint, you&#8217;ll find what host you should be connecting to.<\/p>\n<h3>Read Replicas<\/h3>\n<p>If you want a server to handle only read queries, making it basically a slave, you can easily set one up by right clicking on the instance. Very useful if you need to deal with high read load on MySQL.<\/p>\n<h2>Migration Preparation<\/h2>\n<p>Now the important part, migrating your entire production database without going down for hours (hopefully less than a minute). You&#8217;ll need MySQL binlogging enabled.<\/p>\n<p>First, take a full MySQL dump of your database\u2026<\/p>\n<p><code>mysqldump --single-transaction --master-data=2 -C -q dbname -u username -p &gt; backup.sql<\/code><\/p>\n<p>This will export your whole database into one huge sql file.<\/p>\n<p>Next, import it to your RDS instance\u2026<\/p>\n<p><code>mysql -u username -p -h RDS_endpoint DB_name &lt; backup.sql<\/code><\/p>\n<p>This may take a while depending on your DB size. The <span class=\"code\">&#8211;master-data=2<\/span> flag when you took the dump stores the start position and filename of the binlog, to view it run something like <span class=\"code\">more backup.sql<\/span> and find something that looks like <span class=\"code\">CHANGE MASTER TO MASTER_LOG_FILE=&#8217;mysql-bin.000003&#8242;, MASTER_LOG_POS=350789121;<\/span>. mysql-bin.x is the filename and the number is the log position. As there&#8217;s data always being modified\/added in your production database, you&#8217;ll need to keep the new RDS instance up to date without spending the time importing a new dump, which would be too much downtime for many companies.<\/p>\n<p><code>mysqlbinlog \/var\/log\/mysql\/mysql-bin.000003 --start-position=350789121 --base64-output=NEVER &gt; output.sql<\/code><\/p>\n<p>This command will convert the binlog to valid SQL which can be run on the RDS to update it to the latest data.<\/p>\n<p>To update it on the server, run:<\/p>\n<p><code>cat output.sql | mysql -h RDS_endpoint -u username -p DB_name<\/code><\/p>\n<p>See Errors if you have any issues.<\/p>\n<p>To now find what line of the binlog the RDS instance is up to date to, tail output.sql and find end_log_pos. The longer you leave between updating the database with the binlogs, the longer it&#8217;ll take to update, so keep running it so when you come to the actual migration it won&#8217;t take long.<\/p>\n<h2>Migration<\/h2>\n<p>When you&#8217;re ready to actually migrate, prepare by getting the latest log position ready in the mysqlbinlog command.<\/p>\n<p>Then, on your current production database, run <span class=\"code\">FLUSH TABLES WITH READ LOCK;<\/span> on MySQL to stop all writes from going through.<\/p>\n<p>After this is done, run the prepared mysqlbinlog command. Then, update RDS to the very latest database.<\/p>\n<p>As soon as that has run, deploy all of your applications with the new MySQL configs. Migration complete.<\/p>\n<h2>Errors<\/h2>\n<p>While updating the RDS servers with the latest binlog, you may experience an error or two, we had this and spent ages debugging it. Here are a couple of common errors and fixes.<\/p>\n<h3 style=\"line-height:30px\">You do not have the SUPER privilege and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)<\/h3>\n<p>With RDS, you can&#8217;t have the SUPER privilege, so go into your DB Security Groups, create a new group and change <span class=\"code\">log_bin_trust_function_creaters<\/span> to <span class=\"code\">1<\/span>.<\/p>\n<h3 style=\"line-height:30px\">ERROR 1227 (42000) at line N: Access denied; you need (at least one of) the SUPER privilege(s) for this operation<\/h3>\n<p>We need to remove all lines with <span class=\"code\">SET @@session.pseudo_thread_id<\/span> commands, a simple grep can do this and then we can update our RDS instance straight away&#8230;<\/p>\n<p><code>grep -v \"SET @@session.pseudo_thread_id\" output.sql | mysql -h RDS_endpoint -u username -p DB_name<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>At GoSquared, we recently migrated our MySQL databases over to Amazon RDS, here&#8217;s how and why we&#8217;ve done it.<\/p>\n<h2>Why migrate to RDS?<\/h2>\n<p>They&#8217;re easy to maintain and scale whilst being pretty good value. The Multi-AZ deployment also means there should be minimal downtime should anything go wrong. We also didn&#8217;t like having to maintain multiple MySQL servers and handle replication between them. It was therefore a good time to change MySQL to a different server, and RDS is a great option.<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1452],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-engineering"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v18.6 (Yoast SEO v19.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Migrating MySQL to Amazon RDS without downtime - GoSquared Engineering<\/title>\n<meta name=\"description\" content=\"Migrating a production MySQL database to Amazon RDS can be done without affecting your users. Here&#039;s a quick guide to help make things easier.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Migrating a production MySQL Database to Amazon RDS with minimal downtime\" \/>\n<meta property=\"og:description\" content=\"Migrating a production MySQL database to Amazon RDS can be done without affecting your users. Here&#039;s a quick guide to help make things easier.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds\" \/>\n<meta property=\"og:site_name\" content=\"GoSquared Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GoSquared\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-04T14:31:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-28T12:37:16+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@simon_tabor\" \/>\n<meta name=\"twitter:site\" content=\"@GoSquared\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Simon Tabor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\",\"name\":\"GoSquared\",\"url\":\"https:\/\/www.gosquared.com\/blog\/\",\"sameAs\":[\"https:\/\/instagram.com\/gosquaredteam\",\"https:\/\/www.linkedin.com\/company\/go-squared-ltd.\",\"https:\/\/www.facebook.com\/GoSquared\",\"https:\/\/twitter.com\/GoSquared\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png\",\"contentUrl\":\"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png\",\"width\":1270,\"height\":250,\"caption\":\"GoSquared\"},\"image\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#website\",\"url\":\"https:\/\/www.gosquared.com\/blog\/\",\"name\":\"GoSquared Blog\",\"description\":\"Turn visitors into customers.\",\"publisher\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.gosquared.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#webpage\",\"url\":\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds\",\"name\":\"Migrating MySQL to Amazon RDS without downtime - GoSquared Engineering\",\"isPartOf\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#website\"},\"datePublished\":\"2013-01-04T14:31:37+00:00\",\"dateModified\":\"2019-11-28T12:37:16+00:00\",\"description\":\"Migrating a production MySQL database to Amazon RDS can be done without affecting your users. Here's a quick guide to help make things easier.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.gosquared.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Migrating a production MySQL Database to Amazon RDS with minimal downtime\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#webpage\"},\"author\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/71fad71f60ad33cf9356687b37aed3d0\"},\"headline\":\"Migrating a production MySQL Database to Amazon RDS with minimal downtime\",\"datePublished\":\"2013-01-04T14:31:37+00:00\",\"dateModified\":\"2019-11-28T12:37:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#webpage\"},\"wordCount\":788,\"publisher\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\"},\"articleSection\":[\"Engineering\"],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/71fad71f60ad33cf9356687b37aed3d0\",\"name\":\"Simon Tabor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dc920e48608646bda51d2e6e2595e8ad926cff52eba534c1d25fb1618f15b59f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/dc920e48608646bda51d2e6e2595e8ad926cff52eba534c1d25fb1618f15b59f?s=96&d=mm&r=g\",\"caption\":\"Simon Tabor\"},\"description\":\"Lead developer at GoSquared for integrations, partnerships and the API. Works on pretty much everything.\",\"sameAs\":[\"http:\/\/simontabor.com\",\"https:\/\/twitter.com\/simon_tabor\"],\"url\":\"https:\/\/www.gosquared.com\/blog\/author\/simontabor\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Migrating MySQL to Amazon RDS without downtime - GoSquared Engineering","description":"Migrating a production MySQL database to Amazon RDS can be done without affecting your users. Here's a quick guide to help make things easier.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds","og_locale":"en_US","og_type":"article","og_title":"Migrating a production MySQL Database to Amazon RDS with minimal downtime","og_description":"Migrating a production MySQL database to Amazon RDS can be done without affecting your users. Here's a quick guide to help make things easier.","og_url":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds","og_site_name":"GoSquared Blog","article_publisher":"https:\/\/www.facebook.com\/GoSquared","article_published_time":"2013-01-04T14:31:37+00:00","article_modified_time":"2019-11-28T12:37:16+00:00","twitter_card":"summary_large_image","twitter_creator":"@simon_tabor","twitter_site":"@GoSquared","twitter_misc":{"Written by":"Simon Tabor","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/www.gosquared.com\/blog\/#organization","name":"GoSquared","url":"https:\/\/www.gosquared.com\/blog\/","sameAs":["https:\/\/instagram.com\/gosquaredteam","https:\/\/www.linkedin.com\/company\/go-squared-ltd.","https:\/\/www.facebook.com\/GoSquared","https:\/\/twitter.com\/GoSquared"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png","contentUrl":"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png","width":1270,"height":250,"caption":"GoSquared"},"image":{"@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/www.gosquared.com\/blog\/#website","url":"https:\/\/www.gosquared.com\/blog\/","name":"GoSquared Blog","description":"Turn visitors into customers.","publisher":{"@id":"https:\/\/www.gosquared.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.gosquared.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#webpage","url":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds","name":"Migrating MySQL to Amazon RDS without downtime - GoSquared Engineering","isPartOf":{"@id":"https:\/\/www.gosquared.com\/blog\/#website"},"datePublished":"2013-01-04T14:31:37+00:00","dateModified":"2019-11-28T12:37:16+00:00","description":"Migrating a production MySQL database to Amazon RDS can be done without affecting your users. Here's a quick guide to help make things easier.","breadcrumb":{"@id":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.gosquared.com\/blog"},{"@type":"ListItem","position":2,"name":"Migrating a production MySQL Database to Amazon RDS with minimal downtime"}]},{"@type":"Article","@id":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#article","isPartOf":{"@id":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#webpage"},"author":{"@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/71fad71f60ad33cf9356687b37aed3d0"},"headline":"Migrating a production MySQL Database to Amazon RDS with minimal downtime","datePublished":"2013-01-04T14:31:37+00:00","dateModified":"2019-11-28T12:37:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.gosquared.com\/blog\/migrating-mysql-to-amazon-rds#webpage"},"wordCount":788,"publisher":{"@id":"https:\/\/www.gosquared.com\/blog\/#organization"},"articleSection":["Engineering"],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/71fad71f60ad33cf9356687b37aed3d0","name":"Simon Tabor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/dc920e48608646bda51d2e6e2595e8ad926cff52eba534c1d25fb1618f15b59f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc920e48608646bda51d2e6e2595e8ad926cff52eba534c1d25fb1618f15b59f?s=96&d=mm&r=g","caption":"Simon Tabor"},"description":"Lead developer at GoSquared for integrations, partnerships and the API. Works on pretty much everything.","sameAs":["http:\/\/simontabor.com","https:\/\/twitter.com\/simon_tabor"],"url":"https:\/\/www.gosquared.com\/blog\/author\/simontabor"}]}},"wps_subtitle":"Using Amazon RDS to minimise downtime","_links":{"self":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":0,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}