pt-online-schema-change使用

原理就再也不此處介紹了,你們能夠自行度娘html

1、使用限制

哪些ddl是不能夠作的,作了容易出錯:mysql

  1. 禁止建立惟一索引,會丟失數據,更加不容許添加 --alter-check=no,--check-unique-key-change=no
  2. 若是原表沒有主鍵,或者也沒有惟一索引,這些表是不容許用pt作DDL的
  3. 禁止對外鍵的表進行pt ddl
  4. 禁止對錶進行重命名
  5. 禁止對列進行重命名,若是必定要作,也必須先print出來檢測清楚列名是否正確
  6. 新增字段,NOT NULL必需要指定默認值
  7. 不容許刪除主鍵
    因爲pt觸發器原理,rowcopy會產業一堆的binlog,因此作以前要檢測binlog空間是否夠用,也要檢測數據空間多一倍表空間是否夠用
    禁止在業務高峯期進行pt-online-schema-change操做
    原表不能有觸發器
    MySQL最好設置爲innodb_autoinc_lock_mode=2,不然在高併發的寫入狀況下,很容易產生所等待以及死鎖
    master的表結構必須跟slave的表結構一致,不容許異構,不然pt-online-schema-change的原理就是會rename,而後slave不一致的表結構會被master覆蓋,謹記
    8.操做時,操做的表必須有主鍵或則惟一索引。必要要在業務低峯期

注意事項:
1.若是異常終止pt-online-schema-change程序,新表上的觸發器不會自動刪除,若是要刪除新表,那麼必需要先刪除觸發器,而後再刪除新表,不然向老表插入數據會由於找不到新表而報錯,致使老表寫入數據失敗sql

2.在添加有中文備註的列時記得要指定字符集 --charset=utf8 ;若是沒有指定字符集--charset=utf8 會致使表全部的表結構註釋亂碼 在改完變後查看錶結構註釋亂碼,這個可直接使用客戶端軟件修改回去,修改註釋不影響什麼數據庫

3.若是在誤在從庫上執行了pt-online-schema-change操做,未執行完成不要取消,等到執行完成了,在修改爲原來的狀態。
4.若是在誤在從庫上執行了pt-online-schema-change操做,未執行完成取消的話,刪除有 pt-online-schema-change在從庫上建立的臨時表和觸發器便可併發

2、具體使用實例

必須在master庫上執行下面的命令app

2.1給表刪除索引:

time pt-online-schema-change  --user=root --password='lzlzl89723' --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters   --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4   --alter "DROP key index_experienceId" D=appdb,t=hf_ad_experience_detail --print --execute

2.2給表hf_ad_experience_detail添加索引:

time pt-online-schema-change  --user=root --password='lzlzl89723' --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters   --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4  --alter "ADD INDEX index_experienceId (experienceId)" D=appdb,t=hf_ad_experience_detail --print --execute

2.3給表hf_user 添加索引報錯:

[root@localhost ~]# time pt-online-schema-change  --user=root --password='lzlzldleler' --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters   --critical-load threads_connected:500,threads_running:200 --max-load threads_connected:300,threads_running:150 --alter "ADD INDEX index_phone(phone)" D=appDB,t=hf_user --print --execute
Cannot connect to MySQL: DBI connect('appDB;host=localhost;port=3306;charset=utf8;mysql_read_default_group=client','root',...) failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) at /usr/local/percona-toolkit/bin/pt-online-schema-change line 2345.

real    0m0.246s
user    0m0.201s
sys 0m0.032s

報錯,當主機名爲localhost時,pt-online-schema-change連接數據庫 默認是找socket /var/lib/mysql/mysql.sock 文件來登陸數據庫,
然而本機的mysql的socket文件是/tmp/mysql.sock,致使pt-online-schema-change經過localhost主機名連接數據庫失敗。

因而受權127.0.0.1 做爲登陸mysql的帳戶:socket

grant all on *.*  to root@'127.0.0.1' identified by 'lzlzldleler';flush privileges;

成功解決上訴報錯。ide

2.3表hf_user_action 建立聯合索引:

表hf_user_action 字段 userId, dataType, opeUser 建立聯合索引:
當前此表記錄數爲: 4408200
建立此聯合索引用時:2分18s搞定高併發

time pt-online-schema-change  --user=root --password='lzlzldleler‘ --port=3306 --host=127.0.0.1 --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters   --critical-load threads_connected:500,threads_running:200 --max-load threads_connected:300,threads_running:150 --alter "ADD INDEX index_userId_dataType_opeUser(userId,dataType,opeUser)" D=appDB,t=hf_user_action --print --execute

2.4添加字段

time pt-online-schema-change  --user=root --password='lzlzldleler'  --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters   --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4    --alter='add column vip int'  D=appDB,t=lz_ad_experience_detail --print --execute

2.5刪除字段

time pt-online-schema-change  --user=root --password='lzlzldleler'  --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters   --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4    --alter='drop column vip int'  D=appDB,t=lz_ad_experience_detail --print --execute

2.6修改字段

time pt-online-schema-change  --user=root --password='lzlzldleler'  --port=3306 --host=localhost --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters   --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4    --alter='modify  column sid bigint(25)'  D=appDB,t=lz_ad_experience_detail --print --execute

2.7添加字段

time pt-online-schema-change --user=pt_tools --password='NITOcsFLV' --port=3306 --host=目標庫IP地址 --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4 --alter="ADD gray_phone tinyint(4) NOT NULL DEFAULT 0 COMMENT '號碼是否都爲灰色1:是;0:否'" D=db_crm,t=t_customer_ext --print --executecode

2.8修改字段長度

time pt-online-schema-change  --user=pt_tools --password='NITsFLV'  --port=3306 --host=目標庫IP地址 --charset=utf8 --check-interval=5 --chunk-size=1000 --chunk-size-limit=4 --chunk-time=1 --nocheck-replication-filters   --critical-load=threads_connected:500,threads_running:200 --max-load=threads_connected:300,threads_running:150 --max-lag=4  --alter "modify from_order_id varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '原訂單號'" D=db_mall_bill_test01,t=b_bill_allocate --print --execute --no-check-alter

簡單演示到此爲止
參考資料:
https://www.cnblogs.com/allenhu320/p/11358652.html
http://www.javashuo.com/article/p-mbshjkvw-km.html

相關文章
相關標籤/搜索