name字段之前32个字符修改为64个字符,备注需要是双引号,中文的需要加上字符集
pt-online-schema-change -h localhost -uroot -pyeemiao1117 --charset='utf8mb4' --alter='modify column name varchar(64) COMMENT "用户姓名"' D=db_admin,t=tb_test --execute
./pt-online-schema-change -h localhost -uroot -pmysql --charset='utf8mb4' --no-version-check --alter='ADD COLUMN name1 varchar(64) NOT NULL DEFAULT "0" COMMENT "名称1",ADD COLUMN name2 varchar(64) NOT NULL DEFAULT "0" COMMENT "名称2" ' D=db_admin,t=tb_test --execute
/opt/percona-toolkit-3.1.0/bin/pt-online-schema-change -h 192.168.1.22 -uroot -pyeemiao3040 --charset='utf8mb4' --no-version-check --alter='DROP COLUMN create_time01' D=db_admin,t=child_logss_temp --execute
6.添加索引
/opt/percona-toolkit-3.1.0/bin/pt-online-schema-change -h 192.168.1.22 -uroot -pyeemiao3040 --charset='utf8mb4' --no-version-check --alter='add index idx_create_time(create_time),add index idx_user_id(user_id)' D=db_admin,t=child_logss_temp --execute
发现创建索引超级慢,因为需要将原表的数据写入到已经创建好索引的中间表中,写入速度很慢。
pt-online-schema-change -h localhost -utest -ptest --charset='utf8mb4' --no-version-check --alter='convert to character set utf8mb4' D=db_test,t=app_scheduled --execute
8.修改表的引擎
/opt/percona-toolkit-3.1.0/bin/pt-online-schema-change -h localhost -uroot -ptesttest --charset='utf8mb4' --no-version-check --alter='ENGINE =INNODB' D=zjs,t=multiple_info_hxl --execute --socket=/tmp/mysql.sock
9.--critical-load Threads_running=200
/opt/percona-toolkit-3.1.0/bin/pt-online-schema-change -h localhost -uroot -ptesttest --charset='utf8mb4' --no-version-check --alter='ENGINE =INNODB' D=zjs,t=multiple_info_hxl --critical-load Threads_running=200 --execute --socket=/tmp/mysql.sock
10.复制原理
创建触发器
create trigger tri_user_token_del after delete on user_token for each row delete ignore from db_rdscmdb.user_token where db_rdscmdb.user_token.id <=> old.id #删掉新表中db._tb_new.id <=> OLD.id的数据,否则忽略操作
create trigger tri_user_token_upd after update on user_token for each row replace into db_rdscmdb.user_token(id,token,user_id,create_time,modify_time) values(new.id,new.token,new.user_id,new.create_time,new.modify_time) #源表执行update的时候,把对应的数据replace into的方式写入新表
create trigger tri_user_token_ins after insert on user_token for each row replace into db_rdscmdb.user_token(id,token,user_id,create_time,modify_time) values(new.id,new.token,new.user_id,new.create_time,new.modify_time) #源表执行iinsert操作的时候,把对应的数据replace into的方式写入新表
执行导入语句:
insert low_priority ignore into db_rdscmdb.user_token(id,token,user_id,create_time,modify_time)
select id,token,user_id,create_time,modify_time from db_admin.user_token FORCE INDEX(`PRIMARY`) lock in share mode
此处有锁,针对大数据量的表可以采用如下复制
insert low_priority ignore into db_rdscmdb.user_token(id,token,user_id,create_time,modify_time)
select id,token,user_id,create_time,modify_time from db_admin.user_token FORCE INDEX(`PRIMARY`) WHERE ((id >= 1)) AND ((id <= 100000)) lock in share mode
insert low_priority ignore into db_rdscmdb.user_token(id,token,user_id,create_time,modify_time)
select id,token,user_id,create_time,modify_time from db_admin.user_token FORCE INDEX(`PRIMARY`) WHERE ((id >=100001 )) AND ((id <= 200000)) lock in share mode
删除触发器
DROP TRIGGER IF EXISTS `db_admin`.`tri_user_token_del`;
DROP TRIGGER IF EXISTS `db_admin`.`tri_user_token_upd`;
DROP TRIGGER IF EXISTS `db_admin`.`tri_user_token_ins`;
rename到新的表