您的位置:宽带测速网 > mysql教程 > mysql中如何使用percona的pt-archiver工具

mysql中如何使用percona的pt-archiver工具

2025-06-24 08:04来源:互联网 [ ]

DBA或者运维人员经常需定期对数据进行归档和清除,我们可以使用percona的pt-archiver工具能完成这一功能,使得数据归档变得方便简单。

归档数据一例:

原表结构为:

mysql>desc test.t1;

+-------+---------+------+-----+---------+-------+

| Field | Type| Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| id| int(11) | NO | PRI | NULL| |

| a| int(11) | YES | MUL | NULL| |

| b| int(11) | YES || NULL| |

+-------+---------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql>show create table test.t1 \G

*************************** 1. row ***************************

Table: t1

Create Table: CREATE TABLE `t1` (

`id` int(11) NOT NULL,

`a` int(11) DEFAULT NULL,

`b` int(11) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `a` (`a`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

1 row in set (0.00 sec)

归档到文件:

# pt-archiver --source h=192.168.140.51,u=root,p=123456,P=3306,D=test,t=t1 --file '/tmp/test.txt' --progress 500 --no-delete --statistics --where '1=1' --txn-size 1000 --no-check-charset

TIMEELAPSED COUNT

2019-06-19T16:14:48 0 0

2019-06-19T16:14:48 0500

2019-06-19T16:14:49 01000

2019-06-19T16:14:49 01000

Started at 2019-06-19T16:14:48, ended at 2019-06-19T16:14:49

Source: D=test,P=3306,h=192.168.140.51,p=...,t=t1,u=root

SELECT 1000

INSERT 0

DELETE 0

Action Count TimePct

select 10010.6883 72.33

print_file 10000.0149 1.57

commit 20.0004 0.04

other 00.2480 26.06

注意:

pt-archiver操作的表必须有主键。

--where '1=1'条件是导出全部数据,如果想导出部分数据,可以设置where条件,例如--where 'b>500'

--no-check-charset 设置不校验字符集,否则会报错:

Character set mismatch: --source DSN uses latin1, table uses utf8mb4. You can disable this check by specifying --no-check-charset.

常用的参数有:

--limit 10000 每次取1000行数据用pt-archive处理,Number of rows to fetch and archive per statement.

--txn-size 1000 设置1000行为一个事务提交一次,Number of rows pertransaction.

--where 'id<3000' 设置操作条件

--progress 1000每处理1000行输出一次处理信息

--statistics 输出执行过程及最后的操作统计。(只要不加上--quiet,默认情况下pt-archive都会输出执行过程的)

--charset=UTF8指定字符集为UTF8

--bulk-delete 批量删除source上的旧数据(例如每次1000行的批量删除操作)

--bulk-insert 批量插入数据到dest主机 (看dest的general log发现它是通过在dest主机上LOAD DATA LOCAL INFILE插入数据的)

--replace 将insert into 语句改成replace写入到dest库

--sleep 80每次归档了limit个行记录后的休眠80秒(单位为秒)

恢复数据例子:

恢复数据应使用load data命令

mysql>use peihy

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql>CREATE TABLE `t1` (

-> `id` int(11) NOT NULL,

-> `a` int(11) DEFAULT NULL,

-> `b` int(11) DEFAULT NULL,

-> PRIMARY KEY (`id`),

-> KEY `a` (`a`)

-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Query OK, 0 rows affected (0.18 sec)

mysql>load data infile '/tmp/test.txt' into table t1;

Query OK, 1000 rows affected (0.61 sec)

Records: 1000 Deleted: 0 Skipped: 0 Warnings: 0