
Introduction
Migrating a local MySQL Database to Vultr Managed Databases for MySQL is a good practice that implements remote accessibility, increased security, high availability, minimal maintenance, and administration for your database.
In this article, you'll learn how to migrate an on-premise MySQL database to a Vultr Managed Database cluster.
Prerequisites
To successfully migrate the database, you need to:
- Deploy a managed MySQL cluster at Vultr
- Have an existing local MySQL database server to export.
- Have a local MySQL user with
SELECT,VIEW,TRIGGER, andLOCK TABLESprivileges on the local database.
Export the MySQL Database
mysqldump is a backup utility that stores MySQL databases in the form of SQL statements used to reproduce the original database when executed. In this section, you'll back up a single database to a file and import it to a managed database.
Log in to your local MySQL database server. Replace
dbuserwith your actual database user.$ mysql -u dbuser -pShow databases, and keep note of the database name you intend to export.
SHOW DATABASES;Exit the MySQL console.
EXITStop all services using the local database to prevent modifications during export. Services that use the local database will be offline during the migration.
Using the
mysqldumputility, export a single database to a file.$ mysqldump -u dbuser -p example-database > localdb.sqlTo export all databases, run the following:
$ mysqldump -u dbuser -p --all-databases > Alldb-backup.sqlExporting may take some time to complete depending on your database size.
Optional: Export Using PHPMyAdmin
If you prefer to use PHPMyAdmin instead of the MySQL client tool, export your database as follows:
Log in to PHP MyAdmin
http://127.0.0.1/phpmyadminSelect your target database from the left navigation panel.
Click Export on the top navigation menu.
Choose your desired export method, Quick or Custom.
Click GO to start exporting and download the database file.
Keep note of the downloaded file location. You'll use the MySQL client tool to import the database to your Vultr managed database.
Import the saved database dump to a Vultr Managed Database
List files in your directory and confirm your exported database dump is available.
$ lsLog in to your managed database.
$ mysql -h dbhost.vultrdb.com -P <port> -u vultradmin -pPlease verify the following parameters on your import command if the connection fails.
-h – Defines the Vultr database hostname. -P – Sets the port number for connecting to the database host. -u – Sets the Database user that should import the database. -p – Turns on password authentication for the user to access the database server.
Create a new database with the same name as your old database.
CREATE DATABASE example;Exit the MySQL console.
EXITUsing the MySQL client, import the database to your managed database.
$ mysql -h host.vultrdb.com -P <port> -u vultradmin -p --database=example-database < localdb.sqlThis rebuilds the database selected with the
--database=flag.When the database importation is complete, log in to your managed database.
$ mysql -h dbhost.vultrdb.com -P <port> -u vultradmin -pSwitch to the database.
USE example-database;Show tables and verify that all data exists.
SHOW tables;Exit the console.
EXIT
Recreate MySQL Database Users
Log in to your local database server.
$ mysql -u dbuser -pView all MySQL users.
SELECT user,host FROM mysql.user;Show the privileges granted to each user on the database. For example,
dbuser.SHOW GRANTS FOR 'dbuser'@'localhost';Keep note of the privileges, or copy the listed GRANT statement in the output as below.
+--------------------------------------------------------------------------------------------------------------+ | Grants for dbuser@localhost | +--------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO `dbuser`@`localhost` IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' | | GRANT ALL PRIVILEGES ON `example-database`.* TO `dbuser`@`localhost` | +--------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.000 sec)Exit the MySQL console.
EXITLog in to your managed database.
$ mysql -h dbhost.vultrdb.com -P <port> -u vultradmin -pCreate all the database users.
CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'password';> When logged in to a Vultr Managed Database for MySQL, it's referred to as localhost.
Assign the user correct privileges to the database.
GRANT ALL PRIVILEGES ON `example-database`.* TO `dbuser`@`localhost`;Refresh MySQL's privileges.
FLUSH PRIVILEGES;Exit the console.
EXIT
Next Steps
You have migrated your local MySQL Database to a Vultr Managed Database for MySQL. Next, update the connection details in your applications to use the new database. Because you've created the same users and passwords in the new database, you should only need to change the database connection string to use the new managed database.