一、什麼是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/zh-tw/n/333774.html