1. 程式人生 > 其它 >使用pt-online-schema-change pt-osc 更改mysql大表

使用pt-online-schema-change pt-osc 更改mysql大表

安裝

centos

-- 安裝 yum 倉庫
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
-- 安裝 percona toolkit
yum install percona-toolkit -y

ubuntu

apt-get install percona-toolkit -y

語法

pt-online-schema-change [OPTIONS] DSN

其中 DSN 是指 Data Source Name,是連線資料庫的變數資訊。格式為key=value。

DSN 的 key 有:

  • A:預設字符集
  • D:資料庫
  • F:只從給定的檔案中讀取預設值
  • P:埠號
  • S:socket 檔案
  • h:主機 IP 或主機名
  • p:密碼
  • t:要更新的表
  • u:使用者名稱

執行前測試

# pt-online-schema-change --print --statistics \
    --progress time,30 --preserve-triggers --user=ptosc \
    --password=ptosc --alter 'modify c varchar(200) not null default ""' \
    h=127.0.1.1,P=3306,D=testdb,t=sbtest1 \
    
--pause-file=/tmp/aa.txt --max-load=threads_running=100,threads_connected=200 \ --critical-load=threads_running=1000 --chunk-size=1000 \ --alter-foreign-keys-method auto --dry-run
  • --print:列印工具執行的SQL語句。
  • --statistics:列印統計資訊。
  • --pause-file:當指定的檔案存在時,終止執行。
  • --max-load:超過指定負載時,暫定執行
  • --critical-load:超過指定負載時,終止執行
  • --chunck-size:指定每次複製的行數
  • --alter-foreign-keys-method:指定外來鍵更新方式
  • --progress:copy進度列印的頻率

執行

--dry-run 修改為 --execute

# pt-online-schema-change --print --statistics \
    --progress time,30 --preserve-triggers --user=ptosc \
    --password=ptosc --alter 'modify c varchar(200) not null default ""' \
    h=127.0.1.1,P=3306,D=testdb,t=sbtest1 \
    --pause-file=/tmp/aa.txt --max-load=threads_running=100,threads_connected=200 \
    --critical-load=threads_running=1000  --chunk-size=1000 \
    --alter-foreign-keys-method auto  --execute