一、什么是pt-osc
Percona Toolkit是大名鼎鼎的MySQL企业级运维软件套件,是一个由Percona公司开发的一组命令行工具,为MySQL管理员提供许多有用的功能。
pt-osc即是Percona Toolkit软件包中的一个组件,它提供了一种原子化的,无锁的方式来执行数据库表迁移和更改,它相当于MySQL企业版中的Online DDL和Facebook开源的gh-ost库。 在使用pt-osc时,表仍可以被查询和修改和表函数就能继续工作。通过在Innodb表上执行原子(有锁)DDL语句,此工具可以执行表结构更改。
二、pt-osc 的优点
pt-osc作为Percona Toolkit中的一部分,具有以下几个特点:
1、可以在进行表更新时,不阻塞大部分查询
2、支持有锁的 DDL (ALTER) 语句
3、可操作性强,可以针对多数案例进行操作
4、支持数据更改和Schema变更的同时进行
三、pt-osc 的使用方法
pt-osc命令非常灵活,可以通过参数来修改要求,具体的使用方法如下:
pt-osc [OPTIONS] [DSN]
OPTIONS:
--alter ENGINE Specify storage engine for ALTER TABLE.
--ask-pass Prompt for password.
--check-alter-conflict Check for ALTER TABLE conflicts before executing changes. Deprecated, use --check-alter instead.
--check-alter-engine Check for ALTER TABLE engine change conflicts before executing changes.
--check-alter-online Check for ALTER TABLE online execution conflicts before executing changes.
--check-alter-same-name Check for ALTER TABLE using same table name in FROM and ALTER specification before executing changes.
--check-replication-filters Check replication filters before executing changes.
--chunk-size SIZE Determine number of rows per chunk for processing.
--config FILE Config filename (required).
--critical-load AVGLIMIT Block until the slave load average is below AVGLIMIT.
--drop-new-table Drop table after swapping table names.
--help Display this help and exit.
--ignore Ignore any errors encountered.
--ignore-version-check Ignore version check. (Default on.)
--limit TIME Maximum time to run quiesce (default '1s').
--max-load AVG1[,AVG2] Block when the average load exceeds AVG1. AVG2 ignored. (default '1000000000')
--max-lag LIMIT Block when the slave lag exceeds LIMIT.
--no-drop-new-table Do not drop new table after swapping table names.
--no-lock Do not acquire locks (sanity testing only).
--pause-file FILE Block until file FILE is deleted.
--pid-file FILE The pid file location.
--progress Show progress bar (you can pass SIZE)
--recursion METHOD Choose among NAIVE, PROCESSLIST and CRASH_SAFE (default: CRASH_SAFE).
--recurse-method METHOD Same as --recursion.
--recursion-method METHOD Same as --recursion.
--recursion-methods-deprecation-is-ok
Do not show deprecation warning for '--recurse-method'.
--recursion-methods-directory DIRECTORY
Directory for recursion method plugins.
--recursion-methods-early-lookup
Turn on early lookup for plugins.
--recursion-methods-force Force the desired recursion method plugin.
--recursion-methods-help Print help for recursion method plugins.
--recursion-methods-version Print version for recursion method plugins.
--rename-to NEWNAME Rename table to NEWNAME after swapping table names.
--skip-check-slave-lag Do not check slave lag.
--sleep N Sleep N seconds before starting quiesce (default '0s').
--statistics Gather statistics on performance.
--swap-tables Swap existing table with new table.
--version Output version information and exit.
--wrap [DB1.TABLE1[,]DB2.TABLE2[,DBN.TABLEN]
Wrap or ignore certain tables.
DSN:
DSN = [USER[:PASSWORD]@][:SOCKET/]DBNAME[/TABLE]
四、pt-osc 实践案例
以下是一个pt-osc实践案例。在数据库中,有一个表`t1` 存储用户消息,我们使用pt-osc 命令增加一个新字段`extra_message`。
# 现在我们要添加extra_message列到t1表 pt-osc --alter "ADD COLUMN extra_message varchar(50) NOT NULL DEFAULT ''" D=mydb,t=t1 --set-vars innodb_lock_wait_timeout=300 --set-vars LOCK_TIMEOUT=200
上述代码的含义是:在数据库mydb下的表t1中增加一个varchar类型的字段extra_message,长度为50个字符,且默认值为空字符串”。
五、总结
通过本文,我们可以简单了解到Percona Toolkit中一个非常强大的元素 pt-osc ,不仅可以让我们在进行表更新时不阻塞大部分查询,而且还支持有锁的 DDL (ALTER) 语句。因此,在我们进行数据库表的修改时,可以尽可能多地加入pt-osc这种工具,增加我们的工作效率和准确性。
原创文章,作者:DWNMS,如若转载,请注明出处:https://www.506064.com/n/333774.html
微信扫一扫
支付宝扫一扫