1. For the installation of percona toolkit, please refer to: https://blog.csdn.net/yuanyk1222/article/details/100066788
2. Here are some uses of this tool:
pt-online-schema-change
The function can organize the table structure online, collect fragments, and add fields and indexes to large tables. Avoid blocking read and write operations by locking tables. However, for MySQL version 5.7, you can use online DDL directly without using this command.
There are the following tables in Mysql database:
mysql> desc t; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | k | int(11) | NO | MUL | NULL | | | b | varchar(10) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
If we want to add a field online, we can execute the following command:
[root@i-vvwtw5ne pt-kill]# pt-online-schema-change --user=root --password=Falsesoul1207 --host=192.192.18.34 --alter="add column c int after b" D=test,t=t --execute Found 1 slaves: i-s70m0mun Will check slave lag on: i-s70m0mun ******************************************************************* 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 /usr/bin/pt-online-schema-change line 6576. Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Altering `test`.`t`... Creating new table... Created new table test._t_new OK. Altering new table... Altered `test`.`_t_new` OK. 2019-12-08T00:17:36 Creating triggers... 2019-12-08T00:17:36 Created triggers OK. 2019-12-08T00:17:36 Copying approximately 1 rows... 2019-12-08T00:17:36 Copied rows OK. 2019-12-08T00:17:36 Swapping tables... 2019-12-08T00:17:36 Swapped original and new tables OK. 2019-12-08T00:17:36 Dropping old table... 2019-12-08T00:17:36 Dropped old table `test`.`_t_old` OK. 2019-12-08T00:17:36 Dropping triggers... 2019-12-08T00:17:36 Dropped triggers OK. Successfully altered `test`.`t`.
Check the original table structure again, and successfully add a field after the field b. a field c is added after the field b:
mysql> desc t; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | k | int(11) | NO | MUL | NULL | | | b | varchar(10) | YES | | NULL | | | c | int(11) | YES | | NULL | | | name | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
Let's focus on the use of Pt kill:
show PROCESSLIST results are as follows:
print queries with execution time more than 10s:
[root@i-vvwtw5ne ~]# pt-kill -h192.192.18.34 -P3306 -uroot -pFalsesoul1207 --busy-time=10 --print # 2019-12-08T19:53:06 KILL 67432267 (Query 33 sec) do sleep(60000)
kill queries that take more than 11s to execute:
[root@i-vvwtw5ne ~]# pt-kill -h192.192.18.34 -P3306 -uroot -pFalsesoul1207 --busy-time=10 --kill
Execute all queries whose Command condition is Query every 10s
[root@i-vvwtw5ne ~]# pt-kill -h192.192.18.34 -P3306 -uroot -pFalsesoul1207 --busy-time=10 --match-command=Query --victims=all --interval=10 --kill
print so the query whose "State" condition is "User sleep":
[root@i-vvwtw5ne ~]# pt-kill -h192.192.18.34 -P3306 -uroot -pFalsesoul1207 --busy-time=10 --match-state='User sleep' --victims=all --interval=10 --print # 2019-12-08T19:59:34 KILL 67432969 (Query 12 sec) do sleep(6000000)
Print the sql statement whose database is test state=User sleep every 10s
[root@i-vvwtw5ne ~]# pt-kill -h192.192.18.34 -P3306 -uroot -pFalsesoul1207 --match-db='test' --busy-time=10 --match-state='User sleep' --victims=all --interval=10 --print