mysql管理工具percona-toolkit-3简单使用介绍

安装percona-toolkit-3

# yum localinstall percona-toolkit-3.0.-.el6.x86_64.rpm perl-TermReadKey.x86_64 :2.30-.el6 -y

1.pt-summary #显示和系统相关的基本信息:

[root@master ~]# pt-summary
# Percona Toolkit System Summary Report ######################
Date | -- :: UTC (local TZ: CST +)
Hostname | master
Uptime | day, :, user, load average: 0.00, 0.00, 0.00
System | VMware, Inc.; VMware Virtual Platform; vNone (Other)
Service Tag | VMware- 4d c6 e2 - f4 f3 f8
Platform | Linux
Release | CentOS release 6.5 (Final)
Kernel | 2.6.-.el6.x86_64
Architecture | CPU = -bit, OS = -bit
Threading | NPTL 2.12
SELinux | Disabled
Virtualized | VMWare
# Processor ##################################################
Processors | physical = , cores = , virtual = , hyperthreading = no
Speeds | 2x2592.
Models | 2xIntel(R) Core(TM) i7-6700HQ CPU @ .60GHz
Caches | 2x6144 KB
# Memory #####################################################
Total | .8G
Free | 123.4M
Used | physical = .7G, swap allocated = .9G, swap used = 5.8M, virtual = .7G
Shared | 0.0
Buffers | 40.6M
Caches | 905.3M
Dirty | kB
UsedRSS | 882.9M
Swappiness |
DirtyPolicy | ,
DirtyStatus | ,
...

2.pt-mysql-summary #查看mysql的各个统计信息:

[root@master ~]# pt-mysql-summary -S /data/mysql/mysql.sock --user=root --host=localhost --port= --password=
Warning: Using a password on the command line interface can be insecure.
# Percona Toolkit MySQL Summary Report #######################
System time | -- :: UTC (local TZ: CST +)
# Instances ##################################################
Port Data Directory Nice OOM Socket
===== ========================== ==== === ======
/data/mysql/data /data/mysql/mysql.sock
# MySQL Executable ###########################################
Path to executable | /usr/sbin/mysqld
Has symbols | No
# Report On Port ########################################
User |
Time | (CST)
Hostname | master
Version | 5.7.-- Percona XtraDB Cluster (GPL), Release rel15, Revision 7693d6e, WSREP version 29.20, wsrep_29.
Built On | Linux x86_64
Started | (up +::)
Databases |
Datadir | /data/mysql/data/
Processes | connected, running
Replication | Is not a slave, has slaves connected
Pidfile | /data/mysql/logs/mysql.pid (exists)
# Processlist ################################################ Command COUNT(*) Working SUM(Time) MAX(Time)
------------------------------ -------- ------- --------- ---------
Query
Sleep User COUNT(*) Working SUM(Time) MAX(Time)
------------------------------ -------- ------- --------- ---------
root
system user Host COUNT(*) Working SUM(Time) MAX(Time)
------------------------------ -------- ------- --------- --------- localhost db COUNT(*) Working SUM(Time) MAX(Time)
------------------------------ -------- ------- --------- ---------
NULL State COUNT(*) Working SUM(Time) MAX(Time)
------------------------------ -------- ------- --------- ---------
starting
wsrep
...
# Configuration File #########################################
Config File | /etc/my.cnf [mysqld]
user = mysql
innodb_buffer_pool_size = 1024M
datadir = /data/mysql/data
port =
server_id =
socket = /data/mysql/mysql.sock
pid-file = /data/mysql/logs/mysql.pid
log-error = /data/mysql/logs/error.log
log_warnings =
slow_query_log_file = /data/mysql/logs/slow.log
long_query_time = 0.1
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
wsrep_provider = /usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address = gcomm://192.168.3.12,192.168.3.13,192.168.3.198
binlog_format = ROW
default_storage_engine = InnoDB
wsrep_slave_threads =
wsrep_log_conflicts
innodb_autoinc_lock_mode =
wsrep_node_address = 192.168.3.12
wsrep_cluster_name = my-pxc-cluster
wsrep_node_name = pxc01
pxc_strict_mode = ENFORCING
wsrep_sst_method = xtrabackup-v2
wsrep_sst_auth = "sstuser:sstuser"
# Memory management library ##################################
jemalloc enabled in MySQL config for process with ID
Using jemalloc from /usr/lib64/libjemalloc.so.
# The End ####################################################

3.pt-slave-find #查找和显示指定的Master 有多少个Slave:

# pt-slave-find --host=10.10.76.96 --port= --user=root --password=
10.10.76.96
Version 5.5.-ucloudrel1-log
Server ID
Uptime +:: (started --01T14::)
Replication Is not a slave, has slaves connected, is not read_only
Filters
Binary logging MIXED
Slave status
Slave mode STRICT
Auto-increment increment , offset
InnoDB version 1.1.

4. pt-query-digest 慢日志查询:

# pt-query-digest /data/mysql/master-log.

5.pt-online-schema-change
#在线DDL操作改变表结构并且不阻塞,正常情况下在线ALTER一个表增加一个字段、一个索引的话MySQL会锁表,正常的流程如下:

①按照原始表(original_table)的表结构和DDL语句,新建一个不可见的临时表(tmp_table)
②在原表上加write lock,阻塞所有更新操作(insert、delete、update等)
③执行insert into tmp_table select * from original_table
④rename original_table和tmp_table,最后drop original_table
⑤释放 write lock

但是在线上直接操作的话会导致大量的写入阻塞,临时表可能会导致磁盘空间占满等问题,因此要使用pt-online-schema-change更新表结果的过程如下:
创建一个和原表相同表结构的表,比如table_tmp
执行表结构修改,并从原表copy数据到修改后的表
在原表上创建触发器,将copy过程中产生的数据更新到新表
copy完成,将新表rename成当前使用的表,删除旧表

使用pt-online-schema-change在线更新表,注意是在线、在线、在线更新表结构不停止业务使用的在线更新表结构:

# pt-online-schema-change --user=root --host=localhost --port= --password= --execute --alter "ADD COLUMN Job VARCHAR(20)" D=testdatabase,t=testtable2

6.修改表结构并保留原表
pt-online-schema-change, test111库的tbl_app_table新增app_status字段默认为0

[root@test7_chat_api_im ~]# pt-online-schema-change --host=10.10.191.89 --user=root --password= --port= --no-drop-old-table --execute --alter "add column app_status char(1) default 0" D=test111,t=tbl_app_table

No slaves found. See --recursion-method if host qas01 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
analyze_table, ,
copy_rows, , 0.25
create_triggers, ,
drop_triggers, ,
swap_tables, ,
update_foreign_keys, ,
Altering `test111`.`tbl_app_table`...
Creating new table...
Created new table test111._tbl_app_table_new OK.
Altering new table...
Altered `test111`.`_tbl_app_table_new` OK.
--08T17:: Creating triggers...
--08T17:: Created triggers OK.
--08T17:: Copying approximately rows...
--08T17:: Copied rows OK.
--08T17:: Analyzing new table...
--08T17:: Swapping tables...
--08T17:: Swapped original and new tables OK.
Not dropping old table because --no-drop-old-table was specified.
--08T17:: Dropping triggers...
--08T17:: Dropped triggers OK.
Successfully altered `test111`.`tbl_app_table`.
上一篇:051 日志案例分析(PV,UV),以及动态分区


下一篇:DiMP: Learning Discriminative Model Prediction for Tracking