mysql> create table gift( id int auto_increment, name varchar(255), description text, created_at datetime not null, primary key(id) )engine=InnoDB; Query OK, 0 rows affected (0.02 sec)
これをSpider化していきますが、今回は定期的にデータを投入しながらSpider化を行ってみます。 まず、以下のようなシェルを実行し、常にデータの投入がされている状態をつくります。
$ vi insert2gift.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
i=0 | |
while ((1)) | |
do | |
name=$(uuidgen | tr -d '-') | |
desc=$(mkpasswd -l 1000) | |
mysql -u xxxxxxx cloudpack -pxxxxxxxxxxxxx -e "INSERT INTO gift(name, description,created_at) VALUES('${name}', '${desc}', NOW());" | |
echo "." | |
sleep 1 | |
done; | |
exit 1 |
$ sh insert2gift.sh ................................それではVPをつかって無停止でSpiderに移行を行なってみたいと思います。 基本的には、前回と同様です。 切り替え用のダミーと2つのデータノードをもつ新規Spiderテーブル、それらを束ねるVPテーブルを用意します。
データノードの設定
mysql> GRANT ALL PRIVILEGES ON *.* TO 'xxxxxxxxxxxxxx'@localhost IDENTIFIED BY 'xxxxxxxxxxxxxx'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'xxxxxxxxxxxxxx'@'%' IDENTIFIED BY 'xxxxxxxxxxxxxx'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'xxxxxxxxxxxxxx'@'123.123.123.123' IDENTIFIED BY 'xxxxxxxxxxxxxx'; Query OK, 0 rows affected (0.00 sec) mysql> use cloudpack; Database changed mysql> create table gift( id int auto_increment, name varchar(255), description text, created_at datetime not null, primary key(id) )engine=InnoDB; Query OK, 0 rows affected (0.00 sec)
SpiderノードのSpider、ダミー、VPテーブルの設定
mysql> create table gift_new( id int auto_increment, name varchar(255), description text, created_at datetime not null, primary key(id) ) engine = Spider DEFAULT CHARSET=utf8 CONNECTION ' table "gift", user "xxxxxxxxxxxxx", password "xxxxxxxxxxxxx" ' PARTITION BY LIST(MOD(id, 2)) ( PARTITION hostb VALUES IN (0) comment 'host "111.111.111.111", port "3306"', PARTITION hostc VALUES IN (1) comment 'host "222.222.222.222", port "3306"' ); Query OK, 0 rows affected (0.02 sec) mysql> create table gift_dummy like gift; Query OK, 0 rows affected (0.01 sec) mysql> create table gift_vp( id int auto_increment, name varchar(255), description text, created_at datetime not null, primary key(id) )engine=vp comment 'table_name_list "gift_dummy gift_new", cit "2", cil "2", ctm "1", ist "1", zru "1"'; Query OK, 0 rows affected (0.03 sec)この時点では、以下のイメージのような構成になっています。
テーブルのリネーム
ここも前回同様テーブルのリネームを行い、接続先をVPテーブルに向けます。
mysql> rename table gift_dummy to gift_delete, gift to gift_dummy, gift_vp to gift; Query OK, 0 rows affected (0.03 sec)
データのコピー
ここも前回同様です。
mysql> select vp_copy_tables('gift', 'gift_dummy', 'gift_new'); Query OK, 0 rows affected (0.01 sec)
テーブルの再リネーム
コピーが完了したら、テーブルのリネームを再度行い、移行先のSpiderテーブルをgiftにします。
mysql> rename table gift to gift_vp, gift_new to gift; Query OK, 0 rows affected (0.01 sec)
不要テーブルの削除
最後に、必要のなくなったテーブルを削除します。
mysql> drop table gift_dummy, gift_vp, gift_delete; Query OK, 0 rows affected (0.00 sec)
これで、移行が完了しました。 それでは、実際にテーブルの内容を見てみましょう。
host A
mysql> select * from gift order by id; +------+----------------------------------+----------------------------------+---------------------+ | id | name | description | created_at | +------+----------------------------------+----------------------------------+---------------------+ | 1 | d84c7d13d56e48999f6e42396bb0d6b8 | 57b6f87681df4141953b63cd6ee74... | 2011-09-21 22:35:23 | | 2 | fc52fbdee1904e40a92711bc3ff5b53b | 4fe1116428e142369ab419d603a8b... | 2011-09-21 22:35:23 | | 3 | 936917c0e7b246b380573654c80863e1 | 08cf01342f1149b5a7fa15f2df5b6... | 2011-09-21 22:35:24 | | 4 | 5d630544409e4ef98ece5b489cd5aff5 | c3c1848832d04b69bc9f9c1bd7249... | 2011-09-21 22:35:23 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 5429 | 0462eaf7646b4855a993a49128ba3f4d | 0d07cdbac3c14723b25bc1a1bc74a... | 2011-09-22 01:07:42 | | 5430 | b67fdb0b633c40829dd812c0358f5ccb | 85a310e34668427ab392b07fe36f8... | 2011-09-22 01:07:49 | | 5431 | 431a0be6d0d94223bad8405d0011560e | f78b63b0ed5c42d0a947dc3db6bcb... | 2011-09-22 01:07:56 | | 5432 | 4a0f9c4dcbaa42a99aeb014232091551 | 9ceb2dce039e49268280560631578... | 2011-09-22 01:08:02 | +------+----------------------------------+----------------------------------+---------------------+ 5007 rows in set (0.06 sec)
host B
mysql> select * from gift order by id; +------+----------------------------------+----------------------------------+---------------------+ | id | name | description | created_at | +------+----------------------------------+----------------------------------+---------------------+ | 1 | d84c7d13d56e48999f6e42396bb0d6b8 | 57b6f87681df4141953b63cd6ee74... | 2011-09-21 22:35:23 | | 3 | 936917c0e7b246b380573654c80863e1 | 08cf01342f1149b5a7fa15f2df5b6... | 2011-09-21 22:35:24 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 5429 | 0462eaf7646b4855a993a49128ba3f4d | 0d07cdbac3c14723b25bc1a1bc74a... | 2011-09-22 01:07:42 | | 5431 | 431a0be6d0d94223bad8405d0011560e | f78b63b0ed5c42d0a947dc3db6bcb... | 2011-09-22 01:07:56 | +------+----------------------------------+----------------------------------+---------------------+ 2503 rows in set (0.01 sec)
host C
mysql> select * from gift order by id; +------+----------------------------------+----------------------------------+---------------------+ | id | name | description | created_at | +------+----------------------------------+----------------------------------+---------------------+ | 2 | fc52fbdee1904e40a92711bc3ff5b53b | 4fe1116428e142369ab419d603a8b... | 2011-09-21 22:35:23 | | 4 | 5d630544409e4ef98ece5b489cd5aff5 | c3c1848832d04b69bc9f9c1bd7249... | 2011-09-21 22:35:23 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 5430 | b67fdb0b633c40829dd812c0358f5ccb | 85a310e34668427ab392b07fe36f8... | 2011-09-22 01:07:49 | | 5432 | 4a0f9c4dcbaa42a99aeb014232091551 | 9ceb2dce039e49268280560631578... | 2011-09-22 01:08:02 | +------+----------------------------------+----------------------------------+---------------------+ 2504 rows in set (0.00 sec)
このように、元々入っていたデータと、移行中に投入されたデータがきれいにシャーディングされていることがわかります。
本日はここまで。