mysql online modification pt-online-schema-change table structure of the large

Foreword

DDL operations MySQL large fields: Modified field, index, modify the field attributes, etc., prior to 5.1 it is very time-consuming and labor-intensive, especially MySQL service will have an impact. After 5.1 With the advent of online Plugin Innodb indexed improved a lot, but also affect the (shortened time), mainly during the change, will generate a mutex, blocking all operations on the entire table. But MySQL 5.6 to avoid the above situation, support the online DDL operations. But the majority are using version prior to 5.6, so the DDL operation and maintenance personnel operation has been a headache. Then how safely and quickly update the table structure without the lock table? Now to explain under percona-toolkit of pt-online-schema-change (abbreviation: OSC) instructions for use, you can solve the above problems.

In our previous approach in order not to affect the online business, we generally use: first online change table structure from the library, and then replace the line modifications from the library, so that Taiwan and Taiwan, and finally the main library to do some switching, a process will take a long time, and call the shots when switching libraries, the risk is very large, how can we make the time shorter, and can not block read-write case modify it online?

Online modifications may affect a large table:

  • Online modify a large table table structure execution time is often unpredictable, usually a long time
  • Since the modified table structure is a table level lock, and therefore when the modified table structure, table write operations affect
  • If long modify table structure, halfway modification failed due modify table structure is a transaction, so after the failure restores table structure, in the process table are locked can not be written
  • Large easily lead to modify the database table structure CPU, IO consumption and other properties, so that performance degradation MySQL server
  • Online modify table structure easily lead to large master-slave delay, thus affecting business reading

Currently InnoDB engine DDL is performed by the following steps:

  1. In the original table (original_table) table structure and DDL statements, a new invisible temporary table (tmp_table)
  2. In the original table plus write lock, blocking all update operations (insert, delete, update, etc.)
  3. 执行insert into tmp_table select * from original_table
  4. rename original_table和tmp_table,最后drop original_table
  5. Release write lock.

We can see when InnoDB DDL execution, the original table is read only can not write.

With the launch of perconal a tool pt-online-schema-change, which is characterized by the modification process will not cause obstruction to read and write.

working principle:

If the table has a foreign key, unless -alter-foreign-keys-method to specify a particular value, otherwise the tool will not be executed.

  1. Creating a and you want to perform the same table structure alter table space operations.
  2. Table structural modifications performed, and the data from the original table to the table to copy the modified table structure,
  3. Create a table in the original trigger process will copy data, the update operation updates the original table to the new table Note: If the table has been defined trigger this tool will not work.
  4. After the copy is complete, instead of the original table rename table with a new table, delete the original default table.

Installation pt-online-schema-change:

#安装依赖
yum install perl-DBI
yum install perl-DBD-MySQL
yum install perl-Time-HiRes
yum -y install perl-Digest-MD5
yum install perl-IO-Socket-SSL
# 切换目录
cd /usr/local/src
# 下载
wget percona.com/get/percona-toolkit.tar.gz
# 解压
tar -zvxf percona-toolkit.tar.gz
cd percona-toolkit-3.0.13/
# 安装perl依赖
yum install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
perl Makefile.PL
# 编译安装
make
make install
# 验证
pt-online-schema-change

Use pt-online-schema-change:

./bin/pt-online-schema-change --help you can view the arguments, we just want to modify a table structure, only need to know a few simple parameters on it

--user=        连接mysql的用户名
--password=    连接mysql的密码
--host=        连接mysql的地址
P=3306         连接mysql的端口号
D=             连接mysql的库名
t=             连接mysql的表名
--alter        修改表结构的语句
--execute      执行修改表结构
--charset=utf8 使用utf8编码,避免中文乱码
--no-version-check  不检查版本,在阿里云服务器中一般加入此参数,否则会报错

In order to avoid every time a bunch of input parameters, write a script reuse it, pt.sh:

#!/bin/bash
table=$1
alter_conment=$2

cnn_host='127.0.0.1'
cnn_user='user'
cnn_pwd='password'
cnn_db='database_name'

echo "$table"
echo "$alter_conment"
/root/percona-toolkit-2.2.19/bin/pt-online-schema-change --charset=utf8 --no-version-check --user=${cnn_user} --password=${cnn_pwd} --host=${cnn_host}  P=3306,D=${cnn_db},t=$table --alter="${alter_conment}" --execute

Table fields such as adding SQL statement:
the ALTER TABLE  test the ADD the COLUMN  status tinyint (4) the NOT NULL the DEFAULT 0;
then use the pt-online-schema-change, you can write
SH pt.sh the Test "the ALTER TABLE  test the ADD the COLUMN  status tinyint (4) the NOT NULL DEFAULT 0 "

Results are as follows:

other:

  • pt-online-schema-change tools there are many other parameters, there are many restrictions, such as restrictions on CPU, the number of threads, status, etc. from the library, but I did modify the structure of a table than 6000W, found almost no performance penalty , very smooth and very stable on the modified table structure, so the use of more conventional parameters to meet the basic business
  • Be sure to do business off-peak period, so as to ensure foolproof

File

1. Official Reference: HTTPS: //www.percona.com/doc/p ...
2. Official Download: HTTPS: //www.percona.com/downl ...

Reference: https://segmentfault.com/a/1190000014924677

Published 109 original articles · won praise 101 · views 360 000 +

Guess you like

Origin blog.csdn.net/Alen_xiaoxin/article/details/105095189