pt-archiver tools archive and restore data one case

DBA or operation and maintenance personnel often need to archive data regularly and clear, we can use percona of pt-archiver tools to accomplish this function, making data archiving has become convenient and simple.

Archive data one example:

The original table structure is:

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)


Archive to file:

# 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)

郑州不孕不育医院:http://byby.zztjyy.com/yiyuanzaixian/zztjyy//

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



Guess you like

Origin blog.51cto.com/14339005/2412108