mysql:pt-online-schema-change 在線修改表、刪除表數據【轉】


pt-online-schema-change
名字:pt-online-schema-change - ALTER tables without locking them. 在線改表

下載地址:
https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/6/x86_64/percona-toolkit-3.0.12-1.el6.x86_64.rpm
https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html#downloading
官方文檔:https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html

1.首先了解一下pt-online-schema-change工作原理,其實原理很簡單

  1. 1、如果存在外鍵,根據 alter-foreign-keys-method參數的值,檢測外鍵相關的表,做相應設置的處理。沒有使用 --alter-foreign-keys-method 指定特定的值,該工具不予執行
  2. 2、創建一個新的表,表結構為修改后的數據表,用於從源數據表向新表中導入數據。
  3. 3、創建觸發器,用於記錄從拷貝數據開始之后,對源數據表繼續進行數據修改的操作記錄下來,用於數據拷貝結束后,執行這些操作,保證數據不會丟失。如果表中已經定義了觸發器這個工具就不能工作了。
  4. 4、拷貝數據,從源數據表中拷貝數據到新表中。
  5. 5、修改外鍵相關的子表,根據修改后的數據,修改外鍵關聯的子表。
  6. 6、rename源數據表為old表,把新表rename為源表名,並將old表刪除。
  7. 7、刪除觸發器。


2.使用方法:

  1. pt-online-schema- change [OPTIONS] DSN
  2.  
  3. 具體說下 OPTIONS 的一些常用的參數:
  4. 復制代碼
  5.  
  6. --user:
  7. -u,連接的用戶名
  8.  
  9. --password:
  10. -p,連接的密碼
  11.  
  12. --database:
  13. -D,連接的數據庫
  14.  
  15. --port
  16. -P,連接數據庫的端口
  17.  
  18. --host:
  19. -h,連接的主機地址
  20.  
  21. --socket:
  22. -S,連接的套接字文件
  23.  
  24. --ask-pass
  25. 隱式輸入連接MySQL的密碼
  26.  
  27. --charset
  28. 指定修改的字符集
  29.  
  30. --defaults-file
  31. -F,讀取配置文件
  32.  
  33. --alter:
  34. 結構變更語句,不需要 alter table關鍵字。可以指定多個更改,用逗號分隔。如下場景,需要注意:
  35. 不能用 RENAME來重命名表。
  36. 列不能通過先刪除,再添加的方式進行重命名,不會將數據拷貝到新列。
  37. 如果加入的列非空而且沒有默認值,則工具會失敗。即其不會為你設置一個默認值,必須顯示指定。
  38. 刪除外鍵( drop foreign key constrain_name)時,需要指定名稱_constraint_name,而不是原始的constraint_name。
  39. 如: CONSTRAINT `fk_foo` FOREIGN KEY (`foo_id`) REFERENCES `bar` (`foo_id`),需要指定:--alter "DROP FOREIGN KEY _fk_foo"
  40.  
  41. --alter-foreign-keys-method
  42. 如何把外鍵引用到新表?需要特殊處理帶有外鍵約束的表,以保證它們可以應用到新表.當重命名表的時候,外鍵關系會帶到重命名后的表上。
  43. 該工具有兩種方法,可以自動找到子表,並修改約束關系。
  44. auto: 在rebuild_constraints和drop_swap兩種處理方式中選擇一個。
  45. rebuild_constraints:使用 ALTER TABLE語句先刪除外鍵約束,然后再添加.如果子表很大的話,會導致長時間的阻塞。
  46. drop_swap: 執行FOREIGN_KEY_CHECKS= 0,禁止外鍵約束,刪除原表,再重命名新表。這種方式很快,也不會產生阻塞,但是有風險:
  47. 1, 在刪除原表和重命名新表的短時間內,表是不存在的,程序會返回錯誤。
  48. 2, 如果重命名表出現錯誤,也不能回滾了.因為原表已經被刪除。
  49. none: 類似"drop_swap"的處理方式,但是它不刪除原表,並且外鍵關系會隨着重命名轉到老表上面。
  50.  
  51. --[no]check-alter
  52. 默認yes,語法解析。配合 --dry-run 和 --print 一起運行,來檢查是否有問題(change column,drop primary key)。
  53.  
  54. --max-lag
  55. 默認 1s。每個chunk拷貝完成后,會查看所有復制Slave的延遲情況。要是延遲大於該值,則暫停復制數據,直到所有從的滯后小於這個值,使用Seconds_Behind_Master。如果有任何從滯后超過此選項的值,則該工具將睡眠--check-interval指定的時間,再檢查。如果從被停止,將會永遠等待,直到從開始同步,並且延遲小於該值。如果指定--check-slave-lag,該工具只檢查該服務器的延遲,而不是所有服務器。
  56.  
  57. --check-slave-lag
  58. 指定一個從庫的DSN連接地址,如果從庫超過 --max-lag參數設置的值,就會暫停操作。
  59.  
  60. --recursion-method
  61. 默認是 show processlist,發現從的方法,也可以是host,但需要在從上指定report_host,通過show slave hosts來找到,可以指定none來不檢查Slave。
  62.  
  63. METHOD USES
  64. =========== ==================
  65. processlist SHOW PROCESSLIST
  66. hosts SHOW SLAVE HOSTS
  67. dsn=DSN DSNs from a table
  68. none Do not find slaves
  69. 指定 none則表示不在乎從的延遲。
  70.  
  71. --check-interval
  72. 默認是 1。--max-lag檢查的睡眠時間。
  73.  
  74. --[no]check-plan
  75. 默認yes。檢查查詢執行計划的安全性。
  76.  
  77. --[no]check-replication-filters
  78. 默認yes。如果工具檢測到服務器選項中有任何復制相關的篩選,如指定binlog_ignore_db和replicate_do_db此類。發現有這樣的篩選,工具會報錯且退出。因為如果更新的表 Master上存在,而Slave上不存在,會導致復制的失敗。使用–no-check-replication-filters選項來禁用該檢查。
  79.  
  80. --[no]swap-tables
  81. 默認yes。交換原始表和新表,除非你禁止 --[no]drop-old-table。
  82.  
  83. --[no]drop-triggers
  84. 默認yes,刪除原表上的觸發器。 --no-drop-triggers 會強制開啟 --no-drop-old-table 即:不刪除觸發器就會強制不刪除原表。
  85.  
  86. --new-table-name
  87. 復制創建新表的名稱,默認%T_new。
  88.  
  89. --[no]drop-new-table
  90. 默認yes。刪除新表,如果復制組織表失敗。
  91.  
  92. --[no]drop-old-table
  93. 默認yes。復制數據完成重命名之后,刪除原表。如果有錯誤則會保留原表。
  94.  
  95. --max-load
  96. 默認為Threads_running= 25。每個chunk拷貝完后,會檢查SHOW GLOBAL STATUS的內容,檢查指標(thread)是否超過了指定的閾值。如果超過,則先暫停。這里可以用逗號分隔,指定多個條件,每個條件格式:status指標=MAX_VALUE或者status指標:MAX_VALUE。如果不指定MAX_VALUE,那么工具會指定其為當前值的120%。
  97.  
  98. --critical-load
  99. 默認為Threads_running= 50。用法基本與--max-load類似,如果不指定MAX_VALUE,那么工具會指定其為當前值的200%。如果超過指定值,則工具直接退出,而不是暫停。
  100.  
  101. --default-engine
  102. 默認情況下,新的表與原始表是相同的存儲引擎,所以如果原來的表使用 InnoDB的,那么新表將使用InnoDB的。在涉及復制某些情況下,很可能主從的存儲引擎不一樣。使用該選項會默認使用默認的存儲引擎。
  103.  
  104. --set-vars
  105. 設置MySQL變量,多個用逗號分割。默認該工具設置的是: wait_timeout= 10000 innodb_lock_wait_timeout=1 lock_wait_timeout=60
  106.  
  107. --chunk-size-limit
  108. 當需要復制的塊遠大於設置的 chunk-size大小,就不復制.默認值是4.0,一個沒有主鍵或唯一索引的表,塊大小就是不確定的。
  109.  
  110. --chunk-time
  111. chunk-time執行的時間內,動態調整chunk-size的大小,以適應服務器性能的變化,該參數設置為0,或者指定chunk-size,都可以禁止動態調整。
  112.  
  113. --chunk-size
  114. 指定塊的大小,默認是 1000行,可以添加k,M,G后綴.這個塊的大小要盡量與--chunk-time匹配,如果明確指定這個選項,那么每個塊就會指定行數的大小.
  115.  
  116. --[no]check-plan
  117. 默認yes。為了安全,檢查查詢的執行計划.默認情況下,這個工具在執行查詢之前會先 EXPLAIN,以獲取一次少量的數據,如果是不好的EXPLAIN,那么會獲取一次大量的數據,這個工具會多次執行EXPALIN,如果EXPLAIN不同的結果,那么就會認為這個查詢是不安全的。
  118.  
  119. --statistics
  120. 打印出內部事件的數目,可以看到復制數據插入的數目。
  121.  
  122. --dry-run
  123. 創建和修改新表,但不會創建觸發器、復制數據、和替換原表。並不真正執行,可以看到生成的執行語句,了解其執行步驟與細節。 --dry-run與--execute必須指定一個,二者相互排斥。和--print配合最佳。
  124.  
  125. --execute
  126. 確定修改表,則指定該參數。真正執行。 --dry-run與--execute必須指定一個,二者相互排斥。
  127.  
  128. --print
  129. 打印 SQL語句到標准輸出。指定此選項可以讓你看到該工具所執行的語句,和--dry-run配合最佳。
  130.  
  131. --progress
  132. 復制數據的時候打印進度報告,二部分組成:第一部分是百分比,第二部分是時間。
  133.  
  134. --quiet
  135. -q,不把信息標准輸出。

3.安裝

  1. 下載最新的pt-online-schema- change
  2. [root@ 18c data]# wget https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/6/x86_64/percona-toolkit-3.0.12-1.el6.x86_64.rpm
  3. 安裝
  4. [root@ 18c data]# rpm -ivh percona-toolkit-3.0.12-1.el6.x86_64.rpm
  5. warning: percona-toolkit-3.0.12-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
  6. error: Failed dependencies:
  7. perl(DBI) >= 1.13 is needed by percona-toolkit-3.0.12-1.el6.x86_64
  8. perl(DBD::mysql) >= 1.0 is needed by percona-toolkit-3.0.12-1.el6.x86_64
  9. perl( Time::HiRes) is needed by percona-toolkit-3.0.12-1.el6.x86_64
  10. perl(IO::Socket::SSL) is needed by percona-toolkit-3.0.12-1.el6.x86_64
  11. perl(Term::ReadKey) is needed by percona-toolkit-3.0.12-1.el6.x86_64
  12.  
  13. 安裝相關的依賴包:
  14. [root@ 18c data]# yum install perl-TermReadKey.x86_64
  15. [root@ 18c data]# yum install perl-DBI
  16. [root@ 18c data]# yum install perl-DBD-MySQL
  17. [root@ 18c data]# yum install perl-Time-HiRes
  18. [root@ 18c data]# yum install perl-IO-Socket-SSL
  19. 然后再次安裝
  20. [root@ 18c data]# rpm -ivh percona-toolkit-3.0.12-1.el6.x86_64.rpm
  21. warning: percona-toolkit-3.0.12-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
  22. Preparing... ########################################### [100%]
  23. 1:percona-toolkit ########################################### [100%]
  24.  

至此軟件已經安裝好,下面就通過測試來了解如何使用該工具

創建測試數據

  1. mysql> show databases;
  2. + --------------------+
  3. | Database |
  4. + --------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | sys |
  9. + --------------------+
  10. 4 rows in set (0.00 sec)
  11.  
  12. mysql> create database test;
  13. Query OK, 1 row affected (0.00 sec)
  14.  
  15. mysql> use test;
  16. Database changed
  17. mysql> CREATE TABLE users (id int( 11) NOT NULL AUTO_INCREMENT COMMENT '用戶主鍵',name varchar(20) DEFAULT '' COMMENT '用戶名',PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用戶表';
  18. Query OK, 0 rows affected (0.04 sec)
  19. mysql> insert into users(name) values('test1'),('test2'),('test3');
  20. Query OK, 3 rows affected (0.01 sec)
  21. Records: 3 Duplicates: 0 Warnings: 0
  22. mysql> show tables;
  23. + ----------------+
  24. | Tables_in_test |
  25. + ----------------+
  26. | users |
  27. + ----------------+
  28. 1 row in set (0.00 sec)
  29.  
  30. mysql> select count(*) from users;
  31. + ----------+
  32. | count(*) |
  33. + ----------+
  34. | 3 |
  35. + ----------+
  36. 1 row in set (0.00 sec)

在正式使用之前,再次了解一下常用的幾個參數:

  1. --nocheck-replication-filters :不檢查復制過濾器,建議啟用。
  2. --no-check-binlog-format : 不檢查復制的binlog模式,要是binlog模式是ROW,則會報錯。
  3. --replicate-check-only :只顯示不同步的信息。
  4. --replicate= :把checksum的信息寫入到指定表中,建議直接寫到被檢查的數據庫當中。
  5. --chunk-size=50 Number of rows to select for each chunk copied. Allowable suffixes are k, M, G.默認1000
  6. --critical-load="Threads_running=600" 表示線程到600自動退出,默認值50
  7. --max-load="Threads_running=100" 表示線程到100暫停pt操作,默認25
  8. --progress=time,2 兩秒打印一次日志輸出

測試各種不同的場景

1.整理表碎片

  1.  
  2. sql語句: alter table table_name engine=innodb
  3. pt執行語句,以下 2種都可以
  4. ( 1)pt-online-schema-change --alter="engine innodb;" --execute D=test,t=users --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --socket=/tmp/mysql.sock --user=root --password=123 --charset=utf8 --progress=time,2 --chunk-size=100
  5. ( 2)pt-online-schema-change --alter="engine innodb;" --execute D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --progress=time,2 --chunk-size=100
  6. 具體執行過程:
  7. [root@ 18c ~]# pt-online-schema-change --alter="engine innodb;" --execute D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --progress=time,2 --chunk-size=100
  8. No slaves found. See --recursion-method if host 18c has slaves.
  9. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  10. Operation, tries, wait:
  11. analyze_table, 10, 1
  12. copy_rows, 10, 0.25
  13. create_triggers, 10, 1
  14. drop_triggers, 10, 1
  15. swap_tables, 10, 1
  16. update_foreign_keys, 10, 1
  17. Altering `test`.`users`...
  18. Creating new table...
  19. Created new table test._users_new OK.
  20. Altering new table...
  21. Altered `test`.`_users_new` OK.
  22. 2018-12-06T13:28:05 Creating triggers...
  23. 2018-12-06T13:28:05 Created triggers OK.
  24. 2018-12-06T13:28:05 Copying approximately 3 rows...
  25. 2018-12-06T13:28:05 Copied rows OK.
  26. 2018-12-06T13:28:05 Analyzing new table...
  27. 2018-12-06T13:28:05 Swapping tables...
  28. 2018-12-06T13:28:05 Swapped original and new tables OK.
  29. 2018-12-06T13:28:05 Dropping old table...
  30. 2018-12-06T13:28:05 Dropped old table `test`.`_users_old` OK.
  31. 2018-12-06T13:28:05 Dropping triggers...
  32. 2018-12-06T13:28:05 Dropped triggers OK.
  33. Successfully altered `test`.`users`.

2.添加字段:

  1. (1)添加一個字段
  2. alter table users add age varchar(10) NOT NUll DEFAULT '' COMMENT '性別';
  3. pt語句: pt- online-schema-change --alter="add age varchar(10) NOT NUll DEFAULT '' COMMENT '性別' ;" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  4. 具體執行過程
  5. [root@ 18c ~]# pt-online-schema-change --alter="add age varchar(10) NOT NUll DEFAULT '' COMMENT '性別' ;" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  6. No slaves found. See --recursion-method if host 18c has slaves.
  7. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  8. Operation, tries, wait:
  9. analyze_table, 10, 1
  10. copy_rows, 10, 0.25
  11. create_triggers, 10, 1
  12. drop_triggers, 10, 1
  13. swap_tables, 10, 1
  14. update_foreign_keys, 10, 1
  15. Altering `test`.`users`...
  16. Creating new table...
  17. CREATE TABLE `test`.`_users_new` (
  18. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶主鍵',
  19. `name` varchar(20) DEFAULT '' COMMENT '用戶名',
  20. PRIMARY KEY (`id`)
  21. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用戶表'
  22. Created new table test._users_new OK.
  23. Altering new table...
  24. ALTER TABLE `test`.`_users_new` add age varchar(10) NOT NUll DEFAULT '' COMMENT '��' ;
  25. Altered `test`.`_users_new` OK.
  26. 2018-12-06T13:36:55 Creating triggers...
  27. 2018-12-06T13:36:55 Created triggers OK.
  28. 2018-12-06T13:36:55 Copying approximately 3 rows...
  29. INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`) SELECT `id`, `name` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17461 copy table*/
  30. 2018-12-06T13:36:55 Copied rows OK.
  31. 2018-12-06T13:36:55 Analyzing new table...
  32. 2018-12-06T13:36:55 Swapping tables...
  33. RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
  34. 2018-12-06T13:36:55 Swapped original and new tables OK.
  35. 2018-12-06T13:36:55 Dropping old table...
  36. DROP TABLE IF EXISTS `test`.`_users_old`
  37. 2018-12-06T13:36:55 Dropped old table `test`.`_users_old` OK.
  38. 2018-12-06T13:36:55 Dropping triggers...
  39. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
  40. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
  41. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
  42. 2018-12-06T13:36:55 Dropped triggers OK.
  43. Successfully altered `test`.`users`.
  44. ( 2)添加2個或多個字段
  45. sql語句:alter table users add column address varchar(100) default '' comment='地址' after age,add column telephone int default 0 comment='手機號碼';
  46. pt語句:pt-online-schema- change --alter="add column address varchar(100) default '' comment '地址' after age,add column telephone int default 0 comment '手機號碼' after address ;" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  47. 具體執行過程:
  48. [root@ 18c ~]# pt-online-schema-change --alter="add column address varchar(100) default '' comment '地址' after age,add column telephone int default 0 comment '手機號碼' after address ;" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  49. No slaves found. See --recursion-method if host 18c has slaves.
  50. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  51. Operation, tries, wait:
  52. analyze_table, 10, 1
  53. copy_rows, 10, 0.25
  54. create_triggers, 10, 1
  55. drop_triggers, 10, 1
  56. swap_tables, 10, 1
  57. update_foreign_keys, 10, 1
  58. Altering `test`.`users`...
  59. Creating new table...
  60. CREATE TABLE `test`.`_users_new` (
  61. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶主鍵',
  62. `name` varchar(20) DEFAULT '' COMMENT '用戶名',
  63. `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性別',
  64. PRIMARY KEY (`id`)
  65. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用戶表'
  66. Created new table test._users_new OK.
  67. Altering new table...
  68. ALTER TABLE `test`.`_users_new` add column address varchar(100) default '' comment '��' after age,add column telephone int default 0 comment '����' after address ;
  69. Altered `test`.`_users_new` OK.
  70. 2018-12-06T14:02:40 Creating triggers...
  71. 2018-12-06T14:02:40 Created triggers OK.
  72. 2018-12-06T14:02:40 Copying approximately 3 rows...
  73. INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`) SELECT `id`, `name`, `age` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17584 copy table*/
  74. 2018-12-06T14:02:40 Copied rows OK.
  75. 2018-12-06T14:02:40 Analyzing new table...
  76. 2018-12-06T14:02:40 Swapping tables...
  77. RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
  78. 2018-12-06T14:02:40 Swapped original and new tables OK.
  79. 2018-12-06T14:02:40 Dropping old table...
  80. DROP TABLE IF EXISTS `test`.`_users_old`
  81. 2018-12-06T14:02:40 Dropped old table `test`.`_users_old` OK.
  82. 2018-12-06T14:02:40 Dropping triggers...
  83. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
  84. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
  85. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
  86. 2018-12-06T14:02:40 Dropped triggers OK.
  87. Successfully altered `test`.`users`.

3.修改字段

  1. sql語句: alter table users modify column address varchar(200) default '' comment '家庭住址';
  2. pt語句: pt- online-schema-change --alter="modify column address varchar(200) default '' comment '家庭住址';" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  3. 具體執行過程:
  4. [root@ 18c ~]# pt-online-schema-change --alter="modify column address varchar(200) default '' comment '家庭住址';" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  5. No slaves found. See --recursion-method if host 18c has slaves.
  6. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  7. Operation, tries, wait:
  8. analyze_table, 10, 1
  9. copy_rows, 10, 0.25
  10. create_triggers, 10, 1
  11. drop_triggers, 10, 1
  12. swap_tables, 10, 1
  13. update_foreign_keys, 10, 1
  14. Altering `test`.`users`...
  15. Creating new table...
  16. CREATE TABLE `test`.`_users_new` (
  17. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶主鍵',
  18. `name` varchar(20) DEFAULT '' COMMENT '用戶名',
  19. `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性別',
  20. `address` varchar(100) DEFAULT '' COMMENT '地址',
  21. `telephone` int(11) DEFAULT '0' COMMENT '手機號碼',
  22. PRIMARY KEY (`id`)
  23. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用戶表'
  24. Created new table test._users_new OK.
  25. Altering new table...
  26. ALTER TABLE `test`.`_users_new` modify column address varchar(200) default '' comment '家庭��';
  27. Altered `test`.`_users_new` OK.
  28. 2018-12-06T14:08:57 Creating triggers...
  29. 2018-12-06T14:08:57 Created triggers OK.
  30. 2018-12-06T14:08:57 Copying approximately 3 rows...
  31. INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17607 copy table*/
  32. 2018-12-06T14:08:57 Copied rows OK.
  33. 2018-12-06T14:08:57 Analyzing new table...
  34. 2018-12-06T14:08:57 Swapping tables...
  35. RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
  36. 2018-12-06T14:08:57 Swapped original and new tables OK.
  37. 2018-12-06T14:08:57 Dropping old table...
  38. DROP TABLE IF EXISTS `test`.`_users_old`
  39. 2018-12-06T14:08:57 Dropped old table `test`.`_users_old` OK.
  40. 2018-12-06T14:08:57 Dropping triggers...
  41. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
  42. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
  43. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
  44. 2018-12-06T14:08:57 Dropped triggers OK.
  45. Successfully altered `test`.`users`.

4.添加索引

  1. sql語句: alter table users add index index_age(age);
  2. pt語句:pt-online-schema- change --alter="add index index_age(age);" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  3. 具體執行過程:
  4. [root@ 18c ~]# pt-online-schema-change --alter="add index index_age(age);" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  5. No slaves found. See --recursion-method if host 18c has slaves.
  6. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  7. Operation, tries, wait:
  8. analyze_table, 10, 1
  9. copy_rows, 10, 0.25
  10. create_triggers, 10, 1
  11. drop_triggers, 10, 1
  12. swap_tables, 10, 1
  13. update_foreign_keys, 10, 1
  14. Altering `test`.`users`...
  15. Creating new table...
  16. CREATE TABLE `test`.`_users_new` (
  17. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶主鍵',
  18. `name` varchar(20) DEFAULT '' COMMENT '用戶名',
  19. `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性別',
  20. `address` varchar(200) DEFAULT '' COMMENT '家庭住址',
  21. `telephone` int(11) DEFAULT '0' COMMENT '手機號碼',
  22. PRIMARY KEY (`id`)
  23. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用戶表'
  24. Created new table test._users_new OK.
  25. Altering new table...
  26. ALTER TABLE `test`.`_users_new` add index index_age(age);
  27. Altered `test`.`_users_new` OK.
  28. 2018-12-06T14:11:39 Creating triggers...
  29. 2018-12-06T14:11:39 Created triggers OK.
  30. 2018-12-06T14:11:39 Copying approximately 3 rows...
  31. INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17622 copy table*/
  32. 2018-12-06T14:11:39 Copied rows OK.
  33. 2018-12-06T14:11:39 Analyzing new table...
  34. 2018-12-06T14:11:39 Swapping tables...
  35. RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
  36. 2018-12-06T14:11:39 Swapped original and new tables OK.
  37. 2018-12-06T14:11:39 Dropping old table...
  38. DROP TABLE IF EXISTS `test`.`_users_old`
  39. 2018-12-06T14:11:39 Dropped old table `test`.`_users_old` OK.
  40. 2018-12-06T14:11:39 Dropping triggers...
  41. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
  42. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
  43. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
  44. 2018-12-06T14:11:39 Dropped triggers OK.
  45. Successfully altered `test`.`users`.

5.刪除索引

  1. sql語句: alter table users drop index index_age;
  2. pt語句: pt-online-schema- change --alter="drop index index_age;" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  3. 具體執行過程:
  4. [root@ 18c ~]# pt-online-schema-change --alter="drop index index_age;" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  5. No slaves found. See --recursion-method if host 18c has slaves.
  6. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  7. Operation, tries, wait:
  8. analyze_table, 10, 1
  9. copy_rows, 10, 0.25
  10. create_triggers, 10, 1
  11. drop_triggers, 10, 1
  12. swap_tables, 10, 1
  13. update_foreign_keys, 10, 1
  14. Altering `test`.`users`...
  15. Creating new table...
  16. CREATE TABLE `test`.`_users_new` (
  17. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶主鍵',
  18. `name` varchar(20) DEFAULT '' COMMENT '用戶名',
  19. `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性別',
  20. `address` varchar(200) DEFAULT '' COMMENT '家庭住址',
  21. `telephone` int(11) DEFAULT '0' COMMENT '手機號碼',
  22. PRIMARY KEY (`id`),
  23. KEY `index_age` (`age`)
  24. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用戶表'
  25. Created new table test._users_new OK.
  26. Altering new table...
  27. ALTER TABLE `test`.`_users_new` drop index index_age;
  28. Altered `test`.`_users_new` OK.
  29. 2018-12-06T14:13:25 Creating triggers...
  30. 2018-12-06T14:13:25 Created triggers OK.
  31. 2018-12-06T14:13:25 Copying approximately 3 rows...
  32. INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17629 copy table*/
  33. 2018-12-06T14:13:25 Copied rows OK.
  34. 2018-12-06T14:13:25 Analyzing new table...
  35. 2018-12-06T14:13:25 Swapping tables...
  36. RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
  37. 2018-12-06T14:13:25 Swapped original and new tables OK.
  38. 2018-12-06T14:13:25 Dropping old table...
  39. DROP TABLE IF EXISTS `test`.`_users_old`
  40. 2018-12-06T14:13:25 Dropped old table `test`.`_users_old` OK.
  41. 2018-12-06T14:13:25 Dropping triggers...
  42. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
  43. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
  44. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
  45. 2018-12-06T14:13:25 Dropped triggers OK.
  46. Successfully altered `test`.`users`.

6.修改自增ID

  1. sql語句: alter table users modify column id bigint not null auto_increment;
  2. pt語句: pt-online-schema- change --alter="modify column id bigint not null auto_increment;" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  3.  
  4. 具體執行過程:
  5. [root@ 18c ~]# pt-online-schema-change --alter="modify column id bigint not null auto_increment;" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100
  6. No slaves found. See --recursion-method if host 18c has slaves.
  7. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  8. Operation, tries, wait:
  9. analyze_table, 10, 1
  10. copy_rows, 10, 0.25
  11. create_triggers, 10, 1
  12. drop_triggers, 10, 1
  13. swap_tables, 10, 1
  14. update_foreign_keys, 10, 1
  15. Altering `test`.`users`...
  16. Creating new table...
  17. CREATE TABLE `test`.`_users_new` (
  18. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶主鍵',
  19. `name` varchar(20) DEFAULT '' COMMENT '用戶名',
  20. `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性別',
  21. `address` varchar(200) DEFAULT '' COMMENT '家庭住址',
  22. `telephone` int(11) DEFAULT '0' COMMENT '手機號碼',
  23. PRIMARY KEY (`id`)
  24. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用戶表'
  25. Created new table test._users_new OK.
  26. Altering new table...
  27. ALTER TABLE `test`.`_users_new` modify column id bigint not null auto_increment;
  28. Altered `test`.`_users_new` OK.
  29. 2018-12-06T14:17:35 Creating triggers...
  30. 2018-12-06T14:17:35 Created triggers OK.
  31. 2018-12-06T14:17:35 Copying approximately 3 rows...
  32. INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17643 copy table*/
  33. 2018-12-06T14:17:35 Copied rows OK.
  34. 2018-12-06T14:17:35 Analyzing new table...
  35. 2018-12-06T14:17:35 Swapping tables...
  36. RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
  37. 2018-12-06T14:17:35 Swapped original and new tables OK.
  38. 2018-12-06T14:17:35 Dropping old table...
  39. DROP TABLE IF EXISTS `test`.`_users_old`
  40. 2018-12-06T14:17:35 Dropped old table `test`.`_users_old` OK.
  41. 2018-12-06T14:17:35 Dropping triggers...
  42. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
  43. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
  44. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
  45. 2018-12-06T14:17:35 Dropped triggers OK.
  46. Successfully altered `test`.`users`.

7.修改列名,並添加索引

  1. sql語句: alter table test change column age ages varchar(5) default '' comment '性別新' after address,add index index_ages(ages);
  2. pt語句:pt-online-schema- change --alter="change column age ages varchar(5) after address,add index index_ages(ages);" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100 --no-check-alter
  3.  
  4. 具體執行過程:
  5. [root@ 18c ~]# pt-online-schema-change --alter="change column age ages varchar(5) after address,add index index_ages(ages);" --execute --print --max-lag=5 D=test,t=users,u=root,p=123,S=/tmp/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8 --chunk-size=100 --no-check-alter
  6. No slaves found. See --recursion-method if host 18c has slaves.
  7. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  8. Operation, tries, wait:
  9. analyze_table, 10, 1
  10. copy_rows, 10, 0.25
  11. create_triggers, 10, 1
  12. drop_triggers, 10, 1
  13. swap_tables, 10, 1
  14. update_foreign_keys, 10, 1
  15. Altering `test`.`users`...
  16. Renaming columns:
  17. age to ages
  18. Creating new table...
  19. CREATE TABLE `test`.`_users_new` (
  20. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  21. `name` varchar(20) DEFAULT '' COMMENT '用戶名',
  22. `age` varchar(10) NOT NULL DEFAULT '' COMMENT '性別',
  23. `address` varchar(200) DEFAULT '' COMMENT '家庭住址',
  24. `telephone` int(11) DEFAULT '0' COMMENT '手機號碼',
  25. PRIMARY KEY (`id`)
  26. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用戶表'
  27. Created new table test._users_new OK.
  28. Altering new table...
  29. ALTER TABLE `test`.`_users_new` change column age ages varchar(5) after address,add index index_ages(ages);
  30. Altered `test`.`_users_new` OK.
  31. 2018-12-06T14:55:27 Creating triggers...
  32. 2018-12-06T14:55:27 Created triggers OK.
  33. 2018-12-06T14:55:27 Copying approximately 3 rows...
  34. INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `ages`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE /*pt-online-schema-change 17822 copy table*/
  35. 2018-12-06T14:55:27 Copied rows OK.
  36. 2018-12-06T14:55:27 Analyzing new table...
  37. 2018-12-06T14:55:27 Swapping tables...
  38. RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
  39. 2018-12-06T14:55:27 Swapped original and new tables OK.
  40. 2018-12-06T14:55:27 Dropping old table...
  41. DROP TABLE IF EXISTS `test`.`_users_old`
  42. 2018-12-06T14:55:27 Dropped old table `test`.`_users_old` OK.
  43. 2018-12-06T14:55:27 Dropping triggers...
  44. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
  45. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
  46. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
  47. 2018-12-06T14:55:27 Dropped triggers OK.
  48. Successfully altered `test`.`users`.

上面做了這么多的操作,現在來看看測試表已經變成啥模樣了

    1. mysql> show create table users;
    2. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    3. | Table | Create Table |
    4. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    5. | users | CREATE TABLE `users` (
    6. `id` bigint(20) NOT NULL AUTO_INCREMENT,
    7. `name` varchar(20) DEFAULT '' COMMENT '用戶名',
    8. `address` varchar(200) DEFAULT '' COMMENT '家庭住址',
    9. `ages` varchar(5) DEFAULT NULL,
    10. `telephone` int(11) DEFAULT '0' COMMENT '手機號碼',
    11. PRIMARY KEY ( `id`),
    12. KEY `index_ages` (`ages`)
    13. ) ENGINE=InnoDB AUTO_INCREMENT= 4 DEFAULT CHARSET=utf8 COMMENT='用戶表' |
    14. +-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    15. 1 row in set (0.00 sec)

 

轉自

(3條消息)mysql:pt-online-schema-change 在線修改表_雨花石-CSDN博客_pt-online-schema-change https://blog.csdn.net/shiyu1157758655/article/details/84854513

參考

(3條消息)案例 - optimize table 的一些坑_weixin_34248023的博客-CSDN博客_optimize table https://blog.csdn.net/weixin_34248023/article/details/91617531

(3條消息)[mysql]清除單表大量數據方法(需保留部分數據)_yyongsheng的博客-CSDN博客_mysql truncate清除部分數據 https://blog.csdn.net/yyongsheng/article/details/82938683

MySQL大表刪除工具pt-osc​ - 雲+社區 - 騰訊雲 https://cloud.tencent.com/developer/article/1533722

PT-OSC在線DDL變更工具使用攻略 - MySQL數據庫技術棧的個人空間 - OSCHINA https://my.oschina.net/u/3678773/blog/4467882

(3條消息)mysql 案例~關於pt-osc工具的用途_weixin_34212189的博客-CSDN博客_mysql ptosc 工具 https://blog.csdn.net/weixin_34212189/article/details/93262658

pt-osc原理 - 茁壯的小草 - 博客園 https://www.cnblogs.com/mysql-dba/p/9901632.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM