哪些ddl是不可以做的,做了容易出错:
注意事项:
1.如果异常终止pt-online-schema-change程序,新表上的触发器不会自动删除,如果要删除新表,那么必须要先删除触发器,然后再删除新表,否则向老表插入数据会因为找不到新表而报错,导致老表写入数据失败
2.在添加有中文备注的列时记得要指定字符集 --charset=utf8 ;如果没有指定字符集--charset=utf8 会导致表所有的表结构注释乱码 在改完变后查看表结构注释乱码,这个可直接使用客户端软件修改回去,修改注释不影响什么
3.如果在误在从库上执行了pt-online-schema-change操作,未执行完成不要取消,等到执行完成了,在修改成原来的状态。
4.如果在误在从库上执行了pt-online-schema-change操作,未执行完成取消的话,删除有 pt-online-schema-change在从库上创建的临时表和触发器即可
必须在master库上执行下面的命令
time pt-online-schema-change --user=root --password=‘lzlzl89723‘ --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4 --alter "DROP key index_experienceId" D=appdb,t=hf_ad_experience_detail --print --execute
time pt-online-schema-change --user=root --password=‘lzlzl89723‘ --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4 --alter "ADD INDEX index_experienceId (experienceId)" D=appdb,t=hf_ad_experience_detail --print --execute
[root@localhost ~]# time pt-online-schema-change --user=root --password=‘lzlzldleler‘ --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters --critical-load threads_connected:500,threads_running:200 --max-load threads_connected:300,threads_running:150 --alter "ADD INDEX index_phone(phone)" D=appDB,t=hf_user --print --execute
Cannot connect to MySQL: DBI connect(‘appDB;host=localhost;port=3306;charset=utf8;mysql_read_default_group=client‘,‘root‘,...) failed: Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2) at /usr/local/percona-toolkit/bin/pt-online-schema-change line 2345.
real 0m0.246s
user 0m0.201s
sys 0m0.032s
报错,当主机名为localhost时,pt-online-schema-change链接数据库 默认是找socket /var/lib/mysql/mysql.sock 文件来登录数据库,
然而本机的mysql的socket文件是/tmp/mysql.sock,导致pt-online-schema-change通过localhost主机名链接数据库失败。
于是授权127.0.0.1 作为登录mysql的账户:
grant all on *.* to root@‘127.0.0.1‘ identified by ‘lzlzldleler‘;flush privileges;
成功解决上诉报错。
表hf_user_action 字段 userId, dataType, opeUser 创建联合索引:
当前此表记录数为: 4408200
创建此联合索引用时:2分18s搞定
time pt-online-schema-change --user=root --password=‘lzlzldleler‘ --port=3306 --host=127.0.0.1 --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters --critical-load threads_connected:500,threads_running:200 --max-load threads_connected:300,threads_running:150 --alter "ADD INDEX index_userId_dataType_opeUser(userId,dataType,opeUser)" D=appDB,t=hf_user_action --print --execute
time pt-online-schema-change --user=root --password=‘lzlzldleler‘ --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4 --alter=‘add column vip int‘ D=appDB,t=lz_ad_experience_detail --print --execute
time pt-online-schema-change --user=root --password=‘lzlzldleler‘ --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4 --alter=‘drop column vip int‘ D=appDB,t=lz_ad_experience_detail --print --execute
time pt-online-schema-change --user=root --password=‘lzlzldleler‘ --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4 --alter=‘modify column sid bigint(25)‘ D=appDB,t=lz_ad_experience_detail --print --execute
简单演示到此为止
参考资料:
https://www.cnblogs.com/allenhu320/p/11358652.html
https://www.cnblogs.com/zping/p/11356468.html
原文:https://blog.51cto.com/wujianwei/2487025