千家信息网

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

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,这篇文章给大家分享的是有关mysql中如何使用percona的pt-archiver工具的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。DBA或者运维人员经常需定期对数据进行
千家信息网最后更新 2025年02月05日mysql中如何使用percona的pt-archiver工具

这篇文章给大家分享的是有关mysql中如何使用percona的pt-archiver工具的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

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

TIME ELAPSED COUNT

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

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

2019-06-19T16:14:49 0 1000

2019-06-19T16:14:49 0 1000

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 Time Pct

select 1001 0.6883 72.33

print_file 1000 0.0149 1.57

commit 2 0.0004 0.04

other 0 0.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

感谢各位的阅读!关于"mysql中如何使用percona的pt-archiver工具"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0