1. 程式人生 > 實用技巧 >AWS多元複製到EC2機器

AWS多元複製到EC2機器

需求:把多個rds同步到一個rds,實現跨例項資料查詢。

憂鬱AWS rds本身不支援多元複製,只能利用ec2自建mysql,然後把rds資料同步下來。

(1)確定4個rds slave 需要同步的庫
 select concat('replicate_wild_do_table=',TABLE_SCHEMA,'.%') from
(select distinct table_schema from information_schema.TABLES where TABLE_SCHEMA not in('information_schema','mysql','awsdms_control'
,'sys','performance_schema'))ma;

然後新增到my.cnf,我的複製相關主要配置如下:

#gtid
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
binlog_checksum =NONE
enforce_gtid_consistency = 1
log_slave_updates=1
binlog_gtid_simple_recovery
enforce-gtid-consistency=on
transaction_write_set_extraction 
= XXHASH64 slave_parallel_workers=4 slave_parallel_type=LOGICAL_CLOCK

然後在目標端建表結構,如果用AWS同步工具也可以建表結構,但是很坑,工具轉出來的表結構問題很多,導致後面複製經常出問題,因此推薦用mysqldump操作。

(2)用mysqldump匯出表結構,然後在目標機器先把表結構建起來
#CALL mysql.rds_stop_replication;
#在rds slave 制度副本停止同步
mysql -hpro-cxxxx.rds.amazonaws.com -uadmin -pxxx -e "show databases;
"| grep -vEw "Database|information_schema|mysql|awsdms_control|sys|performance_schema"|xargs mysqldump --single-transaction -hxxxx.rds.amazonaws.com -uadmin -pxxxxx -B -R -d > ./copy_$(date +%F).sql

然後倒到目標機器,有幾個源,就這樣操作幾次。

資料初始化方法1:利用AWS自帶收費同步統計DMS,開始嘗試過一次,特別坑,好多欄位都給我轉錯了,遇到不少坑,不推薦。

資料初始化方法2:利用mysqldump,但是太慢了,也不推薦。

資料初始化方法3:利用mydumper多執行緒匯入。

系統是Ubuntu 18.04,mysql是5.7.28

(3)導資料
sudo apt-get update -y apt-get install -y mydumper mydumper -u admin -p xxxx -h pxxxxs.com -k -R -E -G -c -K -t 6 myloader -u root -p xxx -S /tmp/mysql.sock -t 6 -v 2 -d /root/sql/export-20200717-160449

接著在源端建好同步使用者,並且配置好許可權:

create user repl_user@'%' identified by 'xx#';
grant replication slave, replication client on *.* to repl_user@'%';
show grants for repl_user@'%';

最後purge gtid,然後起同步程序:

reset master;
SET @@GLOBAL.GTID_PURGED='6d90ea98-9ff7-11ea-b5b4-0e134e486bba:1-205057735,
38a5a557-a189-11ea-a593-0a7a1a2d24b0:1-2694418,99d788b4-a189-11ea-a11d-0a6b6ede7002:1-15177358,
1fbf39d5-9c10-11ea-8bc2-0a6232fd7ba6:1-56184400,78eb9741-9cd3-11ea-8ce3-06e09c3a69e8:1-200';
CHANGE MASTER TO MASTER_HOST='host1', MASTER_PORT=3306, Master_User='repl_user',Master_Password='xxxx' for channel 'channel1';
CHANGE MASTER TO MASTER_HOST='host2', MASTER_PORT=3306, Master_User='repl_user',Master_Password='xxxx' for channel 'channel2';
CHANGE MASTER TO MASTER_HOST='host3', MASTER_PORT=3306, Master_User='repl_user',Master_Password='xxxx' for channel 'channel3';
CHANGE MASTER TO MASTER_HOST='host4', MASTER_PORT=3306, Master_User='repl_user',Master_Password='xxxx' for channel 'channel4';
start slave;

這裡特別需要注意的地方就是gtid了,purge的gtid是上面從rds的slave倒資料停止從rds主庫同步資料的時候gtid,從rds 只讀副本倒資料不像自建的mysql可以加master-data=2,rds的沒有這種操作,需要確保源端的資料沒有寫,類似利用mysqldump的時候加鎖一樣。

原理跟自建mysql的建slave類似,只是有一些操作不同,受制於平臺,操作方式有一些不一樣而已。

slave起來以後,可能會有一些報錯,需要排查,比如我遇到的:

Worker 2 failed executing transaction '1fbf39d5-9c10-11ea-8bc2-0a6232fd7ba6:59345201' at master log mysql-bin-changelog.016295, end_log_pos 5465018; Column 8
of table 'fcoin.fcoinjournals' cannot be converted from type 'datetime' to type 'datetime'


mysqlbinlog -vv relay-bin.010875 --base64-output=decode-rows --start-position=694756|more
 relay-bin.000100

#200716 10:05:35 server id 372383214  end_log_pos 825728 CRC32 0xd9ed5587     Table_map: `fcoin`.`fcoinjournals` mapped to number 893
# at 825961
#200716 10:05:35 server id 372383214  end_log_pos 825821 CRC32 0xe8a9f75c     Write_rows: table id 893 flags: STMT_END_F
### INSERT INTO `fcoin`.`fcoinjournals`
### SET
###   @1=816067 /* LONGINT meta=0 nullable=0 is_null=0 */
###   @2=132231 /* INT meta=0 nullable=0 is_null=0 */
###   @3=0.01000 /* DECIMAL(18,5) meta=4613 nullable=0 is_null=0 */
###   @4=1 /* INT meta=0 nullable=0 is_null=0 */
###   @5='2020-07-16 10:05:35.097668' /* DATETIME(6) meta=6 nullable=0 is_null=0 */
###   @6='瓜分F鑽活動獲取' /* VARSTRING(1536) meta=1536 nullable=0 is_null=0 */
# at 826054
#200716 10:05:35 server id 372383214  end_log_pos 825885 CRC32 0x712ae0e1     Table_map: `fcoin`.`settledetails` mapped to number 895
# at 826118
#200716 10:05:35 server id 372383214  end_log_pos 825989 CRC32 0x23bce7c7     Update_rows: table id 895 flags: STMT_END_F

我遇到這個就是最開始利用aws同步工具,表結構不是自己建的時候,工具給我把欄位建錯了,後面都是我自己從源端匯入。

可能需要用到:

select * from performance_schema.replication_applier_status_by_worker
show slave status;
start slave for channel 'channel1';
start slave for channel 'channel2';
start slave for channel 'channel3';
start slave for channel 'channel3';


STOP SLAVE;
set @@SESSION.GTID_NEXT= '1fbf39d5-9c10-11ea-8bc2-0a6232fd7ba6:62647265';
begin;commit;
SET SESSION GTID_NEXT = AUTOMATIC;
start slave;

類似跳過的,具體的看報錯情況來處理。