【工具】在mysql 5.6的环境下修改生产环境的表结构(在线ddl) 工具ptosc

随着需求的变化越来越快,在线修改表结构变得越来越需要。

在mysql5.6以前,mysql的修改表结构操作会锁表,这样就会造成开发人员或者DBA修改表结构必须要等到凌晨流量谷值或者停服修改。这样必定会流失一部分用户,在当下的互联网需求里是不太能容忍的。

在mysql5.6之后,虽然mysql支持在线ddl,但是一些操作仍然会造成锁表。详情请看mysql官方文档介绍。

所以博主在查阅工具手册,他人经验后,采取了一种相对稳妥的办法。采用pt-osc工具。

注:pt-osc不适用于

1:修改带有触发器的表

2:不能用于修改主键

//在线修改tb_pt_user表结构(在线ddl)
一、安装插件
1.sudo yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
2.yum list | grep percona-toolkit
3.冲突问题先手动安装yum list Percona-Server-shared-compat包
4.yum install Percona-Server-shared-compat.x86_64
5.sudo yum install percona-toolkit
二、数据更新处理
1.更新zwcmopenid 的NULL字段为''
update tb_pt_user set zwcmopenid = '' where zwcmopenid is null;
2.更新openid 的NULL字段为''
update tb_pt_user set openid = '' where openid is null;
3.更新usernmae 的NULL字段为''
update tb_pt_user set username = '' where username is null;
4.更新telephone 的NULL字段为 '0'
update tb_pt_user set telephone = '0' where telephone is null;
5.更新country 的NULL字段为''
update tb_pt_user set country = '' where country is null;
6.更新province 的NULL字段为''
update tb_pt_user set province = '' where province is null;
7.更新city的 NULL字段为''
update tb_pt_user set city = '' where city is null;
8.更新sex的 NULL字段为 0
update tb_pt_user set sex = 0 where sex is null;
9.更新picture 的NULL字段为''
update tb_pt_user set picture = '' where picture is null;
10.更新browsetime 的NULL 字段为 '1900-01-01 00:00:00'
update tb_pt_user set browsetime = '1900-01-01 00:00:00' where browsetime is null;
11.更新channelid 的NULL 字段为 0
update tb_pt_user set channelid = 0 where channelid is null;
12.更新ptid 的NULL字段为 0
update tb_pt_user set ptid = 0 where ptid is null;
13.更新template 的NULL字段为0
update tb_pt_user set template = 0 where template is null;
14.更新createtime 的NULL字段为 '1900-01-01 00:00:00'
update tb_pt_user set createtime = '1900-01-01 00:00:00' where createtime is null;
15.更新cardno 的NULL字段为 '0'
update tb_pt_user set cardno = '0' where cardno is null; 
16.更新bindbankcard 的NULL字段为0
update tb_pt_user set bindbankcard = 0 where bindbankcard is null;
三、通过pt-osc工具修改表结构
pt-online-schema-change h=127.0.0.1,u=root,p=,D=test_user,t=tb_pt_user --alter="MODIFY zwcmopenid varchar(64) NOT NULL DEFAULT '',MODIFY openid varchar(64) NOT NULL DEFAULT '',MODIFY username varchar(128) NOT NULL  DEFAULT '',DROP COLUMN password ,MODIFY telephone varchar(20) NOT NULL DEFAULT '0',MODIFY country varchar(16) NOT NULL DEFAULT '',MODIFY province varchar(16) NOT NULL DEFAULT '',MODIFY city varchar(16) NOT NULL DEFAULT '',MODIFY sex tinyint(1) NOT NULL DEFAULT '0',MODIFY picture varchar(255) NOT NULL DEFAULT '',DROP COLUMN email ,DROP COLUMN address ,MODIFY browsetime datetime NOT NULL DEFAULT '1900-01-01 00:00:00',DROP COLUMN device ,MODIFY channelid int(11) NOT NULL DEFAULT '0',DROP COLUMN channelname,MODIFY ptid int(11) NOT NULL DEFAULT '0',DROP COLUMN ptname ,MODIFY template tinyint(1) NOT NULL DEFAULT '0',MODIFY loadfull tinyint(1) NOT NULL DEFAULT '1',MODIFY createtime datetime NOT NULL DEFAULT '1900-01-01 00:00:00',MODIFY cardno varchar(32) NOT NULL DEFAULT '',MODIFY bindbankcard tinyint(4) NOT NULL DEFAULT '0',ADD COLUMN is_check_tel tinyint(1) NOT NULL DEFAULT 0,ADD INDEX idx_tel_zwcmopenid (telephone,zwcmopenid)" --set-vars --lock-wait-timeout=1 --ask-pass --execute --no-drop-old-table

原文地址:https://www.cnblogs.com/jysdhr/p/6722784.html