MySQL database migration by using Python
You can migrate MySQL databases from one server to another by using the MySQL database migration script. This script is applicable to any platform:
- IBM Cloud® classic infrastructure to IBM Cloud VPC
- On-premises to IBM Cloud VPC
- Other cloud service providers to IBM Cloud VPC
Prerequisites
Review the following prerequisites before you begin your migration:
- Set up an IBM Cloud Object Storage bucket.
- Make sure that you have write access to the Object Storage bucket.
- Make sure that you have a connection to the source and target server from your system.
- Make sure that Python3 (version 3.0) and Pip3 are installed on your system.
Migration overview diagram
Clone and run the database migration script
Complete the following steps to clone and run the database migration script:
-
Run the following command to clone the public GitHub repository.
git clone https://github.com/IBM-Cloud/vpc-migration-tools.git
-
Go to the
mysql
folder.cd vpc-migration-tools/db-migration/mysql
-
Install the prerequisites modules for Python by running the following commands:
pip3 install -U pip setuptools
pip3 install -r requirements.txt
setuptools
facilitate packaging Python projects by enhancing the Python standard library distutils. Therequirements.txt
files install the Python libraries that are required in the script. -
Run the database migration script:
python3 db_migration.py
Provide migration details
After you run the database migration script, you need to provide the details for the following parameters:
- Object Storage
- Source server
- Source server database
- Target server
- Target server database
Object Storage details
The Object Storage bucket acts as centralized storage for the source and the target server. By using the s3fs
utility, the bucket is mounted as a file system on both the source and target server. The source uses the bucket to
store the database backup, and the target uses the bucket to retrieve the database backup, which minimizes the migration duration.
Complete the following steps to mount the Object Storage bucket:
- Enter the Object Storage bucket name; for example,
my-db-bucket
. The bucket must already be provisioned. - Enter the Object Storage endpoint, which is the location of the bucket; for example,
https://s3.dal.us.cloud-object-storage.appdomain.cloud
. For more information, see Endpoints and storage locations. - Enter your IBM Cloud API key. For more information, see Creating an IBM Cloud API key.
Source server details
After you mount your Object Storage bucket, you need to provide your source server details:
- Enter the source server IP address or hostname. The source server is the MySQL database that needs to be migrated.
- Enter the source server login credentials. The script needs to authenticate with the username and password. Make sure that the user privileges are equivalent to the
root
.
Source server database details
- Enter the source server MySQL connection details, such as the username for the MySQL application, which is
root
by default. - Enter your password for MySQL, which authenticates you to perform actions on the MySQL database that you want to migrate.
- Enter the database name that you want to migrate.
Target server details
After you provide your source server and source server database details, you need to provide your target server details:
- Enter the target server IP address or hostname. The target server is where the MySQL database is to be migrated.
- Enter the target server login credentials. Similarly, for the source server, the script needs to be authenticated with the username and password. Make sure that the user privilege is equivalent to the
root
.
Target server database details
- Enter the target server MySQL connection details, such as the username for the MySQL application, which is
root
by default. - Enter your password for MySQL, which authenticates you to perform actions to restore the database.
- Enter the source server database name.
- Enter the target database name. By default, this script fetches the database name from the source database name. If you want to have a different database name for migration, then you can provide the input per your requirement.
- After a successful database migration, a "complete" message is displayed.