pt-online-change-schema 이란,
pt-online-change-schema 동작 방식
-
session parameter 변경 ( innodb_lock_wait_timeout / lock_wait_timeout / sql_mode 등 )
-
외래키 확인
-
TO-BE TABLE 생성
-
TO-BE TABLE DDL 실행
-
TRIGGER 생성
-
insert into select 구문으로 data 이관
-
trigger를 이용하여 AS-IS에서 발생했던 dml TO-BE TABLE 적용(PT-OSC 의 수행 방식은 트리거를 활용하여 데이터를 복제하는 방식)
-
RENAME TABLE
■ Perl 패키지 설치
rpm으로 설치하면 간편하지만, 가끔 rpm파일이 설치가 안될때가 있어 tar.gz으로 설치 진행
[root@jh-mysql ~]# yum -y install perl-DBI perl-DBD-MySQL perl-TermReadKey perl perl-IO-Socket-SSL perl-Time-HiRes perl-devel
■ percona-toolkit 설치
[root@jh-mysql ~]# wget percona.com/get/percona-toolkit.tar.gz
[root@jh-mysql ~]# tar -xvf percona-toolkit.tar.gz
[root@jh-mysql ~]# cd percona-toolkit-3.3.1/
[root@jh-mysql percona-toolkit-3.3.1]# perl ./Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for percona-toolkit
[root@jh-mysql percona-toolkit-3.3.1]# make && make install
...(생략)
Installing /usr/local/bin/pt-slave-delay
Installing /usr/local/bin/pt-sift
Installing /usr/local/bin/pt-diskstats
Installing /usr/local/bin/pt-visual-explain
Installing /usr/local/bin/pt-pg-summary
Installing /usr/local/bin/pt-variable-advisor
Installing /usr/local/bin/pt-index-usage
Installing /usr/local/bin/pt-duplicate-key-checker
Installing /usr/local/bin/pt-config-diff
Installing /usr/local/bin/pt-stalk
Appending installation info to /usr/lib64/perl5/perllocal.pod
■ alias 설정
[root@jh-mysql ~]# vi ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/mysql/bin
export PATH
alias pt-online-schema-change="/root/percona-toolkit-3.3.1/bin/pt-online-schema-change"
■ test용 테이블 생성
mysql> create table test_tbl ( a int primary key auto_increment, b varchar(100), c varchar(100));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test_tbl select null, uuid(),uuid() from information_schema.columns as a, information_schema.columns as b limit 1000000;
Query OK, 1000000 rows affected (17.32 sec)
Records: 1000000 Duplicates: 0 Warnings: 0
mysql> select sum(data_length+index_length)/1024/1024 from information_schema.tables where TABLE_NAME='test_tbl';
+-----------------------------------------+
| sum(data_length+index_length)/1024/1024 |
+-----------------------------------------+
| 101.62500000 |
+-----------------------------------------+
1 row in set (0.00 sec)
## 데이터 파일 크기 확인
[root@jh-mysql kim]# du -sh /data/kim/test_tbl.ibd
113M /data/kim/test_tbl.ibd
## 데이터 스토리지 여유 공간 확인
[root@jh-mysql kim]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda3 48G 6.7G 42G 14% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs 1.8G 8.4M 1.8G 1% /run
tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
tmpfs 354M 0 354M 0% /run/user/0
pt-online-schema-change
pt-online-schema-change
www.percona.com
pt-online-schema-change --alter "변경할 Alter 정보" D=데이터베이스,t=테이블명 \
--no-drop-old-table \
--no-drop-new-table \
--chunk-size=500 \
--chunk-size-limit=600 \
--defaults-file=/etc/my.cnf \
--host=127.0.0.1 \
--port=3306 \
--user=root \
--ask-pass \
--progress=time,30 \
--max-load="Threads_running=100" \
--critical-load="Threads_running=1000" \
--chunk-index=PRIMARY \
--charset=UTF8 \
--alter-foreign-keys-method=auto \
--preserve-triggers
--execute
* --no-drop-old-table
* 마이그레이션 후, 기존 테이블을 삭제 하지 않겠다는 옵션 입니다.
* host, port, user
* 스크립트를 실행할 대상 DB 정보입니다.
* chunk-size
* 한번에 복사할 데이터양을 얘기합니다. 데이터를 복사하는 단위 인 chunk 사이즈를 줄여서 데이터를 조금씩 복사(데이터 건 수를 줄여서)하여 MySQL의 부하를 낮추거나 IOPS 가 높은 스토리지 시스템을 통해서 새벽 시간때 작업할 경우 빠른 작업 완료를 위해 chunk 사이즈를 증가 시켜서 사용할 수도 있는 옵션입니다. chunk-size-limit=1 : chunk-size 의 max 배수값, 기본값 4.0
* --charset=UTF8
* 필수로 넣으셔야 합니다.
* 안넣을 경우 한글명 데이터들이 이관하면서 깨지는 경우가 생깁니다.
* --alter-foreign-keys-method=auto
* FK도 복사할것인지 여부입니다.
* 옵션값이 auto일 경우 최적화된 방식으로 FK를 복사합니다.
■ OSC 이용 테이블 컬럼 추가
[root@jh-mysql ~]# pt-online-schema-change --execute \
--recursion-method=none \
--host=localhost \
--defaults-file=/etc/my.cnf \
--chunk-size=500 --chunk-size-limit=1 \
--chunk-index=PRIMARY \
--sleep=0.1 --progress=time,10 \
--port=3306 --charset=UTF8 \
--user=root --ask-pass \
--alter "add column newcolumn varchar(30)" \
D=kim,t=test_tbl
No slaves found. See --recursion-method if host jh-mysql has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
possibly with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at /root/percona-toolkit-3.3.1/bin/pt-online-schema-change line 7119.
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
possibly with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at /root/percona-toolkit-3.3.1/bin/pt-online-schema-change line 7119.
# A software update is available:
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 `kim`.`test_tbl`...
Creating new table...
Created new table kim._test_tbl_new OK.
Altering new table...
Altered `kim`.`_test_tbl_new` OK.
2022-08-19T14:19:22 Creating triggers...
2022-08-19T14:19:22 Created triggers OK.
2022-08-19T14:19:22 Copying approximately 993705 rows...
Copying `kim`.`test_tbl`: 4% 03:49 remain
Copying `kim`.`test_tbl`: 8% 03:28 remain
Copying `kim`.`test_tbl`: 13% 03:16 remain
Copying `kim`.`test_tbl`: 17% 03:05 remain
Copying `kim`.`test_tbl`: 22% 02:54 remain
Copying `kim`.`test_tbl`: 26% 02:43 remain
Copying `kim`.`test_tbl`: 31% 02:33 remain
Copying `kim`.`test_tbl`: 35% 02:22 remain
Copying `kim`.`test_tbl`: 40% 02:12 remain
Copying `kim`.`test_tbl`: 44% 02:02 remain
Copying `kim`.`test_tbl`: 49% 01:52 remain
Copying `kim`.`test_tbl`: 53% 01:42 remain
Copying `kim`.`test_tbl`: 58% 01:32 remain
Copying `kim`.`test_tbl`: 62% 01:22 remain
Copying `kim`.`test_tbl`: 67% 01:12 remain
Copying `kim`.`test_tbl`: 72% 01:02 remain
Copying `kim`.`test_tbl`: 76% 00:52 remain
Copying `kim`.`test_tbl`: 81% 00:42 remain
Copying `kim`.`test_tbl`: 85% 00:32 remain
Copying `kim`.`test_tbl`: 89% 00:22 remain
Copying `kim`.`test_tbl`: 94% 00:12 remain
Copying `kim`.`test_tbl`: 98% 00:02 remain
2022-08-19T14:23:06 Copied rows OK.
2022-08-19T14:23:06 Analyzing new table...
2022-08-19T14:23:06 Swapping tables...
2022-08-19T14:23:06 Swapped original and new tables OK.
2022-08-19T14:23:06 Dropping old table...
2022-08-19T14:23:06 Dropped old table `kim`.`_test_tbl_old` OK.
2022-08-19T14:23:06 Dropping triggers...
2022-08-19T14:23:06 Dropped triggers OK.
Successfully altered `kim`.`test_tbl`.
## 생성 확인
mysql> desc kim.test_tbl;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| a | int | NO | PRI | NULL | auto_increment |
| b | varchar(100) | YES | | NULL | |
| c | varchar(100) | YES | | NULL | |
| newcolumn | varchar(30) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
※ 아래 에러 발생 시 1
Can't locate Digest/MD5.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /root/percona-toolkit-3.3.1/bin/pt-online-schema-change line 6340.
BEGIN failed--compilation aborted at /root/percona-toolkit-3.3.1/bin/pt-online-schema-change line 6340.
패키지 설치
[root@jh-mysql ~]# yum install perl-Digest-MD5
※ 아래 에러 발생 시 2
Cannot connect to MySQL: DBI connect('kim;mysql_read_default_file=/etc/my.cnf;host=localhost;charset=UTF8;mysql_read_default_group=client','root',...) failed: Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory at /root/percona-toolkit-3.3.1/bin/pt-online-schema-change line 2345.
mysql_native_password 옵션 사용
mysql> alter user root@localhost identified with mysql_native_password by 'root';
※ replication 구조일 시
복제 구성 환경에서는 실행하는 서버에서 -> Replica(slave) MySQL 로 접속이 가능해야 합니다.
slave에 접속하지 못하는 경우(예: CloudDB for MySQL) 에는 --recursion-method=none 옵션 사용
mysql> show tables;
+---------------+
| Tables_in_kim |
+---------------+
| _test_tbl_new |
| test_tbl |
+---------------+
2 rows in set (0.00 sec)
## 데이터파일 생성 확인
[root@jh-mysql ~]# du -sh /data/kim/*
113M /data/kim/test_tbl.ibd
112M /data/kim/_test_tbl_new.ibd
■ 추가작업 예시 (alter구문만 수정하여 사용)
## 컬럼 삭제
pt-online-schema-change --execute \
--recursion-method=none \
--host=localhost \
--defaults-file=/etc/my.cnf \
--chunk-size=500 --chunk-size-limit=1 \
--chunk-index=PRIMARY \
--sleep=0.1 --progress=time,10 \
--port=3306 --charset=UTF8 \
--user=root --ask-pass \
--alter "drop column newcolumn" \
D=kim,t=test_tbl
## b,c 컬럼 인덱스 생성
pt-online-schema-change --execute \
--recursion-method=none \
--host=localhost \
--defaults-file=/etc/my.cnf \
--chunk-size=500 --chunk-size-limit=1 \
--chunk-index=PRIMARY \
--sleep=0.1 --progress=time,10 \
--port=3306 --charset=UTF8 \
--user=root --ask-pass \
--alter "add index idx_test_1(b,c)" \
D=kim,t=test_tbl
## 인덱스 삭제
pt-online-schema-change --execute \
--recursion-method=none \
--host=localhost \
--defaults-file=/etc/my.cnf \
--chunk-size=500 --chunk-size-limit=1 \
--chunk-index=PRIMARY \
--sleep=0.1 --progress=time,10 \
--port=3306 --charset=UTF8 \
--user=root --ask-pass \
--alter "drop index idx_test_1" \
D=kim,t=test_tbl
db do failed: You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)[for Statement "CREATE TRIGGER `pt_osc_...`]
mysql> show variables like 'log_bin_trust_function_creators';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin_trust_function_creators | OFF |
+---------------------------------+-------+
1 row in set (0.00 sec)
mysql> set global log_bin_trust_function_creators=ON;
Query OK, 0 rows affected (0.00 sec)
참고
pt-online-schema-change에 숨겨진 무시무시한 이슈! – gywndi's database
Overview 최근들어 거의 연단위로 블로깅을 하나씩 올리는 듯 하는군요. 여기저기 시국이 어지럽고, 바쁘다는 말도 안되는 핑계를 무마시키기 위해.. 아무튼 간만에 블로깅 하나 올려봅니다. MySQL
gywn.net
[mysql] Percona pt-online-schema-change 설치 및 사용하기
Mysql에서 몇억건 이상의 대량의 데이터를 갖고 있는 테이블을 수정 하는것은 쉬운일이 아닙니다. 단순히 alter table 을 해버리면 4시간, 5시간 이상 수행되기 떄문인데요. 이를 해결 하기 위해 create
jojoldu.tistory.com
https://hoing.io/archives/10871
MySQL - Percona Toolkit - PT-ONLINE-SCHEMA-CHANGE
hoing.io
'DataBase > MySQL & MariaDB' 카테고리의 다른 글
[MySQL - mysql-proxy 설치] part 1. (0) | 2022.09.27 |
---|---|
[MySQL - phpMyAdmin 설치] (0) | 2022.09.27 |
[MySQL - Online DDL] part 2. 실습 (0) | 2022.08.25 |
[MySQL - Online DDL] part 1. 알고리즘 (0) | 2022.08.25 |
[MySQL - 테이블 단편화 optimize/analyze table 정리] (0) | 2022.07.30 |