溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL 5.7如何使用pt-online-schema-change對大表加字段

發(fā)布時間:2021-11-03 09:33:16 來源:億速云 閱讀:379 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章將為大家詳細講解有關(guān)MySQL 5.7如何使用pt-online-schema-change對大表加字段,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

業(yè)務需求:

對一張線上的3000W行的表加字段:

用普通方式,建議小表使用:

ALTER TABLE `idempotent`

ADD COLUMN `orderid` VARCHAR(20) NULL DEFAULT NULL COMMENT '業(yè)務單據(jù)號' AFTER `isdelete`;

ALTER TABLE ship_type ADD printer_name VARCHAR(50)  COMMENT '打印機名稱';

大表會鎖表。

對大表加字段,建議用pt-online-schema-change工具,可以不鎖表。

對表idempotent加字段orderid:

命令:

[root@DB001 ~]#pt-online-schema-change --user=root --password=123456 --host=192.168.1.18  --alter "ADD COLUMN orderid VARCHAR(20) NULL DEFAULT NULL COMMENT '業(yè)務單據(jù)號'" D=ora,t=idempotent --no-check-replication-filters  --execute

Found 1 slaves:

localhost.localdomain -> 192.168.1.19:socket

Will check slave lag on:

localhost.localdomain -> 192.168.1.19:socket

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

Altering `ora`.`idempotent`...

Creating new table...

Created new table ora._idempotent_new OK.

Altering new table...

Altered `ora`.`_idempotent_new` OK.

2019-10-23T19:01:00 Creating triggers...

2019-10-23T19:01:00 Created triggers OK.

2019-10-23T19:01:00 Copying approximately 24968401 rows...

Copying `ora`.`idempotent`:   4% 10:53 remain

Copying `ora`.`idempotent`:   8% 10:40 remain

Copying `ora`.`idempotent`:  11% 11:49 remain

Copying `ora`.`idempotent`:  14% 12:08 remain

Copying `ora`.`idempotent`:  17% 12:09 remain

Copying `ora`.`idempotent`:  19% 12:15 remain

Copying `ora`.`idempotent`:  22% 12:15 remain

Copying `ora`.`idempotent`:  24% 12:11 remain

Copying `ora`.`idempotent`:  27% 12:09 remain

Copying `ora`.`idempotent`:  29% 12:03 remain

Copying `ora`.`idempotent`:  31% 11:45 remain

Copying `ora`.`idempotent`:  34% 11:23 remain

Copying `ora`.`idempotent`:  37% 11:01 remain

Copying `ora`.`idempotent`:  39% 10:39 remain

Copying `ora`.`idempotent`:  42% 10:11 remain

Copying `ora`.`idempotent`:  44% 09:50 remain

Copying `ora`.`idempotent`:  47% 09:35 remain

Copying `ora`.`idempotent`:  49% 09:14 remain

Copying `ora`.`idempotent`:  52% 08:44 remain

Copying `ora`.`idempotent`:  55% 08:10 remain

Copying `ora`.`idempotent`:  59% 07:17 remain

Copying `ora`.`idempotent`:  63% 06:21 remain

Copying `ora`.`idempotent`:  67% 05:32 remain

Copying `ora`.`idempotent`:  71% 04:44 remain

Copying `ora`.`idempotent`:  75% 03:59 remain

Copying `ora`.`idempotent`:  80% 03:11 remain

Copying `ora`.`idempotent`:  84% 02:28 remain

Copying `ora`.`idempotent`:  88% 01:49 remain

Copying `ora`.`idempotent`:  92% 01:08 remain

Copying `ora`.`idempotent`:  96% 00:28 remain

2019-10-23T19:18:24 Copied rows OK.

2019-10-23T19:18:24 Analyzing new table...

2019-10-23T19:18:24 Swapping tables...

2019-10-23T19:18:24 Swapped original and new tables OK.

2019-10-23T19:18:24 Dropping old table...

2019-10-23T19:18:26 Dropped old table `ora`.`_idempotent_old` OK.

2019-10-23T19:18:26 Dropping triggers...

2019-10-23T19:18:26 Dropped triggers OK.

Successfully altered `ora`.`idempotent`.

You have mail in /var/spool/mail/root

整個過程持續(xù)10幾分鐘,不鎖表。

字段添加完成。

PS:

切記在業(yè)務低峰運行。

pt-online-schema-change --user=user --password=xxx --host=ip --port=3306 --alter “add column col1 VARCHAR(64) NULL COMMENT ‘訂單號’” D=sy,t=t1 --execute --charset=utf8  --nocheck-replication-filters --max-load=“Threads_running=20”

可以指定字符集,防止加好字段的表的注釋出現(xiàn)亂碼。

修改注釋:

ALTER TABLE idempotent MODIFY COLUMN `sysno` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '編號';

關(guān)于“MySQL 5.7如何使用pt-online-schema-change對大表加字段”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI