1. 程式人生 > >讓天下沒有難用的資料庫 » RDS MySql支援online ddl

讓天下沒有難用的資料庫 » RDS MySql支援online ddl

在日常和客戶溝通的過程中發現,他們在做mysql ddl變更的時候由於MySql本身的缺陷不支援online ddl,導致他們的業務不得不hang住一會兒,表越大,時間影響越長,所以期待有更好的解決方法;有些使用者也想了一些方法,比如通過主備切換的方法,先在備庫進行ddl,然後在通過主備切換到原主庫進行ddl,但由於RDS對外提供給使用者的是一個dns加port,所以後端的主備對使用者是透明的,此方法行不通。其實在開源社群中已經有比較成熟的方法,那就是percona的pt-online-schema-change工具是其中之一,下面通過測試主要了解該工具的可靠性以及存在的問題,是否在RDS上支援。

原理:
線上修改表結構的工具,基本處理方式類似,以下對pt-online-schema-change工具的工作原理進行分析:
1、如果存在外來鍵,根據alter-foreign-keys-method引數的值,檢測外來鍵相關的表,做相應設定的處理。
2、建立一個新的表,表結構為修改後的資料表,用於從源資料表向新表中匯入資料。
3、建立觸發器,用於記錄從拷貝資料開始之後,對源資料表繼續進行資料修改的操作記錄下來,用於資料拷貝結束後,執行這些操作,保證資料不會丟失。
4、拷貝資料,從源資料表中拷貝資料到新表中。
5、修改外來鍵相關的子表,根據修改後的資料,修改外來鍵關聯的子表。
6、rename源資料表為old表,把新表rename為源表名,並將old表刪除。
7、刪除觸發器。
3.RDS支援:
a.在現有的使用者許可權基礎上開通replication slave許可權
[

[email protected] bin]# ./pt-online-schema-change –u=test123 –host=test.mysql.rds.aliyuncs.com –port=3306 –password=hell05a –alter=”add column is_sign_1 int(11)” D=test,t=t –execute
DBD::mysql::db selectall_arrayref failed: Access denied; you need the REPLICATION SLAVE privilege for this operation [for Statement “SHOW SLAVE HOSTS”] at ./pt-online-schema-change line 4051.

grant REPLICATION SLAVE ON *.* TO ‘test123’@’%’;

b.表中含有主鍵或者唯一索引
[[email protected] bin]# ./pt-online-schema-change –u=test123 –host=test.mysql.rds.aliyuncs.com –port=3306 –password=hell05a –alter=”add column is_sign_1 int(11)” D=test,t=t –execute
Cannot chunk the original table `test`.`t`: There is no good index and the table is oversized. at ./pt-online-schema-change line 5365.

4.測試:
在測試的過程中,測試插入資料,刪除資料,更新資料,觀察是否阻塞,同時對錶進行不斷的壓測:
delimiter ;;
CREATE
PROCEDURE e_test()
BEGIN
WHILE 1 DO
insert into t(name,gmt_create,name2) values(‘xxx’,now(),’xxx’);
END WHILE;
END;
;;

call e_test();

mysql> insert into test(gmt_create) values(now());
Query OK, 1 row affected (0.12 sec)

mysql> delete from test where id=1;
Query OK, 1 row affected (0.01 sec)

mysql> update test set gmt_Create=now() where id=2;
Query OK, 1 row affected (0.30 sec)
Rows matched: 1 Changed: 1 Warnings: 0

新增欄位:
./pt-online-schema-change –u=test123 –host=test.mysql.rds.aliyuncs.com –port=3306 –password=hell05a –alter=”add column is_sign_2 int(11)” D=qianyi,t=test –execute
新增索引:
./pt-online-schema-change –u=test123 –host=test.mysql.rds.aliyuncs.com –port=3306 –password=hell05a –alter=”add index ind_gmt_create(gmt_create)” D=qianyi,t=test –execute
修改欄位:
./pt-online-schema-change –u=test123 –host=test.mysql.rds.aliyuncs.com –port=3306 –password=hell05a –alter=”modify column is_sign_2 bigint” D=qianyi,t=test –execute
5.結果:
[[email protected] bin]# ./pt-online-schema-change –u=test123 –host=test.mysql.rds.aliyuncs.com –port=3306 –password=hell05a –alter=”add column is_sign_1 int(11)” D=qianyi,t=t –execute
Altering `qianyi`.`test`…
Creating new table…
Created new table qianyi._test_new OK.
Altering new table…
Altered `qianyi`.`_test_new` OK.
Creating triggers…
Created triggers OK.
Copying approximately 8388968 rows…
Copying `qianyi`.`test`: 52% 00:26 remain
Copied rows OK.
Swapping tables…
Swapped original and new tables OK.
Dropping old table…
Dropped old table `qianyi`.`_test_old` OK.
Dropping triggers…
Dropped triggers OK.
Successfully altered `qianyi`.`test`.
[[email protected] bin]#
[[email protected] bin]# ./pt-online-schema-change –u=test123 –host=test.mysql.rds.aliyuncs.com –port=3306 –password=hell05a –alter=”modify column is_sign_2 bigint” D=qianyi,t=t –execute
Altering `qianyi`.`test`…
Creating new table…
Created new table qianyi._test_new OK.
Altering new table…
Altered `qianyi`.`_test_new` OK.
Creating triggers…
Created triggers OK.
Copying approximately 8388885 rows…
Copying `qianyi`.`t`: 53% 00:25 remain
Copied rows OK.
Swapping tables…
Swapped original and new tables OK.
Dropping old table…
Dropped old table `qianyi`.`_test_old` OK.
Dropping triggers…
Dropped triggers OK.
Successfully altered `qianyi`.`test`.

[[email protected] bin]# ./pt-online-schema-change –u=test123 –host=test.mysql.rds.aliyuncs.com –port=3306 –password=hell05a –alter=”add index ind_gmt_create(gmt_create)” D=qianyi,t=t –execute
Altering `qianyi`.`test`…
Creating new table…
Created new table qianyi._test_new OK.
Altering new table…
Altered `qianyi`.`_test_new` OK.
Creating triggers…
Created triggers OK.
Copying approximately 8388785 rows…
Copying `qianyi`.`test`: 41% 00:42 remain
Copying `qianyi`.`test`: 83% 00:12 remain
Copied rows OK.
Swapping tables…
Swapped original and new tables OK.
Dropping old table…
Dropped old table `qianyi`.`_test_old` OK.
Dropping triggers…
Dropped triggers OK.
Successfully altered `qianyi`.`test`.

6結論:
1.RDS開通使用者帳號replication slave許可權支援pt-online-ddl,使用者的表必須要有主鍵或者唯一索引;
2.當業務量較大時,修改操作會等待沒有資料修改後,執行最後的rename操作。因此,在修改表結構時,應該儘量選擇在業務相對空閒時,至少修改表上的資料操作較低時,執行較為妥當。

附:pt-online-schema-change