0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PolarDB provisioned instanceからServerlessにupgrade

Posted at

1. 目的と前提条件

コスト削減のために、開発テスト環境のPolarDB provisioned instanceをServerlessに変更してみたいです。
以前はAlibaba CloudのDTSサービスを利用して、データをProvisioned instanceからServerless instanceに移行するという方法がありました。DTSサービスは選択肢が多すぎて、かなりハードルが高かったです。
最近は簡単な方法がリリースされたので、一通りテストしてみます。
この方法も内部ではDTSが利用されていますが、一からDTSタスクを作成する必要がなく、自動的にDTSタスクを作成してくれるので、かなりハードルが下がりました。
image.png

PolarDB Serverlessとは:

DTSとは:

詳細な手順はこちらのドキュメントをご参考ください。

2. 環境準備

2.1 PolarDB provisioned instanceを作成

2.2 ECSを立ち上げて、必要なコマンドのインストールとsample dataの準備

  • ECSの作成については以下を参考:

  • 以下のサンプルデータを利用:

$ sudo apt update
$ apt install unzip
$ wget https://github.com/datacharmer/test_db/archive/refs/heads/master.zip

$ unzip master.zip
$ ls
  master.zip
  test_db-master
$ sudo apt install mysql-client-core-8.0 -y
  • sample dataをPolarDB instanceにloadする
$ mysql -h ***.rwlb.japan.rds.aliyuncs.com < employees.sql -u dennis -p
Enter password: 
INFO
CREATING DATABASE STRUCTURE
INFO
storage engine: InnoDB
INFO
LOADING departments
INFO
LOADING employees
INFO
LOADING dept_emp
INFO
LOADING dept_manager
INFO
LOADING titles
INFO
LOADING salaries
data_load_time_diff
NULL


$  mysql -h ***.rwlb.japan.rds.aliyuncs.com -u dennis -p
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| __recycle_bin__    |
| employees          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

mysql> use employees;
mysql> show tables;
+----------------------+
| Tables_in_employees  |
+----------------------+
| current_dept_emp     |
| departments          |
| dept_emp             |
| dept_emp_latest_date |
| dept_manager         |
| employees            |
| salaries             |
| titles               |
+----------------------+
  • DTSで増分データを同期するため、PolarDBのbinlogを有効にする必要があります。
    image.png

2.3 新しいServerless instanceを作成

  • PolarDBの購入画面で既存のPolarDB instanceから新しいServerless instanceのupdateを行う
    image.png
    ※必要に応じてServerless instanceのnode数やPCU範囲を選択
    image.png
  • 生成されたServerless instance(DTSタスクIDも確認できる。クリックするとDTSの画面に移動できる)
    image.png
  • DTS画面上でDTSタスクの詳細が確認できる。(確認は問題ありませんが、タスクの変更はしてはいいけません)
    image.png

2.4 realtime同期の動きを確認

  • soure database(PolarDB providsioned instance)上で新しいrecordをinsertしてみる

    mysql> select count(*) from employees;
    +----------+
    | count(*) |
    +----------+
    |   300024 |
    +----------+
    
    mysql> select * from employees order by emp_no desc limit 5;
    +--------+------------+------------+-----------+--------+------------+
    | emp_no | birth_date | first_name | last_name | gender | hire_date  |
    +--------+------------+------------+-----------+--------+------------+
    | 499999 | 1958-05-01 | Sachin     | Tsukuda   | M      | 1997-11-30 |
    | 499998 | 1956-09-05 | Patricia   | Breugel   | M      | 1993-10-13 |
    | 499997 | 1961-08-03 | Berhard    | Lenart    | M      | 1986-04-21 |
    | 499996 | 1953-03-07 | Zito       | Baaz      | M      | 1990-09-27 |
    | 499995 | 1958-09-24 | Dekang     | Lichtner  | F      | 1993-01-12 |
    +--------+------------+------------+-----------+--------+------------+
    
    
    mysql> insert into employees (birth_date,first_name,last_name,gender,hire_date) values("2000-01-01","tanaka","yosuke","M", "2024-01-01");
    
    mysql> select count(*) from employees;
    +----------+
    | count(*) |
    +----------+
    |   300025 |
    +----------+
    
    mysql> select * from employees order by emp_no desc limit 5;
    +--------+------------+------------+-----------+--------+------------+
    | emp_no | birth_date | first_name | last_name | gender | hire_date  |
    +--------+------------+------------+-----------+--------+------------+
    | 499999 | 1958-05-01 | Sachin     | Tsukuda   | M      | 1997-11-30 |
    | 499998 | 1956-09-05 | Patricia   | Breugel   | M      | 1993-10-13 |
    | 499997 | 1961-08-03 | Berhard    | Lenart    | M      | 1986-04-21 |
    | 499996 | 1953-03-07 | Zito       | Baaz      | M      | 1990-09-27 |
    | 499995 | 1958-09-24 | Dekang     | Lichtner  | F      | 1993-01-12 |
    +--------+------------+------------+-----------+--------+------------+
    
    mysql> select * from employees where first_name = "tanaka";
    +--------+------------+------------+-----------+--------+------------+
    | emp_no | birth_date | first_name | last_name | gender | hire_date  |
    +--------+------------+------------+-----------+--------+------------+
    |      0 | 2000-01-01 | tanaka     | yosuke    | M      | 2024-01-01 |
    +--------+------------+------------+-----------+--------+------------+
    
  • target database(PolarDB Serverlessのinstance)上でデータがrealtime同期されていることが確認できた

    mysql> select count(*) from employees;
    +----------+
    | count(*) |
    +----------+
    |   300025 |
    +----------+
    
    mysql> select * from employees where emp_no = 0;
    +--------+------------+------------+-----------+--------+------------+
    | emp_no | birth_date | first_name | last_name | gender | hire_date  |
    +--------+------------+------------+-----------+--------+------------+
    |      0 | 2000-01-01 | tanaka     | yosuke    | M      | 2024-01-01 |
    +--------+------------+------------+-----------+--------+------------+
    

2.5 databaseの切り替え

endpoint自動的に交換してくれるので、わざわざアプリケーション側のDatabaseへの接続endpointを修正する必要もありません。

  • 以下のswitch overボタンをクリック
    image.png
    image.png
    image.png
    image.png
    image.png

2.6 upgradeを終わらせる(DTSタスクを自動的に削除してくれ)

image.png

  • 新databaseのbinlogを無効にするかどうかを選択(無効にするとinstanceは自動的に再起動することになる。すぐ再起動したくない場合は、別途binlogを無効化にすればよい。)
    image.png

Binlogをオンにすると、SELECTクエリーのパフォーマンスには影響しませんが、書き込み更新(INSERT、UPDATE、DELETE)のパフォーマンスに影響します。
PolarDBではDTSによってデータを他のdatabaseに同期するためにbinlogが利用されます。それ以外はbinlog使わないので、無効にしたほうが無難でしょう。

image.png

2.7 provisioned instanceを削除

provisioned instanceはまだ残っている。最後に確認し、とくに問題なければ、手動でこのinstaceを削除すればよいでしょう。
image.png

3. まとめ

  1. 手動でDTSを利用するよりはだいぶ操作が楽になります
  2. endpointまで自動的にきりかえてくれるので、とてもありがたいですね
  3. この方法ではDTS同期の費用も無料になります
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?