zoukankan      html  css  js  c++  java
  • pt-osc在线工具的使用

    一.pt-osc工作原理

    1.创建一个和源表一样表结构的新表
    2.空表执行表结构修改
    3.在源表创建三个触发器分别对应insert、update、delete操作
    4.copy数据到新表
    5.将源表改名,并将新表改成源表名
    6.删除源表
    7.删除trigger
    

    二.pt-osc 工具的限制

    1.源表不能有触发器存在
    不是不能有任何触发器,只是不能有针对insert、update、delete的触发器存在,因为一个表上不能有两个相同类型的触发器
    2.源表必须要有主键
    3.源表有外键,必须使用--alter-foreign-keys-method 指定特定的值
    

    三.注意事项

    1.如果是add column并且定义了not null,那么必须指定default值,否则会失败
    

    四.报错原因

    没有主键,报错如下

    The new table `test`.`_t1_new` does not have a PRIMARY KEY or a unique index which is required for the DELETE trigger.
    Please check you have at least one UNIQUE and NOT NULLABLE index.
    

    没有权限报错如下

    Cannot connect to MySQL: DBI connect('test;host=10.0.1.51;mysql_read_default_group=client','root',...) failed: Access denied for user 'root'@'10.0.1.51' (using password: YES) at /usr/bin/pt-online-schema-change line 2345.
    

    存在触发器,源表不能存在针对(insert、update、delete)触发器

    The table `db_name`.`table_name` has triggers.  This tool needs to create its own triggers, so the table cannot already have triggers.
    

    五.参数说明

    --host=xxx --user=xxx --password=xxx

    连接实例信息,缩写-h xxx -u xxx -p xxx,密码可以使用参数--ask-pass 手动输入
    

    D=db_name,t=table_name

    指定要ddl的数据库名和表名
    

    --dry-run

    创建和修改新表,但不会创建触发器、复制数据、和替换原表。并不真正执行,可以看到生成的执行语句,了解其执行步骤与细节,和--print配合最佳
    

    --execute

    修改表,则指定该参数。真正执行alter。–dry-run与–execute必须指定一个,二者相互排斥
    

    ----[no]check-alter

    解析并检查alter指定的命令:
    在以前的版本,使用CHANGE COLUMN命令会导致数据丢失,现在的版本虽然改进,但是在执行前,还应该使用 --dry-run 和 --print 查看一下详细的操作情况.
    

    六.模拟演示

    环境准备

    db01 [test]>create database yufenchi charset utf8mb4;
    db01 [test]>use yufenchi
    db01 [yufenchi]>create table t1(id int primary key,name varchar(10),age tinyint);
    b01 [yufenchi]>insert into t1 values(1,'zs',18),(2,'lisi',19),(3,'wangwu',20);
    db01 [yufenchi]>select * from t1;
    +----+--------+------+
    | id | name   | age  |
    +----+--------+------+
    |  1 | zs     |   18 |
    |  2 | lisi   |   19 |
    |  3 | wangwu |   20 |
    +----+--------+------+
    

    增加一列qq

    [root@db01 ~]# pt-online-schema-change --host=10.0.1.51 -uroot -p123 --alter "add column qq int(20) default 0" D=yufenchi,t=t1  --execute --no-check-alter
    登录数据库并检查
    db01 [yufenchi]>select * from t1;
    +----+--------+------+------+
    | id | name   | age  | qq   |
    +----+--------+------+------+
    |  1 | zs     |   18 |    0 |
    |  2 | lisi   |   19 |    0 |
    |  3 | wangwu |   20 |    0 |
    +----+--------+------+------+
    

    增加一列weixin(如果加入的列中定义了not null必须要指定default值)

    [root@db01 ~]# pt-online-schema-change --host=10.0.1.51 -uroot -p123 --alter "add column weixin int(20) not null default 0" D=yufenchi,t=t1 --execute --print --no-check-alter
    db01 [yufenchi]>select * from t1;
    +----+--------+------+------+--------+
    | id | name   | age  | qq   | weixin |
    +----+--------+------+------+--------+
    |  1 | zs     |   18 |    0 |      0 |
    |  2 | lisi   |   19 |    0 |      0 |
    |  3 | wangwu |   20 |    0 |      0 |
    +----+--------+------+------+--------+
    

    修改列name为varchar(20)

    [root@db01 ~]# pt-online-schema-change --host=10.0.1.51 -uroot --ask-pass --alter "modify name varchar(20)  default 0" D=yufenchi,t=t1 --execute --print --no-check-alter
    db01 [yufenchi]>show create table t1;
    +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                                                                                                                                                      |
    +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | t1    | CREATE TABLE `t1` (
      `id` int(11) NOT NULL,
      `name` varchar(20) DEFAULT '0',
      `age` tinyint(4) DEFAULT NULL,
      `qq` int(20) DEFAULT '0',
      `weixin` int(20) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
    

    修改列名name为sname

    [root@db01 ~]# pt-online-schema-change --host=10.0.1.51 -uroot --ask-pass --alter "change name sname varchar(20)  default 0" D=yufenchi,t=t1 --execute  --no-check-alter
    db01 [yufenchi]>desc t1;
    +--------+-------------+------+-----+---------+-------+
    | Field  | Type        | Null | Key | Default | Extra |
    +--------+-------------+------+-----+---------+-------+
    | id     | int(11)     | NO   | PRI | NULL    |       |
    | sname  | varchar(20) | YES  |     | 0       |       |
    | age    | tinyint(4)  | YES  |     | NULL    |       |
    | qq     | int(20)     | YES  |     | 0       |       |
    | weixin | int(20)     | NO   |     | 0       |       |
    +--------+-------------+------+-----+---------+-------+
    

    为列sname添加索引

    [root@db01 ~]# pt-online-schema-change --host=10.0.1.51 -uroot -p123 --alter "add index idx_sna(sname)" D=yufenchi,t=t1 --execute --no-check-alter
    db01 [yufenchi]>show index from t1;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | t1    |          0 | PRIMARY  |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
    | t1    |          1 | idx_sna  |            1 | sname       | A         |           3 |     NULL | NULL   | YES  | BTREE      |         |               |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--
    
    学习的进阶之路
  • 相关阅读:
    平衡二叉树之RB树
    平衡二叉树之AVL树
    实现哈希表
    LeetCode Median of Two Sorted Arrays
    LeetCode Minimum Window Substring
    LeetCode Interleaving String
    LeetCode Regular Expression Matching
    PAT 1087 All Roads Lead to Rome
    PAT 1086 Tree Traversals Again
    LeetCode Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/yufenchi/p/12961750.html
Copyright © 2011-2022 走看看