pt-online-schema-change是percona公司开发的一个工具,在percona-toolkit包里面可以找到这个功能,它可以在线修改表结构
原理:
使用pt-online-schema-change执行SQL的日志
SQL语句:
ALTER TABLE
tmp_task_user ADD support tinyint(1) unsigned NOT NULL DEFAULT '1';
sh pt.sh tmp_task_user "ADD COLUMN support tinyint(1) unsigned NOT NULL DEFAULT '1'"
tmp_task_userADD COLUMN support tinyint(1) unsigned NOT NULL DEFAULT '1'No slaves found. See --recursion-method if host h=127.0.0.1,P=3306 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_db`.`tmp_task_user`...Creating new table...Created new table test_db._tmp_task_user_new OK.Altering new table...Altered `test_db`.`_tmp_task_user_new` OK.2018-05-14T18:14:21 Creating triggers...2018-05-14T18:14:21 Created triggers OK.2018-05-14T18:14:21 Copying approximately 6 rows...2018-05-14T18:14:21 Copied rows OK.2018-05-14T18:14:21 Analyzing new table...2018-05-14T18:14:21 Swapping tables...2018-05-14T18:14:21 Swapped original and new tables OK.2018-05-14T18:14:21 Dropping old table...2018-05-14T18:14:21 Dropped old table `test_db`.`_tmp_task_user_old` OK.2018-05-14T18:14:21 Dropping triggers...2018-05-14T18:14:21 Dropped triggers OK.Successfully altered `test_db`.`tmp_task_user`.
好处:
建议:
1.去官网下载对应的版本,官网下载地址: https://www.percona.com/downl...
2.下载解压之后就可以看到pt-online-schema-change
3.该工具需要一些依赖包,直接执行不成功时一般会有提示,这里可以提前yum安装
yum install perl-DBI yum install perl-DBD-MySQL yum install perl-Time-HiRes yum install perl-IO-Socket-SSL
1.参数
./bin/pt-online-schema-change --help 可以查看参数的使用,我们只是要修改个表结构,只需要知道几个简单的参数就可以了
--user= 连接mysql的用户名--password= 连接mysql的密码--host= 连接mysql的地址P=3306 连接mysql的端口号 D= 连接mysql的库名 t= 连接mysql的表名--alter 修改表结构的语句--execute 执行修改表结构--charset=utf8 使用utf8编码,避免中文乱码--no-version-check 不检查版本,在阿里云服务器中一般加入此参数,否则会报错
2.为避免每次都要输入一堆参数,写个脚本复用一下,pt.sh
#!/bin/bashtable=$1alter_conment=$2cnn_host='127.0.0.1'cnn_user='user'cnn_pwd='password'cnn_db='database_name'echo "$table"echo "$alter_conment"/root/percona-toolkit-2.2.19/bin/pt-online-schema-change --charset=utf8 --no-version-check --user=${cnn_user} --password=${cnn_pwd} --host=${cnn_host} P=3306,D=${cnn_db},t=$table --alter
"${alter_conment}" --execute
3.添加表字段
如添加表字段SQL语句为:
ALTER TABLE
tb_test ADD COLUMN
column1 tinyint(4) DEFAULT NULL;
那么使用pt-online-schema-change则可以这样写
sh pt.sh tb_test "ADD COLUMN column1 tinyint(4) DEFAULT NULL"
4.修改表字段
SQL语句:
ALTER TABLE
tb_test MODIFY COLUMN
num int(11) unsigned NOT NULL DEFAULT '0';
pt-online-schema-change工具:
sh pt.sh tb_test "MODIFY COLUMN num int(11) unsigned NOT NULL DEFAULT '0'"
5.修改表字段名
SQL语句:
ALTER TABLE
tb_test CHANGE COLUMN age adress varchar(30);
pt-online-schema-change工具:
sh pt.sh tb_test "CHANGE COLUMN age address varchar(30)"
6.添加索引
SQL语句:
ALTER TABLE
tb_test ADD INDEX idx_address(address);
pt-online-schema-change工具:
sh pt.sh tb_test "ADD INDEX idx_address(address)"
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30166976/viewspace-2711781/ ,如需转载,请注明出处,否则将追究法律责任。