zoukankan      html  css  js  c++  java
  • pt-online-schema-change使用例子

    执行psc的账号需要具有超级权限,否则会报错:
    Error creating triggers: 2019-11-27T09:56:16 DBD::mysql::db do failed: You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) [for Statement "CREATE TRIGGER `pt_osc_db_admin_child_logss_temp_del` AFTER DELETE ON `db_admin`.`child_logss_temp` FOR EACH ROW DELETE IGNORE FROM `db_admin`.`_child_logss_temp_new` WHERE `db_admin`.`_child_logss_temp_new`.`id` <=> OLD.`id`"] at /opt/percona-toolkit-3.1.0/bin/pt-online-schema-change line 11224.
     
     
    1.修改字段长度
    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
     
     
    2.添加多列并设置默认值
    ./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
     
    3.不真正执行,只看执行过程--dry-run
    /opt/percona-toolkit-3.1.0/bin/pt-online-schema-change -h localhost -uroot -pmysql --charset='utf8mb4' --no-version-check --alter='ADD COLUMN name3  varchar(64) NOT NULL DEFAULT "0" COMMENT "名称1",ADD COLUMN name4  varchar(64) NOT NULL DEFAULT "0" COMMENT "名称2" ' D=db_admin,t=tb_test --dry-run --socket=/tmp/mysql.sock

     
    4.碎片整理
    /opt/percona-toolkit-3.1.0/bin/pt-online-schema-change -h localhost -uroot -pyeemiao1117 --charset='utf8mb4' --no-version-check --alter='ENGINE=InnoDB' D=db_admin,t=tb_test --execute --socket=/tmp/mysql.sock --nocheck-replication-filters
     
    5.删除列
    /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
    发现创建索引超级慢,因为需要将原表的数据写入到已经创建好索引的中间表中,写入速度很慢。
     
     7.修改字符集

    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到新的表

  • 相关阅读:
    数据处理
    Interleaving String
    Distinct Subsequences
    caffe 输入图像图像加高斯噪声
    caffe resize用interpolation
    软件測试基本方法(七)之验收測试
    CSS入门学习
    bzoj1458 士兵占据
    Swift高阶函数介绍(闭包、Map、Filter、Reduce)
    每天一点儿Java--list
  • 原文地址:https://www.cnblogs.com/hxlasky/p/11555280.html
Copyright © 2011-2022 走看看