当前位置:网站首页>pg_rewind 修复主备环境的时间线
pg_rewind 修复主备环境的时间线
2022-07-30 11:01:00 【墨天轮】
3.1.配置主从数据库
3.2.备库创建 pg_rewind 用户并授权
step 1.创建 rewind_user 用户
CREATE USER rewind_user LOGIN;GRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;GRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;
step 2.修改 postgresql.conf 配置
wal_log_hints = on # default:off 或者initdb初始化集群时启用数据校验full_page_writes = on # default:on
3.3.回退只读从库
3.3.1.激活从库(从库)
step 1.备节点执行切换,使从库能够读写
[[email protected] data]$ pg_ctl -D /opt/pg15beta1/data -l logfile promotewaiting for server to promote..... doneserver promoted[[email protected] data]$
step 2.查看备库状态
[[email protected] data]$ pg_controldata |grep stateDatabase cluster state: in production[[email protected] data]$
3.3.2.写入从库(从库)
从库激活后产生读写,使用pg_rewind修复从库,回退到只读从库
step 1.在数据库“postgres”中进行初始化(“pgbench -i”)。
[[email protected] data]$ pgbench -idropping old tables...NOTICE: table "pgbench_accounts" does not exist, skippingNOTICE: table "pgbench_branches" does not exist, skippingNOTICE: table "pgbench_history" does not exist, skippingNOTICE: table "pgbench_tellers" does not exist, skippingcreating tables...generating data (client-side)...100000 of 100000 tuples (100%) done (elapsed 0.37 s, remaining 0.00 s)vacuuming...creating primary keys...done in 1.50 s (drop tables 0.00 s, create tables 0.38 s, client-side generate 0.70 s, vacuum 0.08 s, primary keys 0.34 s).[[email protected] data]$
step 2.在从库中写入数据,生成wal日志
[[email protected] data]$ pgbench -M prepared -v -r -P 1 -c 4 -j 4 -T 10 -p 5432pgbench (15beta1)starting vacuum...end.starting vacuum pgbench_accounts...end.progress: 1.0 s, 73.0 tps, lat 52.092 ms stddev 44.973, 0 failedprogress: 2.0 s, 71.0 tps, lat 57.346 ms stddev 48.713, 0 failedprogress: 3.0 s, 102.0 tps, lat 39.707 ms stddev 20.312, 0 failedprogress: 4.0 s, 88.0 tps, lat 45.349 ms stddev 33.727, 0 failedprogress: 5.0 s, 73.0 tps, lat 53.625 ms stddev 47.683, 0 failedprogress: 6.0 s, 87.0 tps, lat 45.796 ms stddev 31.125, 0 failedprogress: 7.0 s, 96.0 tps, lat 42.844 ms stddev 20.861, 0 failedprogress: 8.0 s, 96.0 tps, lat 41.207 ms stddev 22.846, 0 failedprogress: 9.0 s, 100.0 tps, lat 40.096 ms stddev 20.878, 0 failedprogress: 10.0 s, 72.0 tps, lat 55.707 ms stddev 46.478, 0 failedtransaction type: <builtin: TPC-B (sort of)>scaling factor: 1query mode: preparednumber of clients: 4number of threads: 4maximum number of tries: 1duration: 10 snumber of transactions actually processed: 862number of failed transactions: 0 (0.000%)latency average = 46.447 mslatency stddev = 34.642 msinitial connection time = 6.326 mstps = 86.007961 (without initial connection time)statement latencies in milliseconds and failures: 0.002 0 \set aid random(1, 100000 * :scale) 0.001 0 \set bid random(1, 1 * :scale) 0.000 0 \set tid random(1, 10 * :scale) 0.001 0 \set delta random(-5000, 5000) 0.071 0 BEGIN; 0.136 0 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; 0.084 0 SELECT abalance FROM pgbench_accounts WHERE aid = :aid; 7.026 0 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; 27.587 0 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; 0.094 0 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); 11.445 0 END;[[email protected] data]$
此时从库和主库已不在同一个时间线上 。
step 3.查看主备库的时间线
查看从库时间线
[[email protected] ~]$ pg_controldata | grep TimeLineIDLatest checkpoint's TimeLineID: 2Latest checkpoint's PrevTimeLineID: 2[[email protected] ~]$
查看主库时间线
[[email protected] 20220729]$ pg_controldata | grep TimeLineIDLatest checkpoint's TimeLineID: 1Latest checkpoint's PrevTimeLineID: 1[[email protected] 20220729]$
3.3.3.修复从库,使之继续成为当前主库的从库
step 1.查看切换点
[[email protected] data]$ ll $PGDATA/pg_wal/*.history-rw-------. 1 postgres postgres 41 Jul 29 16:54 /opt/pg15beta1/data/pg_wal/00000002.history[[email protected] data]$
查看最新时间线的.history文件
[[email protected] data]$ cat /opt/pg15beta1/data/pg_wal/00000002.history1 0/9005F28 no recovery target specified[[email protected] data]$
step 2.从库激活时间开始产生的WAL必须全部在pg_wal目录中
从 000000020000000000000090 开始,所有的wal必须存在从库pg_wal目录中。如果已经覆盖了,必须从归档目录拷贝到从库pg_wal目录中,也可以直接将归档文件全部拷贝到pg_wal目录下 。
cp -R /opt/pg_backup/archive_log/20220729/* /opt/pg15beta1/data/pg_wal/
step 3.停掉从库
[[email protected] data]$ pg_ctl -D /opt/pg15beta1/data -l logfile stopwaiting for server to shut down.... doneserver stopped[[email protected] data]$
step 4.测试修复是否能够成功
[[email protected] data]$ pg_rewind -n -D /opt/pg15beta1/data --source-server="hostaddr=193.169.100.79 user=postgres port=5432"pg_rewind: servers diverged at WAL location 0/9005F28 on timeline 1pg_rewind: rewinding from last common checkpoint at 0/8000060 on timeline 1pg_rewind: Done![[email protected] data]$
step 5.可以修复,直接修复
[[email protected] ~]$ pg_rewind -D /opt/pg15beta1/data --source-server="hostaddr=193.169.100.79 user=postgres port=5432"pg_rewind: servers diverged at WAL location 0/9005F28 on timeline 1pg_rewind: rewinding from last common checkpoint at 0/8000060 on timeline 1pg_rewind: Done![[email protected] ~]$
修复时间线,会将归档日志恢复到错误的时间线的记录。
step 6.查看从库时间线
[[email protected] pg_wal]$ pg_controldata | grep TimeLineIDLatest checkpoint's TimeLineID: 1Latest checkpoint's PrevTimeLineID: 1[[email protected] pg_wal]$
step 7.修改备库配置文件 postgresql.auto.conf,保证从库能从主库获取到日志流
$ cat postgresql.auto.confprimary_conninfo = 'user=repl passfile=''/home/postgres/.pgpass'' host=193.169.100.79 port=5432 sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'restore_command = 'DATE=`date +%Y%m%d`; DIR="/opt/pg_backup/archive_log/$DATE"; cp $DIR/%f %p'archive_cleanup_command = 'DATE=`date +%Y%m%d`; DIR="/opt/pg_backup/archive_log/$DATE"; pg_archivecleanup $DIR %r'recovery_target_timeline = 'latest'
修复时间线,会将数据库的配置文件恢复到错误时间线的配置。
step 8.删除归档中错误时间线上产生的文件否则会在启动修复后的从库后,走到00000002时间线上,这是不想看到的
mkdir /opt/pg_backup/archive_log/error_tl_2mv 00000002*00 error_tl_2
step 9.创建从库标识文件
touch /opt/pg15beta1/data/standby.signal
边栏推荐
- TestNg整合Retry代码
- Meikle Studio-Look at Hongmeng Device Development Practical Notes 7-Network Application Development
- JSP 语法简介说明
- The configuration process and related syntax of writing markdown format notes in vscode
- 【Flume】batchSize和transactionCapacity区别
- Selected System Design | Design of CAN Bus Controller Based on FPGA (with Code)
- Taobao/Tmall taobao comments q&a list interface API
- Typroa alternative tool marktext
- Linux内核设计与实现(十)| 页高速缓存和页回写
- Easy gene: human tRNA gene loci showed age-related high DNA methylation | research articles
猜你喜欢
idea的package没有空心
2022全球数字经济大会人工智能专场:AI安全受高度关注
Introduction to IoT Technologies: Chapter 6
电压继电器HDY-A/1-220VAC-1
Neural Ordinary Differential Equations
Telerik2022 R2,有效的自动化测试
从数据流中快速查找中位数
Pytorch中 nn.Transformer的使用详解与Transformer的黑盒讲解
spark udf accepts and handles null values.
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
随机推荐
NLP领域的最新研究进展
AB test summary
spin lock和mutex使用场景的差异
京东校招笔试题+知识点总结
Meikle Studio-Look at Hongmeng Device Development Practical Notes 7-Network Application Development
Native js create table
电压跟随器不要随便加
加密和安全
高能产出!腾讯内部的MyCat中间件手册,理论实操齐下
淘宝/天猫淘宝评论问答列表接口 API
单片机开发之静态LED显示
Transfer Learning技术研修
WebAPI 复习
Swift common extension classes and simple encapsulation
数据库事务,JDBC操作和数据类型
AIX shell获取前几个月时间
我又造了个轮子:GrpcGateway
单片机开发之LCD1602显示实验
OC- about alloc and dealloc (haven't started writing yet)
Database dirty reads, non-repeatable reads, phantom reads and corresponding isolation levels