• RDS for MySQL之pt-online-schema-change工具使用


    ##工具介绍:
    pt-online-schema-change是percona toolkit工具箱功能之一,主要是在避免锁表的情况下做增加/删除字段等DDL操作。
    大概原理是新建一个表,并且这个表已经是最新的表结构,
    从旧表往新表insert数据,同时会在旧表上创建增删改三个触发器,旧表上所有增删改操作会插入到新表,最后新表旧表做换名操作。

    【注意事项】
    **要操作的表必须有主键**
    **会有一定概率发生死锁,如一直有死锁发生建议使用Gh-ost软件**

    ##1、安装
    参考官方文档
    https://www.percona.com/downloads/percona-toolkit/LATEST/

    ##2、使用
    pt-online-schema-change --help
    可以查看到该命令的参数列表

    先介绍几个常用参数
    h=:指定主机名
    P=:指定主机端口
    u=:指定数据库用户名
    p=:指定数据库密码
    --execute:确定修改表
    --alter:要执行的语句
    --no-version-check:不检查版本信息

    --recursion-method:Mater寻找Slave的方法,在RDS中使用需要设置为none
    着重介绍一下这个参数,因为pt-online-schema-change这个工具默认会检测主从延迟,而RDS用户看到的都是逻辑IP,PT工具通过show slave status;获取到Slave的IP,这个IP是物理机IP,所以PT工具连接这个IP网络会不通,导致使用PT工具一直卡住很长时间;设置为none,不检测主从延迟。

    ##3、示例
    ###创建测试环境:
    cretae database ceshi;
    use ceshi;
    create table (t1 id int primary key);

    ###执行添加字段name列
    pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "add column name varchar(200)" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1


    ###输出日志如下:
    No slaves found. See --recursion-method if host a48c13173.cloud.na62 has slaves.
    Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
    Operation, tries, wait:
    analyze_table, 10, 1
    copy_rows, 10, 0.25
    create_triggers, 10, 1
    drop_triggers, 10, 1
    swap_tables, 10, 1
    update_foreign_keys, 10, 1
    Altering `ceshi`.`t1`...
    Creating new table...
    Created new table ceshi._t1_new OK.
    Altering new table...
    Altered `ceshi`.`_t1_new` OK.
    2019-09-15T09:57:30 Creating triggers...
    2019-09-15T09:57:31 Created triggers OK.
    2019-09-15T09:57:31 Copying approximately 1 rows...
    2019-09-15T09:57:31 Copied rows OK.
    2019-09-15T09:57:31 Analyzing new table...
    2019-09-15T09:57:31 Swapping tables...
    2019-09-15T09:57:31 Swapped original and new tables OK.
    2019-09-15T09:57:31 Dropping old table...
    2019-09-15T09:57:31 Dropped old table `ceshi`.`_t1_old` OK.
    2019-09-15T09:57:31 Dropping triggers...
    2019-09-15T09:57:31 Dropped triggers OK.
    Successfully altered `ceshi`.`t1`.

    ###Successfully 结尾代表执行成功。

    ##4、常用指南
    1、添加字段:
    pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "add column name varchar(200)" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

    2、删除字段:
    pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "drop column name" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

    3、修改字段类型
    pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "modify name varchar(300)" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

    4、创建索引
    pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "add key idx_name (name)" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

    5、删除索引
    pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "drop key idx_name" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

    6、碎片整理,释放空间
    pt-online-schema-change --no-version-check --execute --recursion-method=none --alter "engine=innodb" h=xxx.mysql.zhangbei.rds.aliyuncs.com,P=3306,u=root,p=xxx,D=ceshi,t=t1

    ##总结
    除了--alter部分内容是变动的,其它参数内容都是固定的;

  • 相关阅读:
    最后一次不用看脸的日子
    经典算法_指针
    #include <process.h>
    经典算法_文件
    stdin、stdout、stderr
    经典算法_位运算
    经典算法_结构体
    经典算法_字符串
    #include <string.h>
    #include <stdio.h>
  • 原文地址:https://www.cnblogs.com/nanxiang/p/14058939.html
Copyright © 2020-2023  润新知