{"id":2035,"date":"2021-10-14T22:17:45","date_gmt":"2021-10-14T19:17:45","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/migrate-mysql-db-upcloud-managed-databases\/"},"modified":"2021-10-14T22:17:45","modified_gmt":"2021-10-14T19:17:45","slug":"migrate-mysql-db-upcloud-managed-databases","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/migrate-mysql-db-upcloud-managed-databases\/","title":{"rendered":"How to migrate MySQL DB to UpCloud Managed Databases"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Migrating computer services is always a daunting task and moving over databases with business-critical data can be doubly so. However, with just a few simple steps, you can safely migrate a MySQL database to UpCloud Managed Databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Moving your MySQL databases to Managed Databases will take away the need for manual maintenance. In this tutorial, we\u2019ll show you the tools, steps and requirements to make the migration easy and painless.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pre-requisites to migrating MySQL<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. New Managed Database<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Start by setting up a new <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/set-up-upcloud-managed-databases\">Managed Database cluster<\/a> you are migrating into. You will need to choose a plan with enough storage capacity for your existing database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Host for the migration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You will also need a host system that can facilitate the migration. While in principle, you could export your database on almost any computer, the storage capacity and network speeds might make it impractical in most cases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Managed Databases perform the best by serving a Cloud Server over a Private network within the same data centre which will make importing much quicker.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/deploy-server\">Create a new Cloud Server<\/a> to export and import your MySQL database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Database tools<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have a Cloud Server up and running, you are almost ready to start migrating. However, you still need to install the tools that will do the heavy lifting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/mysqlpump.html\" target=\"_blank\" rel=\"noopener\">Mysqldump<\/a> is a common MySQL client utility and database backup program that performs logical backups. It is used to produce a set of SQL statements that when executed will reproduce the original database object definitions and table data. It can be used to dump one or more MySQL databases for backup or to migrate to a new SQL server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/mysql.html\" target=\"_blank\" rel=\"noopener\">MySQL client<\/a> is a popular command-line tool for manual input editing capabilities. In addition to offering interactive query options, the MySQL client can be used to import the data dump into the new Managed Database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can install both of these with one of the following commands.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Debian or Ubuntu\nsudo apt install mysql-client\n\n# CentOS\nsudo dnf install mysql-client<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, it\u2019s important to note that the migration steps will take some time, especially with larger databases. And while <tt>mysqldump<\/tt> includes verbose output options, MySQL client doesn\u2019t provide progress status. Therefore, before starting the migration, you should install a pipe viewer which can be used to keep tabs on the transfer processes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Ubuntu or Debian\nsudo apt install pv\n\n# CentOS\nsudo dnf install pv<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the prerequisites all set, you are ready to start migrating!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Exporting data from your old database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you\u2019ve set up your new Managed Database and a Cloud Server to facilitate the migration, you can start by taking a backup of your old MySQL database. This is done using the <tt>mysqldump<\/tt> command-line tool to create a single-file backup.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note that the migration process will take some time.<\/strong> You should stop any applications from modifying the database during the migration which can create some downtime to your services. To help estimate the downtime caused by the migration, you should do a practice run of the migration before committing to the move.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a backup of your MySQL database by running the command below. Replace the database name, hostname and username with those corresponding to your old database. You may also need to change the port number if your database doesn\u2019t use the default port.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mysqldump --database <span style=\"color: #ff0000;\">databasename(s)<\/span> -h <span style=\"color: #ff0000;\">source.db.hostaddress.com<\/span> -P 3306 -u <span style=\"color: #ff0000;\">username<\/span> -p --single-transaction --set-gtid-purged=OFF --hex-blob | pv &gt; mydb_export.sql<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The command will export the database into a file mydb_export.sql in the directory it is executed in.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><tt><strong>databasename(s)<\/strong><\/tt> is used to define the names of the databases you want to export.<br>If you have multiple databases, you can split the task and migrate them individually or migrate them together with <strong><tt>--databases db1 db2<\/tt><\/strong> etc.<\/li><li><tt><strong>-h source.db.hostaddress.com<\/strong><\/tt> defines the database host you want to export.<br>Note that if your old database is not in the same UpCloud data centre as the Cloud Server used for the migration, you\u2019ll need to allow connection over the public network.<\/li><li><tt><strong>-P 3306<\/strong><\/tt> sets the port number used to connect to the database host. Change it as needed according to your old database connection settings.<\/li><li><tt><strong>-u username -p<\/strong><\/tt> options are used to pass the credentials needed to access the database.<br>Note that it is insecure to include the password in the command itself. Rather you will be prompted to enter your password when running the command.<\/li><li><tt><strong>--single-transaction<\/strong><\/tt> option is used to start a transaction before running the export instead of locking the entire database.<br>This way mysqldump can read the database in its current state at the time of the transaction which makes the data dump consistent. Note that only InnoDB tables are dumped in a consistent state using this option. For example, any MyISAM or MEMORY tables may still change state while exporting using this option.<\/li><li><tt><strong>--set-gtid-purged=OFF<\/strong><\/tt> option should be set if you are using Global Transaction Identifiers (GTID) so that the target server records these transactions as applied.<br>For a server where GTIDs are not in use, use the AUTO option. Only use this option for a server where GTIDs are in use if you are sure that the required GTID set is already present in gtid_purged on the target server and should not be changed, or if you plan to identify and add any missing GTIDs manually.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Importing data to Managed Databases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After you have exported your MySQL database to the Cloud Server, you can then begin importing the data to your new Managed Database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pv mydb_export.sql | mysql -h <span style=\"color: #ff0000;\">target.db.upclouddatabases.com<\/span> -P 11550 -u upadmin -p<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The MySQL client command used to import the data has mostly the same parameters as the mysqldump in the previous section. Below is a quick recap of the parameters you will need to set.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><tt><strong>-h target.db.upclouddatabases.com<\/strong><\/tt> defines the database host.<\/li><li><tt><strong>-P 11550<\/strong><\/tt> sets the port number used to connect to the database host.<\/li><li><tt><strong>-u upadmin -p<\/strong><\/tt> options are used to pass the credentials needed to access the database.<br>You will be prompted to enter your password when running the command. The password for your <em>upadmin<\/em> account can be found in your <a rel=\"noopener\" href=\"https:\/\/hub.upcloud.com\/database\/\" target=\"_blank\">UpCloud Control Panel<\/a>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Recreating user accounts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Having finished migrating over your MySQL databases to the UpCloud Managed Databases, you are almost done. However, you will still need to recreate the user accounts used to access your databases by your applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can check the list of users in your old database by connecting with the MySQL client and using the following query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mysql <span style=\"color: #ff0000;\">databasename<\/span>&nbsp;-h <span style=\"color: #ff0000;\">source.db.hostaddress.com<\/span> -P 3306 -u <span style=\"color: #ff0000;\">username<\/span> -p<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SELECT user,host FROM mysql.user;<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------------------+------------+\n| user                  | host       |\n+-----------------------+------------+\n| repluser              | %          |\n| root                  | %:%        |\n| wordpress             | 10.5.9.116 |\n| metrics_user_datadog  | ::1        |\n| metrics_user_telegraf | ::1        |\n| mysql.infoschema      | localhost  |\n| mysql.session         | localhost  |\n| mysql.sys             | localhost  |\n+-----------------------+------------+\n8 rows in set\nTime: 0.013s<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the example above, we have a user account called <tt>wordpress<\/tt> that is used by a WordPress website. It will need to be recreated in the new database to allow WordPress to be switched over.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, connect to the new database using the MySQL client.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mysql <span style=\"color: #ff0000;\">databasename<\/span> -h <span style=\"color: #ff0000;\">target.db.upclouddatabases.com<\/span> -P 11550 -u upadmin -p<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then run the following commands to create the user and grant it permissions to the relevant database.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE USER 'username'@'app.host.ip' IDENTIFIED BY 'password';\nGRANT ALL ON databasename.* TO 'username'@'app.host.ip';\nFLUSH PRIVILEGES;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once done, you can exit the command-line client and continue below with finalising the migration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Finalising the migration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To finalise the migration, you should still run the <tt><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/mysqlcheck.html\" target=\"_blank\" rel=\"noopener\">mysqlcheck<\/a><\/tt> on the database to ensure proper database statistics are in place for the newly loaded data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <tt>mysqlcheck<\/tt> command-line tool is used to perform table maintenance to check, repair, optimise, or analyse tables. The tables are locked for the duration of the check operation and are therefore unavailable to other sessions so this should be run before the new database is adapted to use. Note that this check operation can be time-consuming depending on the size and number of tables in the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the <tt>mysqlcheck<\/tt> command on your new Managed Database using the example underneath. Remember to replace the host address with yours.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mysqlcheck <span style=\"color: #ff0000;\">databasename<\/span> -h <span style=\"color: #ff0000;\">target.db.upclouddatabases.com<\/span> -P 11550 -u upadmin -p<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The output of the check command would be similar to the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">databasename.table1                                  OK\ndatabasename.table10                                 OK\ndatabasename.table2                                  OK\ndatabasename.table3                                  OK\ndatabasename.table4                                  OK\ndatabasename.table5                                  OK\ndatabasename.table6                                  OK\ndatabasename.table7                                  OK\ndatabasename.table8                                  OK\ndatabasename.table9                                  OK<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once all tables have been checked, your MySQL database migration is complete!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can now begin configuring your applications to use the new Managed Databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, reconfigure the database connection details for a WordPress instance by setting up the wp-config.php file by making a backup of the existing file and then change the settings as indicated below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cp \/var\/www\/html\/wordpress\/wp-config.php \/var\/www\/html\/wordpress\/wp-config-backup.php\nnano \/var\/www\/html\/wordpress\/wp-config.php<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Set the database and username as shown below and replace the password<tt><\/tt> and hostname<tt><\/tt> with the details of the new Managed Database.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ ** MySQL settings - You can get this info from your web host ** \/\/\n\/** The name of the database for WordPress *\/\ndefine( 'DB_NAME', 'wordpress' );\n\n\/** MySQL database username *\/\ndefine( 'DB_USER', 'wordpress' );\n\n\/** MySQL database password *\/\ndefine( 'DB_PASSWORD', '<span style=\"color: #ff0000;\">password<\/span>' );\n\n\/** MySQL hostname *\/\ndefine( 'DB_HOST', '<span style=\"color: #ff0000;\">hostname<\/span>.db.upclouddatabases.com:11550' );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When done, save the file and exit the editor. The WordPress site should then be connected to the new Managed Database.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","community-category":[226,268],"class_list":["post-2035","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2035"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2035\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2035"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}