LoginSignup
3
0

【OCI クラウド移行ガイド】 Amazon RDS for MySQL を MySQL on Compute へ移行してみた~MySQL Shell 編~

Last updated at Posted at 2023-10-06

OCIクラウド移行ガイドとは

オンプレミスやAWSなど、複数のプラットフォームからOracle Cloud Infrastructureへの移行プロジェクトに取り組んでいるクラウドエンジニア(@araidon,@kazunishi,@yama6)による、OCI移行手順をまとめたシリーズ記事です。
各回、サンプルワークロードから対象サービスを取り上げ、移行手順をガイドいたします。
まとめ記事は以下になります。

移行するサービス:Amazon RDS for MySQL

今回、移行対象とするのはAmazon RDS for MySQLです。
MySQL Shellを利用したDumpの作成と、DumpファイルをComputeにインストールされたMySQL にLoadする手順をガイドします。

移行イメージ

image.png

前提条件

  • AWSとOCI、それぞれに接続するための踏み台・作業用サーバがパブリックサブネット上に構築されていること。
  • MySQLがインストールされているComputeは、踏み台・作業用サーバと同じインスタンスとします。
  • MySQLは既にインストール済みとします。

動作検証用データベース

本ブログでは、以下サンプルのemployees databaseを利用して移行前後のテーブル内容を確認します。

Amazon RDS for MySQLでの動作確認結果

動作確認結果(長文のため折りたたみ)

上記サンプルを元に作成したemployees DBの内容を確認します。

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| employees          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.02 sec)

mysql> use employees;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show tables;
+----------------------+
| Tables_in_employees  |
+----------------------+
| current_dept_emp     |
| departments          |
| dept_emp             |
| dept_emp_latest_date |
| dept_manager         |
| employees            |
| salaries             |
| titles               |
+----------------------+
8 rows in set (0.00 sec)

mysql> select * from employees limit 30;
+--------+------------+------------+-------------+--------+------------+
| emp_no | birth_date | first_name | last_name   | gender | hire_date  |
+--------+------------+------------+-------------+--------+------------+
|  10001 | 1953-09-02 | Georgi     | Facello     | M      | 1986-06-26 |
|  10002 | 1964-06-02 | Bezalel    | Simmel      | F      | 1985-11-21 |
|  10003 | 1959-12-03 | Parto      | Bamford     | M      | 1986-08-28 |
|  10004 | 1954-05-01 | Chirstian  | Koblick     | M      | 1986-12-01 |
|  10005 | 1955-01-21 | Kyoichi    | Maliniak    | M      | 1989-09-12 |
|  10006 | 1953-04-20 | Anneke     | Preusig     | F      | 1989-06-02 |
|  10007 | 1957-05-23 | Tzvetan    | Zielinski   | F      | 1989-02-10 |
|  10008 | 1958-02-19 | Saniya     | Kalloufi    | M      | 1994-09-15 |
|  10009 | 1952-04-19 | Sumant     | Peac        | F      | 1985-02-18 |
|  10010 | 1963-06-01 | Duangkaew  | Piveteau    | F      | 1989-08-24 |
|  10011 | 1953-11-07 | Mary       | Sluis       | F      | 1990-01-22 |
|  10012 | 1960-10-04 | Patricio   | Bridgland   | M      | 1992-12-18 |
|  10013 | 1963-06-07 | Eberhardt  | Terkki      | M      | 1985-10-20 |
|  10014 | 1956-02-12 | Berni      | Genin       | M      | 1987-03-11 |
|  10015 | 1959-08-19 | Guoxiang   | Nooteboom   | M      | 1987-07-02 |
|  10016 | 1961-05-02 | Kazuhito   | Cappelletti | M      | 1995-01-27 |
|  10017 | 1958-07-06 | Cristinel  | Bouloucos   | F      | 1993-08-03 |
|  10018 | 1954-06-19 | Kazuhide   | Peha        | F      | 1987-04-03 |
|  10019 | 1953-01-23 | Lillian    | Haddadi     | M      | 1999-04-30 |
|  10020 | 1952-12-24 | Mayuko     | Warwick     | M      | 1991-01-26 |
|  10021 | 1960-02-20 | Ramzi      | Erde        | M      | 1988-02-10 |
|  10022 | 1952-07-08 | Shahaf     | Famili      | M      | 1995-08-22 |
|  10023 | 1953-09-29 | Bojan      | Montemayor  | F      | 1989-12-17 |
|  10024 | 1958-09-05 | Suzette    | Pettey      | F      | 1997-05-19 |
|  10025 | 1958-10-31 | Prasadram  | Heyers      | M      | 1987-08-17 |
|  10026 | 1953-04-03 | Yongqiao   | Berztiss    | M      | 1995-03-20 |
|  10027 | 1962-07-10 | Divier     | Reistad     | F      | 1989-07-07 |
|  10028 | 1963-11-26 | Domenick   | Tempesti    | M      | 1991-10-22 |
|  10029 | 1956-12-13 | Otmar      | Herbst      | M      | 1985-11-20 |
|  10030 | 1958-07-14 | Elvis      | Demeyer     | M      | 1994-02-17 |
+--------+------------+------------+-------------+--------+------------+
30 rows in set (0.00 sec)

mysql> 
mysql> select count(*) from employees group by gender;
+----------+
| count(*) |
+----------+
|   179973 |
|   120051 |
+----------+
2 rows in set (0.21 sec)

mysql> select gender,count(*) from employees group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| M      |   179973 |
| F      |   120051 |
+--------+----------+
2 rows in set (0.24 sec)

mysql> show columns from employees;
+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no     | int           | NO   | PRI | NULL    |       |
| birth_date | date          | NO   |     | NULL    |       |
| first_name | varchar(14)   | NO   |     | NULL    |       |
| last_name  | varchar(16)   | NO   |     | NULL    |       |
| gender     | enum('M','F') | NO   |     | NULL    |       |
| hire_date  | date          | NO   |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+
6 rows in set (0.02 sec)

移行フロー

  1. OCIのObject Storageに、Dumpファイル保存用バケットを作成
  2. AWSの作業用EC2に、OCI接続用設定を実施
  3. AWSの作業用EC2に、MySQL Shellをインストール
  4. MySQL Shellを利用して、RDS for MySQLのDumpファイルを作成
  5. OCIの作業用Computeに、OCI接続用設定を実施
  6. OCIの作業用Computeに、MySQL Shellをインストール
  7. MySQL Shellを利用して、MySQLへのロードを実施

1. OCI Object Storage: Dumpファイル保存用バケットを作成

Dumpファイル保存用のバケットを作成します。

既存でWork用バケットがあれば、手順2のAWS EC2に進んでください。

OCIコンソール画面
→ 左上のハンバーガーメニュー
→ 「ストレージ」→ 「オブジェクト・ストレージとアーカイブ・ストレージ」→ 「バケット」を選択

「バケットの作成」を押下
image.png

項目名 設定
バケット名 (任意のバケット名)
デフォルト・ストレージ層 標準
暗号化 Oracle管理キーを使用した暗号化

その他、デフォルトのままで作成
image.png

バケットが作成された事を確認します。

2. AWSの作業用EC2に、OCI接続用設定を実施

EC2の作業ユーザーのホームディレクトリに、 ./.oci/config ファイルを作成します。

既に作成済みの場合は手順3に進んでください。

2-1. OCIコンソールから、APIキーを作成

OCIコンソール画面
→ 「ユーザー設定」を選択
image.png

左下、リソースの「APIキー」→「APIキーの追加」を選択
image.png

「秘密キーのダウンロード」を押下し、保存後に、「追加」を選択
image.png

構成ファイルのプレビューの内容をコピーします。
後で利用しますので、テキストエディタ等に保存してください。
image.png

2-2. 作業用EC2に、OCI構成ファイルを作成

作業用EC2にログインします。

ログイン後に、Touchコマンドで./.oci/config作成します。

touch ~/.oci/config

作成した .oci/config を編集します。

[DEFAULT]
user=(2-1でコピーした内容)
fingerprint=(2-1でコピーした内容)
tenancy=(2-1でコピーした内容)
region=(2-1でコピーした内容)
key_file=oci_api_key.pem

2-2. 作業用EC2に、OCI APIキーをアップロード

手順2-1でダウンロードした秘密キーをアップロードします。

  • ファイル名: oci_api_key.pemとします。
  • 保存先フォルダ: ~/.oci/
$ ls -l ~/.oci/
total 12
-rw------- 1 ubuntu ubuntu  301 Oct  1 08:26 config
-rw------- 1 ubuntu ubuntu 1704 Oct  1 08:26 oci_api_key.pem
$ 

3. AWSの作業用EC2に、MySQL Shellをインストール

既にインストール済の場合は手順4に進んでください。

以下公式の手順を参照してインストールします。

インストール出来ていることを確認

$ mysqlsh --version
mysqlsh   Ver 8.0.34 for Linux on x86_64 - for MySQL 8.0.34 (MySQL Community Server (GPL))
$ 

4. EC2にて、MySQL Shellを利用して、RDS for MySQLのDumpファイルを作成

4-1. EC2にて、MySQL Shellを実行

$ mysqlsh

MySQL Shell 8.0.34

Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help; '\quit' to exit.

4-2. MySQL Shellから、対象のRDS for MySQLのエンドポイントに接続

 \connect RDSのユーザ名@rdsのエンドポイント

途中でRDSユーザのパスワードを聞かれます。

 MySQL  JS > \connect xxxxxx@xxxxxx.xxxxxxxxx.ap-northeast-1.rds.amazonaws.com

Creating a session to 'xxxxxx@xxxxxx.xxxxxxxxx.ap-northeast-1.rds.amazonaws.com'
Please provide the password for 'xxxxxx@xxxxxx.xxxxxxxxx.ap-northeast-1.rds.amazonaws.com': ********
Save password for 'xxxxxx@xxxxxx.xxxxxxxxx.ap-northeast-1.rds.amazonaws.com'? [Y]es/[N]o/Ne[v]er (default No): Y
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 319
Server version: 8.0.33 Source distribution
No default schema selected; type \use <schema> to set one.

 MySQL  xxxxxx@xxxxxx.xxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306 ssl  JS > 

4-3. RDS for MySQLのDumpファイルをOCIのObject Storageに作成

以下のコマンドでDumpファイルを作成

> util.dumpInstance("outputurl", {osBucketName: "bucketname", osNamespace: "namespace", threads:1})
項目 説明
outputurl バケット直下に作成される親ディレクトリ名。任意でOK
osBucketName OCIで作成したObject Storageのバケット名を入力
osNamespace OCIテナントのNameSpace名を入力。OCIコンソールの右上人型アイコンのテナンシをクリックすることで、オブジェクト・ストレージ・ネームスペースが確認出来ます。
threads EC2のvCPU数を指定

項目の詳細は以下公式サイトを参照してください。

実行結果

 MySQL  xxxxxx@xxxxxx.xxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306 ssl  JS > util.dumpInstance("<outputurl>", {osBucketName: "<bucketname>", osNamespace: "<namespace>", threads:1, consistent: true})
NOTE: Backup lock is not available to the account 'admin'@'%' and DDL changes will not be blocked. The dump may fail with an error if schema changes are made while dumping.

(中略)

Dump duration: 00:00:07s                                                                      
Total duration: 00:00:08s                                                                     
Schemas dumped: 1                                                                             
Tables dumped: 6                                                                              
Uncompressed data size: 141.50 MB                                                             
Compressed data size: 36.82 MB                                                                
Compression ratio: 3.8                                                                        
Rows written: 3919015                                                                         
Bytes written: 36.82 MB                                                                       
Average uncompressed throughput: 18.18 MB/s                                                   
Average compressed throughput: 4.73 MB/s                                                      
 MySQL  xxxxxx@xxxxxx.xxxxxxxxx.ap-northeast-1.rds.amazonaws.com:3306 ssl  JS > 

OCIのバケットに作成された事を確認
image.png

5. OCIの作業用Computeに、OCI接続用設定を実施

OCIの作業ユーザーのホームディレクトリに、 ./.oci/config ファイルを作成します。

既に作成済みの場合は手順6に進んでください。

以下手順5-1,5-2は、手順2-1,2-2で実施した内容と全く同じ内容です。
作成するConfigファイル・APIキー共に全く同じ内容でOKです。(key-fileのPATHには注意)

5-1. OCIコンソールから、APIキーを作成

OCIコンソール画面
→ 「ユーザー設定」を選択
image.png

左下、リソースの「APIキー」→「APIキーの追加」を選択
image.png

「秘密キーのダウンロード」を押下し、保存後に、「追加」を選択
image.png

構成ファイルのプレビューの内容をコピーします。
後で利用しますので、テキストエディタ等に保存してください。
image.png

5-2. 作業用EC2に、OCI構成ファイルを作成

作業用EC2にログインします。

ログイン後に、Touchコマンドで./.oci/config作成します。

touch ~/.oci/config

作成した .oci/config を編集します。

[DEFAULT]
user=(5-1でコピーした内容)
fingerprint=(5-1でコピーした内容)
tenancy=(5-1でコピーした内容)
region=(5-1でコピーした内容)
key_file=oci_api_key.pem

5-2. 作業用EC2に、OCI APIキーをアップロード

手順5-1(2-1)でダウンロードした秘密キーをアップロードします。

  • ファイル名: oci_api_key.pemとします。
  • 保存先フォルダ: ~/.oci/
$ ls -l ~/.oci/
total 8
-rw-r--r--. 1 opc opc  298 Oct  6 07:50 config
-rw-r--r--. 1 opc opc 1704 Oct  6 07:39 oci_api_key.pem
$

6. OCIの作業用Computeに、MySQL Shellをインストール

既にインストール済の場合は手順7に進んでください。

以下公式の手順を参照してインストールします。

インストール出来ていることを確認

$ mysqlsh --version
mysqlsh   Ver 8.0.34 for Linux on x86_64 - for MySQL 8.0.34 (MySQL Community Server (GPL))
$ 

7. Computeにて、MySQL Shellを利用して、MySQLへのロードを実施

7-1. Computeにて、MySQL Shellを実行

$ mysqlsh

MySQL Shell 8.0.34

Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help; '\quit' to exit.

7-2. MySQL Shellから、対象のMySQLのエンドポイントに接続(今回はLocalhost)

 \connect <ユーザー名>@localhost:3306

途中でユーザのパスワードを聞かれます。

 MySQL  JS > \connect root@localhost:3306

Creating a session to 'root@localhost:3306'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 25
Server version: 8.0.34 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.

MySQL  localhost:3306 ssl  JS >

7-3. MySQLへのロードを実施

以下のコマンドでLoadを実施

> util.loadDump("<url>", {osBucketName: "<bucketname>", osNamespace: "<namespace>", threads:1})
項目 説明
url Dumpファイルが保存されている親ディレクトリ名。手順4-3で指定したoutputurl。
osBucketName OCIで作成したObject Storageのバケット名を入力
osNamespace OCIテナントのNameSpace名を入力。OCIコンソールの右上人型アイコンのテナンシをクリックすることで、オブジェクト・ストレージ・ネームスペースが確認出来ます。
threads Computeの OCPU数を指定

項目の詳細は以下公式サイトを参照してください。

実行結果

 MySQL  localhost:3306 ssl  JS > util.loadDump("<url>", {osBucketName: "<bucketname>", osNamespace: "<namespace>", threads:1})
Loading DDL and Data from OCI ObjectStorage bucket=mds_fromaws, prefix='rdsdumpforcompute' using 1 thread.
Opening dump...
Target is MySQL 8.0.34. Dump was produced from MySQL 8.0.3

(中略)

100% (141.50 MB / 141.50 MB), 4.13 MB/s, 6 / 6 tables done
Recreating indexes - done       
6 chunks (3.92M rows, 141.50 MB) for 6 tables in 1 schemas were loaded in 29 sec (avg throughput 5.16 MB/s)
0 warnings were reported during the load.                 
 MySQL  localhost:3306 ssl  JS >
 

loadDumpコマンド時に、以下エラーメッセージが発生する場合は、MySQL インスタンスの local_infile システム変数のグローバル設定を ON にする必要があります。

ERROR: The 'local_infile' global system variable must be set to ON in the target server, after the server is verified to be trusted.
Util.loadDump: local_infile disabled in server (MYSQLSH 53025)

動作確認

MySQL にログインし、ロードされていることを確認します。

動作確認結果(長文のため折りたたみ)

employees DBが作成されていることと、Amazon RDS for MySQLと同じ内容になっていることを確認します。

$ mysql -h localhost -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| employees          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 
mysql> 
mysql> 
mysql> 
mysql> use employees;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 
mysql> 
mysql> 
mysql> 
mysql> show tables;
+----------------------+
| Tables_in_employees  |
+----------------------+
| current_dept_emp     |
| departments          |
| dept_emp             |
| dept_emp_latest_date |
| dept_manager         |
| employees            |
| salaries             |
| titles               |
+----------------------+
8 rows in set (0.00 sec)

mysql> 
mysql> 
mysql> 
mysql> select * from employees limit 30;
+--------+------------+------------+-------------+--------+------------+
| emp_no | birth_date | first_name | last_name   | gender | hire_date  |
+--------+------------+------------+-------------+--------+------------+
|  10001 | 1953-09-02 | Georgi     | Facello     | M      | 1986-06-26 |
|  10002 | 1964-06-02 | Bezalel    | Simmel      | F      | 1985-11-21 |
|  10003 | 1959-12-03 | Parto      | Bamford     | M      | 1986-08-28 |
|  10004 | 1954-05-01 | Chirstian  | Koblick     | M      | 1986-12-01 |
|  10005 | 1955-01-21 | Kyoichi    | Maliniak    | M      | 1989-09-12 |
|  10006 | 1953-04-20 | Anneke     | Preusig     | F      | 1989-06-02 |
|  10007 | 1957-05-23 | Tzvetan    | Zielinski   | F      | 1989-02-10 |
|  10008 | 1958-02-19 | Saniya     | Kalloufi    | M      | 1994-09-15 |
|  10009 | 1952-04-19 | Sumant     | Peac        | F      | 1985-02-18 |
|  10010 | 1963-06-01 | Duangkaew  | Piveteau    | F      | 1989-08-24 |
|  10011 | 1953-11-07 | Mary       | Sluis       | F      | 1990-01-22 |
|  10012 | 1960-10-04 | Patricio   | Bridgland   | M      | 1992-12-18 |
|  10013 | 1963-06-07 | Eberhardt  | Terkki      | M      | 1985-10-20 |
|  10014 | 1956-02-12 | Berni      | Genin       | M      | 1987-03-11 |
|  10015 | 1959-08-19 | Guoxiang   | Nooteboom   | M      | 1987-07-02 |
|  10016 | 1961-05-02 | Kazuhito   | Cappelletti | M      | 1995-01-27 |
|  10017 | 1958-07-06 | Cristinel  | Bouloucos   | F      | 1993-08-03 |
|  10018 | 1954-06-19 | Kazuhide   | Peha        | F      | 1987-04-03 |
|  10019 | 1953-01-23 | Lillian    | Haddadi     | M      | 1999-04-30 |
|  10020 | 1952-12-24 | Mayuko     | Warwick     | M      | 1991-01-26 |
|  10021 | 1960-02-20 | Ramzi      | Erde        | M      | 1988-02-10 |
|  10022 | 1952-07-08 | Shahaf     | Famili      | M      | 1995-08-22 |
|  10023 | 1953-09-29 | Bojan      | Montemayor  | F      | 1989-12-17 |
|  10024 | 1958-09-05 | Suzette    | Pettey      | F      | 1997-05-19 |
|  10025 | 1958-10-31 | Prasadram  | Heyers      | M      | 1987-08-17 |
|  10026 | 1953-04-03 | Yongqiao   | Berztiss    | M      | 1995-03-20 |
|  10027 | 1962-07-10 | Divier     | Reistad     | F      | 1989-07-07 |
|  10028 | 1963-11-26 | Domenick   | Tempesti    | M      | 1991-10-22 |
|  10029 | 1956-12-13 | Otmar      | Herbst      | M      | 1985-11-20 |
|  10030 | 1958-07-14 | Elvis      | Demeyer     | M      | 1994-02-17 |
+--------+------------+------------+-------------+--------+------------+
30 rows in set (0.00 sec)

mysql> 
mysql> 
mysql> 
mysql> select gender,count(*) from employees group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| M      |   179973 |
| F      |   120051 |
+--------+----------+
2 rows in set (0.18 sec)

mysql> 
mysql> 
mysql> show columns from employees;
+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no     | int           | NO   | PRI | NULL    |       |
| birth_date | date          | NO   |     | NULL    |       |
| first_name | varchar(14)   | NO   |     | NULL    |       |
| last_name  | varchar(16)   | NO   |     | NULL    |       |
| gender     | enum('M','F') | NO   |     | NULL    |       |
| hire_date  | date          | NO   |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

mysql> 
mysql> 
mysql> exit
Bye

まとめ

MySQL Shellを利用して簡単に移行することができました。
Amazon EC2や、OCI Computeから、OCI Object Storageにつなぐ設定は慣れるまで時間がかかるかもしれません。

次回は、Amazon Auroraからの移行を試してみたいと思います。

参考

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0