1. 程式人生 > 其它 >MySQL命令列工具:percona-toolkit安裝使用初探

MySQL命令列工具:percona-toolkit安裝使用初探

導讀:percona-toolkit 源自 Maatkit 和 Aspersa 工具,這兩個工具是管理 mysql 的最有名的工具,現在 Maatkit 工具已經不維護了,請大家還是使用 percona-toolkit 吧!這些工具主要包括開發、效能、配置、監控、複製、系統、實用六大類,作為一個優秀的 DBA,裡面有的工具非常有用,如果能掌握並加以靈活應用,將能極大的提高工作效率。

一、 工具簡介

percona-toolkit 是一組高階命令列工具的集合,用來執行各種通過手工執行非常複雜和麻煩的 mysql 任務和系統任務,這些任務包括:

  • 檢查 master 和 slave 資料的一致性
  • 有效地對記錄進行歸檔
  • 查詢重複的索引
  • 對伺服器資訊進行彙總
  • 分析來自日誌和 tcpdump 的查詢
  • 當系統出問題的時候收集重要的系統資訊

二、工具安裝

①、下載安裝包

Percona-toolkit 安裝包:

最新 rpm 包:http://percona.com/get/percona-toolkit.rpm

最新編譯包:http://percona.com/get/percona-toolkit.tar.gz (本文略)

Perl-TermReadKey 安裝包:

訪問:http://pkgs.repoforge.org/perl-TermReadKey/ 下載最新 rpm 安裝包

②、安裝記錄

rpm -ivh perl-TermReadKey-2.30-3.el6.rfx.x86_64.rpm 
rpm -ivh percona-toolkit-2.2.16-1.noarch.rpm

若發現缺失元件,則使用 yum 線上安裝即可:

[root@localhost:~]# rpm -ivh percona-toolkit-2.2.16-1.noarch.rpm 
warning: percona-toolkit-2.2.16-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
error: Failed dependencies:
        perl(IO::Socket::SSL) is needed by percona-toolkit-2.2.16-1.noarch

 yum install -y perl-IO-Socket-SSL  

成功安裝後,系統會多出如下 pt 命令:

[root@localhost:~]# pt-
pt-align                  pt-find                   pt-mext                   pt-slave-delay            pt-table-usage
pt-archiver               pt-fingerprint            pt-mysql-summary          pt-slave-find             pt-upgrade
pt-config-diff            pt-fk-error-logger        pt-online-schema-change   pt-slave-restart          pt-variable-advisor
pt-deadlock-logger        pt-heartbeat              pt-pmp                    pt-stalk                  pt-visual-explain
pt-diskstats              pt-index-usage            pt-query-digest           pt-summary                
pt-duplicate-key-checker  pt-ioprofile              pt-show-grants            pt-table-checksum         
pt-fifo-split             pt-kill                   pt-sift                   pt-table-sync

三、使用初探

pt 有很多實用功能,但我就是奔著 alter 不鎖表去的,所以率先測試 pt-online-schema-change 這個欄位屬性修改工具。

  • pt-online-schema-change

①、功能介紹

如果沒有這個工具,直接使用 alter 修改欄位,過程如下:

  • 按照原始表(original_table)的表結構和 DDL 語句,新建一張不可見的臨時表;
  • 在原表上加 write lock,此時對原表的所有 U D I DDL 都是阻塞的;
  • 執行 insert into tmp_table select * from oldtable;
  • rename oldtable 和 tmp_table,再 drop oldtable;
  • 釋放 write lock。

為了避免鎖表,該換 pt-online-schema-change 出馬了!

pt-online-schema-change 的 工作過程解析如下:

  • 如果存在外來鍵,根據 alter-foreign-keys-method 引數的值,檢測外來鍵相關的表,做相應設定的處理。
  • 建立一個和你要執行 alter 操作的表一樣的新的空表結構(是 alter 之前的結構)。
  • 在新表執行 alter table 語句, 在原表中建立觸發器(3 個)三個觸發器分別對應 insert,update,delete 操作 從原表拷貝資料到臨時表,拷貝過程中通過原表上的觸發器在原表進行的寫操作都會更新到新建的臨時表。
  • Rename 原表到 old 表中,在把臨時表 Rename 為原表,預設最後將原表刪除,將原表上所建立的觸發器刪除。

因此,這個工具有如下限制:

  • 表必須是單一列的主鍵或者單一唯一鍵。
  • 若表存在外來鍵,則需要指定引數 --alter-foreign-keys-method=auto|rebuild_constraints|drop_swap|none,而且外來鍵不能有錯誤
  • 表上不能有觸發器,否則修改失敗。如果有觸發器,需要先刪除觸發器再執行 PT,最後再增加觸發器。

②、demo

pt-online-schema-change --user=pttest --password=pttest --host=192.168.1.100  --alter "ADD COLUMN CustomerId_pt INT DEFAULT 0  NOT NULL AFTER Int04;" D=config15_test,t=t_device --execute --alter-foreign-keys-method=auto

③、測試記錄

在測試資料庫上隨便匯入了一份現網的資料,居然就讓我碰到了觸發器和外來鍵,人品不錯!

測試資料,本機無密碼,所以直接執行:

#新增一個欄位,使用execute模式
pt-online-schema-change --alter "ADD COLUMN pt_test1 INT" D=pttest,t=idc_exit --execute

結果如下報錯:

The table `pttest`.`idc_exit` has triggers.  This tool needs to create its own triggers, so the table cannot already have triggers.

物件表存在觸發器!於是,將其中某個表的觸發器全部刪除,繼續執行,又發現如下錯誤:

You did not specify --alter-foreign-keys-method, but there are foreign keys that reference the table. Please read the tool's documentation carefully.

物件表存在外來鍵!

於是加入 –alter-foreign-keys-method=drop_swap 引數成功執行:

[root@localhost:~]# pt-online-schema-change --alter "ADD COLUMN pt_test2 INT" D=pttest,t=netdeviceport --execute --alter-foreign-keys-method=drop_swap 
Error setting innodb_lock_wait_timeout: DBD::mysql::db do failed: Variable 'innodb_lock_wait_timeout' is a read only variable [for Statement "SET SESSION innodb_lock_wait_timeout=1"].  The current value for innodb_lock_wait_timeout is 50.  If the variable is read only (not dynamic), specify --set-vars innodb_lock_wait_timeout=50 to avoid this warning, else manually set the variable and restart MySQL.

Error setting innodb_lock_wait_timeout: DBD::mysql::db do failed: Variable 'innodb_lock_wait_timeout' is a read only variable [for Statement "SET SESSION innodb_lock_wait_timeout=1"].  The current value for innodb_lock_wait_timeout is 50.  If the variable is read only (not dynamic), specify --set-vars innodb_lock_wait_timeout=50 to avoid this warning, else manually set the variable and restart MySQL.

No slaves found.  See --recursion-method if host SWEBMYVMM002293 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, 1
Child tables:
  `pttest`.`dwdm_idc_exit_circuit_map` (approx. 642 rows)
  `pttest`.`dwdm_idc_exit_wave_division_special_line_map` (approx. 330 rows)
  `pttest`.`net_device_link_map` (approx. 8745 rows)
Will use the drop_swap method to update foreign keys.
Altering `pttest`.`netdeviceport`...
Creating new table...
Created new table pttest._netdeviceport_new OK.
Altering new table...
Altered `pttest`.`_netdeviceport_new` OK.
2016-02-23T11:25:27 Creating triggers...
2016-02-23T11:25:27 Created triggers OK.
2016-02-23T11:25:27 Copying approximately 3076859 rows...
Copying `pttest`.`netdeviceport`:  20% 01:55 remain
Copying `pttest`.`netdeviceport`:  40% 01:29 remain
Copying `pttest`.`netdeviceport`:  58% 01:03 remain
Copying `pttest`.`netdeviceport`:  73% 00:42 remain
Copying `pttest`.`netdeviceport`:  86% 00:23 remain
Copying `pttest`.`netdeviceport`:  94% 00:10 remain
2016-02-23T11:28:46 Copied rows OK.
2016-02-23T11:28:46 Drop-swapping tables...
2016-02-23T11:28:46 Dropped and swapped tables OK.
Not dropping old table because --no-drop-old-table was specified.
2016-02-23T11:28:46 Dropping triggers...
2016-02-23T11:28:46 Dropped triggers OK.
Successfully altered `pttest`.`netdeviceport`.

新增欄位成功:

四、教程分享

除此之外,percona-toolkit 還有很多實用功能,比如:

pt-config-diff : 比較本地與遠端 MySQL 配置檔案差異

pt-heartbeat : MySQL 主從複製延遲監控

pt-slave-find : 查詢和列印所有從伺服器複製層級關係

pt-table-checksum : 主從複製一致性檢查

等等...

本文篇幅有限,且奔著不鎖表線上修改表字段功能去的,更多功能的探索這裡附上 percona-toolkit 中文使用教程:

下載地址