博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql:pt-online-schema-change 在线修改表
阅读量:4070 次
发布时间:2019-05-25

本文共 29663 字,大约阅读时间需要 98 分钟。

pt-online-schema-change

名字:pt-online-schema-change - ALTER tables without locking them. 在线改表

下载地址:

https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/6/x86_64/percona-toolkit-3.0.12-1.el6.x86_64.rpm
https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html#downloading
官方文档:https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html

1.首先了解一下pt-online-schema-change工作原理,其实原理很简单

1、如果存在外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,做相应设置的处理。没有使用 --alter-foreign-keys-method 指定特定的值,该工具不予执行2、创建一个新的表,表结构为修改后的数据表,用于从源数据表向新表中导入数据。3、创建触发器,用于记录从拷贝数据开始之后,对源数据表继续进行数据修改的操作记录下来,用于数据拷贝结束后,执行这些操作,保证数据不会丢失。如果表中已经定义了触发器这个工具就不能工作了。4、拷贝数据,从源数据表中拷贝数据到新表中。5、修改外键相关的子表,根据修改后的数据,修改外键关联的子表。6、rename源数据表为old表,把新表rename为源表名,并将old表删除。7、删除触发器。

2.使用方法:

pt-online-schema-change [OPTIONS] DSN具体说下 OPTIONS 的一些常用的参数:复制代码--user:-u,连接的用户名--password:-p,连接的密码--database:-D,连接的数据库--port-P,连接数据库的端口--host:-h,连接的主机地址--socket:-S,连接的套接字文件--ask-pass隐式输入连接MySQL的密码--charset指定修改的字符集--defaults-file-F,读取配置文件--alter:结构变更语句,不需要alter table关键字。可以指定多个更改,用逗号分隔。如下场景,需要注意:    不能用RENAME来重命名表。            列不能通过先删除,再添加的方式进行重命名,不会将数据拷贝到新列。    如果加入的列非空而且没有默认值,则工具会失败。即其不会为你设置一个默认值,必须显示指定。    删除外键(drop foreign key constrain_name)时,需要指定名称_constraint_name,而不是原始的constraint_name。    如:CONSTRAINT `fk_foo` FOREIGN KEY (`foo_id`) REFERENCES `bar` (`foo_id`),需要指定:--alter "DROP FOREIGN KEY _fk_foo"--alter-foreign-keys-method如何把外键引用到新表?需要特殊处理带有外键约束的表,以保证它们可以应用到新表.当重命名表的时候,外键关系会带到重命名后的表上。该工具有两种方法,可以自动找到子表,并修改约束关系。    auto: 在rebuild_constraints和drop_swap两种处理方式中选择一个。    rebuild_constraints:使用 ALTER TABLE语句先删除外键约束,然后再添加.如果子表很大的话,会导致长时间的阻塞。    drop_swap: 执行FOREIGN_KEY_CHECKS=0,禁止外键约束,删除原表,再重命名新表。这种方式很快,也不会产生阻塞,但是有风险:    1, 在删除原表和重命名新表的短时间内,表是不存在的,程序会返回错误。    2, 如果重命名表出现错误,也不能回滚了.因为原表已经被删除。    none: 类似"drop_swap"的处理方式,但是它不删除原表,并且外键关系会随着重命名转到老表上面。--[no]check-alter默认yes,语法解析。配合--dry-run 和 --print 一起运行,来检查是否有问题(change column,drop primary key)。--max-lag默认1s。每个chunk拷贝完成后,会查看所有复制Slave的延迟情况。要是延迟大于该值,则暂停复制数据,直到所有从的滞后小于这个值,使用Seconds_Behind_Master。如果有任何从滞后超过此选项的值,则该工具将睡眠--check-interval指定的时间,再检查。如果从被停止,将会永远等待,直到从开始同步,并且延迟小于该值。如果指定--check-slave-lag,该工具只检查该服务器的延迟,而不是所有服务器。--check-slave-lag指定一个从库的DSN连接地址,如果从库超过--max-lag参数设置的值,就会暂停操作。--recursion-method默认是show processlist,发现从的方法,也可以是host,但需要在从上指定report_host,通过show slave hosts来找到,可以指定none来不检查Slave。METHOD       USES===========  ==================processlist  SHOW PROCESSLISThosts        SHOW SLAVE HOSTSdsn=DSN      DSNs from a tablenone         Do not find slaves指定none则表示不在乎从的延迟。--check-interval 默认是1。--max-lag检查的睡眠时间。 --[no]check-plan 默认yes。检查查询执行计划的安全性。--[no]check-replication-filters 默认yes。如果工具检测到服务器选项中有任何复制相关的筛选,如指定binlog_ignore_db和replicate_do_db此类。发现有这样的筛选,工具会报错且退出。因为如果更新的表Master上存在,而Slave上不存在,会导致复制的失败。使用–no-check-replication-filters选项来禁用该检查。 --[no]swap-tables 默认yes。交换原始表和新表,除非你禁止--[no]drop-old-table。 --[no]drop-triggers 默认yes,删除原表上的触发器。 --no-drop-triggers 会强制开启 --no-drop-old-table 即:不删除触发器就会强制不删除原表。 --new-table-name 复制创建新表的名称,默认%T_new。 --[no]drop-new-table 默认yes。删除新表,如果复制组织表失败。 --[no]drop-old-table 默认yes。复制数据完成重命名之后,删除原表。如果有错误则会保留原表。 --max-load 默认为Threads_running=25。每个chunk拷贝完后,会检查SHOW GLOBAL STATUS的内容,检查指标(thread)是否超过了指定的阈值。如果超过,则先暂停。这里可以用逗号分隔,指定多个条件,每个条件格式:status指标=MAX_VALUE或者status指标:MAX_VALUE。如果不指定MAX_VALUE,那么工具会指定其为当前值的120%。 --critical-load 默认为Threads_running=50。用法基本与--max-load类似,如果不指定MAX_VALUE,那么工具会指定其为当前值的200%。如果超过指定值,则工具直接退出,而不是暂停。 --default-engine 默认情况下,新的表与原始表是相同的存储引擎,所以如果原来的表使用InnoDB的,那么新表将使用InnoDB的。在涉及复制某些情况下,很可能主从的存储引擎不一样。使用该选项会默认使用默认的存储引擎。 --set-vars 设置MySQL变量,多个用逗号分割。默认该工具设置的是: wait_timeout=10000 innodb_lock_wait_timeout=1 lock_wait_timeout=60 --chunk-size-limit当需要复制的块远大于设置的chunk-size大小,就不复制.默认值是4.0,一个没有主键或唯一索引的表,块大小就是不确定的。--chunk-time在chunk-time执行的时间内,动态调整chunk-size的大小,以适应服务器性能的变化,该参数设置为0,或者指定chunk-size,都可以禁止动态调整。--chunk-size指定块的大小,默认是1000行,可以添加k,M,G后缀.这个块的大小要尽量与--chunk-time匹配,如果明确指定这个选项,那么每个块就会指定行数的大小.	--[no]check-plan默认yes。为了安全,检查查询的执行计划.默认情况下,这个工具在执行查询之前会先EXPLAIN,以获取一次少量的数据,如果是不好的EXPLAIN,那么会获取一次大量的数据,这个工具会多次执行EXPALIN,如果EXPLAIN不同的结果,那么就会认为这个查询是不安全的。	--statistics打印出内部事件的数目,可以看到复制数据插入的数目。--dry-run创建和修改新表,但不会创建触发器、复制数据、和替换原表。并不真正执行,可以看到生成的执行语句,了解其执行步骤与细节。--dry-run与--execute必须指定一个,二者相互排斥。和--print配合最佳。--execute确定修改表,则指定该参数。真正执行。--dry-run与--execute必须指定一个,二者相互排斥。--print打印SQL语句到标准输出。指定此选项可以让你看到该工具所执行的语句,和--dry-run配合最佳。--progress复制数据的时候打印进度报告,二部分组成:第一部分是百分比,第二部分是时间。--quiet-q,不把信息标准输出。

3.安装

下载最新的pt-online-schema-change[root@18c data]# wget https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/6/x86_64/percona-toolkit-3.0.12-1.el6.x86_64.rpm安装[root@18c data]# rpm -ivh percona-toolkit-3.0.12-1.el6.x86_64.rpm warning: percona-toolkit-3.0.12-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEYerror: Failed dependencies:        perl(DBI) >= 1.13 is needed by percona-toolkit-3.0.12-1.el6.x86_64        perl(DBD::mysql) >= 1.0 is needed by percona-toolkit-3.0.12-1.el6.x86_64        perl(Time::HiRes) is needed by percona-toolkit-3.0.12-1.el6.x86_64        perl(IO::Socket::SSL) is needed by percona-toolkit-3.0.12-1.el6.x86_64        perl(Term::ReadKey) is needed by percona-toolkit-3.0.12-1.el6.x86_64		安装相关的依赖包: [root@18c data]#  yum install perl-TermReadKey.x86_64 [root@18c data]#   yum install perl-DBI[root@18c data]#   yum install perl-DBD-MySQL[root@18c data]#   yum install perl-Time-HiRes[root@18c data]#   yum install perl-IO-Socket-SSL然后再次安装[root@18c data]# rpm -ivh percona-toolkit-3.0.12-1.el6.x86_64.rpm warning: percona-toolkit-3.0.12-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEYPreparing...                ########################################### [100%]   1:percona-toolkit        ########################################### [100%]

至此软件已经安装好,下面就通过测试来了解如何使用该工具

创建测试数据

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || sys                |+--------------------+4 rows in set (0.00 sec)mysql> create database test;Query OK, 1 row affected (0.00 sec)mysql> use test;Database changedmysql> CREATE TABLE users (id int(11) NOT NULL AUTO_INCREMENT COMMENT '用户主键',name varchar(20)  DEFAULT '' COMMENT '用户名',PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';Query OK, 0 rows affected (0.04 sec)mysql> insert into users(name) values('test1'),('test2'),('test3');Query OK, 3 rows affected (0.01 sec)Records: 3  Duplicates: 0  Warnings: 0mysql> show tables;+----------------+| Tables_in_test |+----------------+| users          |+----------------+1 row in set (0.00 sec)mysql> select count(*) from users;+----------+| count(*) |+----------+|        3 |+----------+1 row in set (0.00 sec)

在正式使用之前,再次了解一下常用的几个参数:

--nocheck-replication-filters :不检查复制过滤器,建议启用。--no-check-binlog-format : 不检查复制的binlog模式,要是binlog模式是ROW,则会报错。--replicate-check-only :只显示不同步的信息。--replicate= :把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中。--chunk-size=50   Number of rows to select for each chunk copied. Allowable suffixes are k, M, G.默认1000--critical-load="Threads_running=600"  表示线程到600自动退出,默认值50--max-load="Threads_running=100"   表示线程到100暂停pt操作,默认25--progress=time,2   两秒打印一次日志输出

测试各种不同的场景

1.整理表碎片

sql语句:alter table table_name engine=innodbpt执行语句,以下2种都可以(1)pt-online-schema-change --alter="engine innodb;" --execute D=test,t=users --no-check-replication-filters --max-load="Threads_running=100"  --critical-load="Threads_running=120" --socket=/tmp/mysql.sock --user=root --password=123  --charset=utf8 --progress=time,2 --chunk-size=100(2)pt-online-schema-change --alter="engine innodb;" --execute D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --progress=time,2 --chunk-size=100具体执行过程:[root@18c ~]# pt-online-schema-change --alter="engine innodb;" --execute D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8    --progress=time,2 --chunk-size=100No slaves found.  See --recursion-method if host 18c 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, 1Altering `test`.`users`...Creating new table...Created new table test._users_new OK.Altering new table...Altered `test`.`_users_new` OK.2018-12-06T13:28:05 Creating triggers...2018-12-06T13:28:05 Created triggers OK.2018-12-06T13:28:05 Copying approximately 3 rows...2018-12-06T13:28:05 Copied rows OK.2018-12-06T13:28:05 Analyzing new table...2018-12-06T13:28:05 Swapping tables...2018-12-06T13:28:05 Swapped original and new tables OK.2018-12-06T13:28:05 Dropping old table...2018-12-06T13:28:05 Dropped old table `test`.`_users_old` OK.2018-12-06T13:28:05 Dropping triggers...2018-12-06T13:28:05 Dropped triggers OK.Successfully altered `test`.`users`.

2.添加字段:

(1)添加一个字段alter table users add age varchar(10) NOT NUll DEFAULT '' COMMENT '性别';pt语句: pt-online-schema-change --alter="add age varchar(10) NOT NUll DEFAULT '' COMMENT '性别' ;"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100具体执行过程[root@18c ~]# pt-online-schema-change --alter="add age varchar(10) NOT NUll DEFAULT '' COMMENT '性别' ;"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100No slaves found.  See --recursion-method if host 18c 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, 1Altering `test`.`users`...Creating new table...CREATE TABLE `test`.`_users_new` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户主键',  `name` varchar(20) DEFAULT '' COMMENT '用户名',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表'Created new table test._users_new OK.Altering new table...ALTER TABLE `test`.`_users_new` add age varchar(10) NOT NUll DEFAULT '' COMMENT 'æ�§å�«' ;Altered `test`.`_users_new` OK.2018-12-06T13:36:55 Creating triggers...2018-12-06T13:36:55 Created triggers OK.2018-12-06T13:36:55 Copying approximately 3 rows...INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`) SELECT `id`, `name` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17461 copy table*/2018-12-06T13:36:55 Copied rows OK.2018-12-06T13:36:55 Analyzing new table...2018-12-06T13:36:55 Swapping tables...RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`2018-12-06T13:36:55 Swapped original and new tables OK.2018-12-06T13:36:55 Dropping old table...DROP TABLE IF EXISTS `test`.`_users_old`2018-12-06T13:36:55 Dropped old table `test`.`_users_old` OK.2018-12-06T13:36:55 Dropping triggers...DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`2018-12-06T13:36:55 Dropped triggers OK.Successfully altered `test`.`users`.(2)添加2个或多个字段sql语句:alter table users add column address varchar(100) default '' comment='地址' after age,add column telephone int default 0 comment='手机号码';pt语句:pt-online-schema-change --alter="add column address varchar(100) default '' comment '地址' after age,add column telephone int default 0 comment '手机号码' after address ;"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100具体执行过程:[root@18c ~]# pt-online-schema-change --alter="add column address varchar(100) default '' comment '地址' after age,add column telephone int default 0 comment '手机号码' after address ;"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100No slaves found.  See --recursion-method if host 18c 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, 1Altering `test`.`users`...Creating new table...CREATE TABLE `test`.`_users_new` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户主键',  `name` varchar(20) DEFAULT '' COMMENT '用户名',  `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性别',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表'Created new table test._users_new OK.Altering new table...ALTER TABLE `test`.`_users_new` add column address varchar(100) default '' comment 'å�°å��' after age,add column telephone int default 0 comment 'æ��æ�ºå�·ç �' after address ;Altered `test`.`_users_new` OK.2018-12-06T14:02:40 Creating triggers...2018-12-06T14:02:40 Created triggers OK.2018-12-06T14:02:40 Copying approximately 3 rows...INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`) SELECT `id`, `name`, `age` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17584 copy table*/2018-12-06T14:02:40 Copied rows OK.2018-12-06T14:02:40 Analyzing new table...2018-12-06T14:02:40 Swapping tables...RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`2018-12-06T14:02:40 Swapped original and new tables OK.2018-12-06T14:02:40 Dropping old table...DROP TABLE IF EXISTS `test`.`_users_old`2018-12-06T14:02:40 Dropped old table `test`.`_users_old` OK.2018-12-06T14:02:40 Dropping triggers...DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`2018-12-06T14:02:40 Dropped triggers OK.Successfully altered `test`.`users`.

3.修改字段

sql语句:alter table users modify column address varchar(200) default '' comment '家庭住址';pt语句: pt-online-schema-change  --alter="modify column address varchar(200) default '' comment '家庭住址';"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100具体执行过程:[root@18c ~]# pt-online-schema-change --alter="modify column address varchar(200) default '' comment '家庭住址';"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100No slaves found.  See --recursion-method if host 18c 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, 1Altering `test`.`users`...Creating new table...CREATE TABLE `test`.`_users_new` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户主键',  `name` varchar(20) DEFAULT '' COMMENT '用户名',  `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性别',  `address` varchar(100) DEFAULT '' COMMENT '地址',  `telephone` int(11) DEFAULT '0' COMMENT '手机号码',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表'Created new table test._users_new OK.Altering new table...ALTER TABLE `test`.`_users_new` modify column address varchar(200) default '' comment '家庭ä½�å��';Altered `test`.`_users_new` OK.2018-12-06T14:08:57 Creating triggers...2018-12-06T14:08:57 Created triggers OK.2018-12-06T14:08:57 Copying approximately 3 rows...INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17607 copy table*/2018-12-06T14:08:57 Copied rows OK.2018-12-06T14:08:57 Analyzing new table...2018-12-06T14:08:57 Swapping tables...RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`2018-12-06T14:08:57 Swapped original and new tables OK.2018-12-06T14:08:57 Dropping old table...DROP TABLE IF EXISTS `test`.`_users_old`2018-12-06T14:08:57 Dropped old table `test`.`_users_old` OK.2018-12-06T14:08:57 Dropping triggers...DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`2018-12-06T14:08:57 Dropped triggers OK.Successfully altered `test`.`users`.

4.添加索引

sql语句:alter table users add index index_age(age);pt语句:pt-online-schema-change --alter="add index index_age(age);"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100具体执行过程:[root@18c ~]# pt-online-schema-change --alter="add index index_age(age);"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100No slaves found.  See --recursion-method if host 18c 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, 1Altering `test`.`users`...Creating new table...CREATE TABLE `test`.`_users_new` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户主键',  `name` varchar(20) DEFAULT '' COMMENT '用户名',  `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性别',  `address` varchar(200) DEFAULT '' COMMENT '家庭住址',  `telephone` int(11) DEFAULT '0' COMMENT '手机号码',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表'Created new table test._users_new OK.Altering new table...ALTER TABLE `test`.`_users_new` add index index_age(age);Altered `test`.`_users_new` OK.2018-12-06T14:11:39 Creating triggers...2018-12-06T14:11:39 Created triggers OK.2018-12-06T14:11:39 Copying approximately 3 rows...INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17622 copy table*/2018-12-06T14:11:39 Copied rows OK.2018-12-06T14:11:39 Analyzing new table...2018-12-06T14:11:39 Swapping tables...RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`2018-12-06T14:11:39 Swapped original and new tables OK.2018-12-06T14:11:39 Dropping old table...DROP TABLE IF EXISTS `test`.`_users_old`2018-12-06T14:11:39 Dropped old table `test`.`_users_old` OK.2018-12-06T14:11:39 Dropping triggers...DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`2018-12-06T14:11:39 Dropped triggers OK.Successfully altered `test`.`users`.

5.删除索引

sql语句:alter table users drop index index_age;pt语句: pt-online-schema-change --alter="drop index index_age;"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100具体执行过程:[root@18c ~]# pt-online-schema-change --alter="drop index index_age;"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100No slaves found.  See --recursion-method if host 18c 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, 1Altering `test`.`users`...Creating new table...CREATE TABLE `test`.`_users_new` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户主键',  `name` varchar(20) DEFAULT '' COMMENT '用户名',  `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性别',  `address` varchar(200) DEFAULT '' COMMENT '家庭住址',  `telephone` int(11) DEFAULT '0' COMMENT '手机号码',  PRIMARY KEY (`id`),  KEY `index_age` (`age`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表'Created new table test._users_new OK.Altering new table...ALTER TABLE `test`.`_users_new` drop index index_age;Altered `test`.`_users_new` OK.2018-12-06T14:13:25 Creating triggers...2018-12-06T14:13:25 Created triggers OK.2018-12-06T14:13:25 Copying approximately 3 rows...INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17629 copy table*/2018-12-06T14:13:25 Copied rows OK.2018-12-06T14:13:25 Analyzing new table...2018-12-06T14:13:25 Swapping tables...RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`2018-12-06T14:13:25 Swapped original and new tables OK.2018-12-06T14:13:25 Dropping old table...DROP TABLE IF EXISTS `test`.`_users_old`2018-12-06T14:13:25 Dropped old table `test`.`_users_old` OK.2018-12-06T14:13:25 Dropping triggers...DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`2018-12-06T14:13:25 Dropped triggers OK.Successfully altered `test`.`users`.

6.修改自增ID

sql语句:alter table users modify column id bigint not null auto_increment;pt语句: pt-online-schema-change --alter="modify column id bigint not null auto_increment;"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100具体执行过程:[root@18c ~]# pt-online-schema-change --alter="modify column id bigint not null auto_increment;"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100No slaves found.  See --recursion-method if host 18c 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, 1Altering `test`.`users`...Creating new table...CREATE TABLE `test`.`_users_new` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户主键',  `name` varchar(20) DEFAULT '' COMMENT '用户名',  `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性别',  `address` varchar(200) DEFAULT '' COMMENT '家庭住址',  `telephone` int(11) DEFAULT '0' COMMENT '手机号码',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表'Created new table test._users_new OK.Altering new table...ALTER TABLE `test`.`_users_new` modify column id bigint not null auto_increment;Altered `test`.`_users_new` OK.2018-12-06T14:17:35 Creating triggers...2018-12-06T14:17:35 Created triggers OK.2018-12-06T14:17:35 Copying approximately 3 rows...INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17643 copy table*/2018-12-06T14:17:35 Copied rows OK.2018-12-06T14:17:35 Analyzing new table...2018-12-06T14:17:35 Swapping tables...RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`2018-12-06T14:17:35 Swapped original and new tables OK.2018-12-06T14:17:35 Dropping old table...DROP TABLE IF EXISTS `test`.`_users_old`2018-12-06T14:17:35 Dropped old table `test`.`_users_old` OK.2018-12-06T14:17:35 Dropping triggers...DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`2018-12-06T14:17:35 Dropped triggers OK.Successfully altered `test`.`users`.

7.修改列名,并添加索引

sql语句:alter table test change column age ages varchar(5) default '' comment '性别新' after address,add index index_ages(ages);pt语句:pt-online-schema-change --alter="change column age ages varchar(5) after address,add index index_ages(ages);"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100 --no-check-alter具体执行过程:[root@18c ~]# pt-online-schema-change --alter="change column age ages varchar(5) after address,add index index_ages(ages);"  --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100 --no-check-alterNo slaves found.  See --recursion-method if host 18c 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, 1Altering `test`.`users`...Renaming columns:  age to agesCreating new table...CREATE TABLE `test`.`_users_new` (  `id` bigint(20) NOT NULL AUTO_INCREMENT,  `name` varchar(20) DEFAULT '' COMMENT '用户名',  `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性别',  `address` varchar(200) DEFAULT '' COMMENT '家庭住址',  `telephone` int(11) DEFAULT '0' COMMENT '手机号码',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表'Created new table test._users_new OK.Altering new table...ALTER TABLE `test`.`_users_new` change column age ages varchar(5) after address,add index index_ages(ages);Altered `test`.`_users_new` OK.2018-12-06T14:55:27 Creating triggers...2018-12-06T14:55:27 Created triggers OK.2018-12-06T14:55:27 Copying approximately 3 rows...INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `ages`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17822 copy table*/2018-12-06T14:55:27 Copied rows OK.2018-12-06T14:55:27 Analyzing new table...2018-12-06T14:55:27 Swapping tables...RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`2018-12-06T14:55:27 Swapped original and new tables OK.2018-12-06T14:55:27 Dropping old table...DROP TABLE IF EXISTS `test`.`_users_old`2018-12-06T14:55:27 Dropped old table `test`.`_users_old` OK.2018-12-06T14:55:27 Dropping triggers...DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`2018-12-06T14:55:27 Dropped triggers OK.Successfully altered `test`.`users`.

上面做了这么多的操作,现在来看看测试表已经变成啥模样了

mysql> show create table users;+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table                                                                                                                                                                                                                                                                                                                                                                                               |+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| users | CREATE TABLE `users` (  `id` bigint(20) NOT NULL AUTO_INCREMENT,  `name` varchar(20) DEFAULT '' COMMENT '用户名',  `address` varchar(200) DEFAULT '' COMMENT '家庭住址',  `ages` varchar(5) DEFAULT NULL,  `telephone` int(11) DEFAULT '0' COMMENT '手机号码',  PRIMARY KEY (`id`),  KEY `index_ages` (`ages`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户表'               |+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)

 

转载地址:http://djhji.baihongyu.com/

你可能感兴趣的文章
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
如何自定义iOS中的控件
查看>>
iOS 开发百问
查看>>
Mac环境下svn的使用
查看>>
github简单使用教程
查看>>
如何高效利用GitHub
查看>>
环境分支-git版本管理
查看>>
uni-app 全局变量
查看>>
js判断空对象的几种方法
查看>>
java 不用递归写tree
查看>>
springboot2 集成Hibernate JPA 用 声明式事物
查看>>
fhs-framework jetcache 缓存维护之自动清除缓存
查看>>
SpringBoot 动态编译 JAVA class 解决 jar in jar 的依赖问题
查看>>
fhs-framework springboot mybatis 解决表关联查询问题的关键方案-翻译服务
查看>>
ZUUL2 使用场景
查看>>
Spring AOP + Redis + 注解实现redis 分布式锁
查看>>
elastic-job 和springboot 集成干货
查看>>
php开发微服务注册到eureka中(使用sidecar)
查看>>