穷鬼玩PolarDB RAC系列 | 实时?归档?
参考文档点击文末阅读原文打开; 推荐《最好的PostgreSQL学习镜像》;
穷鬼玩PolarDB RAC系列 | 实时归档
穷鬼玩PolarDB RAC一写多读集群系列已经写了几篇:
《在Docker容器中用loop设备模拟共享存储》 《如何搭建PolarDB容灾(standby)节点》 《共享存储在线扩容》 《计算节点 Switchover》 《在线备份》 《在线归档》
本篇文章介绍一下如何进行实时归档? 实验环境依赖《在Docker容器中用loop设备模拟共享存储》 , 如果没有环境, 请自行参考以上文章搭建环境.
还需要参考如下文档:
https://www.postgresql.org/docs/current/app-pgreceivewal.html
在线归档需要等wal文件切换时才会进行copy, PolarDB默认的wal文件大小是1GB, 这样可能会有一些弊端:
在主节点进行归档时, copy可能会带来较大的突发IO. 如果存储故障, 可能丢失未归档的wal日志, 最多1GB.
所以这篇文档想介绍一下实时归档.注意: 如果你已经建立了PolarDB容灾(standby)节点, 可以忽略这篇文档, 因为容灾节点本身就有实时接收WAL, 在容灾节点归档即可, 没有必要再接收一份.
DEMO
1、新建docker容器 pb4, 作为实时归档机.
cd ~/data_volumn
PWD=`pwd`
docker run -d -it -v $PWD:/data -P --shm-size=1g --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name pb4 registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04 bash
在宿主机映射到docker容器的目录内创建实时归档目录.
# 进入容器pb4
docker exec -ti pb4 bash
# 创建实时归档目录
mkdir /data/polardb_wal_archive/
将编译好的二进制拷贝到pb4的HOME目录, 便于调用:
$ cp -r /data/polardb/tmp_polardb_pg_15_base ~/
$ which psql
/home/postgres/tmp_polardb_pg_15_base/bin/psql
2、pb1 primary节点, 配置pg_hba.conf, 允许备份机使用流复制链接
# 进入容器pb1
docker exec -ti pb1 bash
# 由于这里pb4网段已经配置了, 所以不需要再配置, 如下:
cat ~/primary/pg_hba.conf
host replication postgres 172.17.0.0/16 trust
# 如果有修改, 需要reload
# pg_ctl reload -D ~/primary
3、pb1 primary节点, 创建 replication slot, 用于确保在线归档未接收的WAL不会被主节点删除.
psql -p 5432 -d postgres -c "SELECT pg_create_physical_replication_slot('online_archwal_1');"
pg_create_physical_replication_slot
-------------------------------------
(online_archwal_1,)
(1 row)
4、pb4, 实时归档机, 使用pg_receivewal工具开始实时归档
编辑接收wal的脚本, 如果pg_receivewal进程退出了可以自动重启
vi /home/postgres/wal.sh
脚本如下:
#!/bin/bash
whiletrue
do
if pgrep -x "pg_receivewal" >/dev/null
then
echo"pg_receivewal 进程正在运行"
else
# 如果想减少频繁IO, 可以去掉 --synchronous
nohup pg_receivewal --synchronous -D /data/polardb_wal_archive -S online_archwal_1 -h 172.17.0.2 -p 5432 -U postgres -d postgres >>/data/polardb_wal_archive/arch.log 2>&1 &
fi sleep 10
done
修改脚本权限
chmod 555 /home/postgres/wal.sh
开启归档
nohup /home/postgres/wal.sh >/dev/null 2>&1 &
5、检查归档是否正常
pb4, 实时归档机. 接收已经开始了.
$ ll -h /data/polardb_wal_archive
total 1.1G
drwxr-xr-x 4 postgres postgres 128 Dec 19 11:17 ./
drwxr-xr-x 9 postgres postgres 288 Dec 18 17:53 ../
-rw------- 1 postgres postgres 1.0G Dec 19 11:17 000000010000000200000000.partial
-rw-r--r-- 1 postgres postgres 334 Dec 19 11:16 arch.log
pb1, PolarDB primary节点. 查看复制槽, 可以看到连接已建立.
postgres=# select * from pg_stat_replication where application_name='pg_receivewal';
-[ RECORD 1 ]----+------------------------------
pid | 4204
usesysid | 10
usename | postgres
application_name | pg_receivewal
client_addr | 172.17.0.5
client_hostname |
client_port | 52744
backend_start | 2024-12-19 11:17:10.982052+08
backend_xmin |
state | streaming
sent_lsn | 2/658
write_lsn | 2/658
flush_lsn | 2/658
replay_lsn |
write_lag | 00:00:12.95224
flush_lag | 00:01:14.971488
replay_lag | 00:01:14.971488
sync_priority | 0
sync_state | async
reply_time | 2024-12-19 11:18:25.957865+08
6、在pb1, PolarDB primary节点写入一些数据, 可以看到pg_stat_replication.sent_lsn的变化.
postgres=# create table test (id int, info text, ts timestamp);
CREATE TABLE
postgres=# insert into test select generate_series(1,100),md5(random()::text),clock_timestamp();
INSERT 0 100
接收wal位置已推进
postgres=# select * from pg_stat_replication where application_name='pg_receivewal';
-[ RECORD 1 ]----+------------------------------
pid | 4285
usesysid | 10
usename | postgres
application_name | pg_receivewal
client_addr | 172.17.0.5
client_hostname |
client_port | 43402
backend_start | 2024-12-19 11:32:27.928248+08
backend_xmin |
state | streaming
sent_lsn | 2/40021960
write_lsn | 2/40021960
flush_lsn | 2/40021960
replay_lsn |
write_lag | 00:00:05.118327
flush_lag | 00:00:10.002961
replay_lag | 00:00:10.002961
sync_priority | 0
sync_state | async
reply_time | 2024-12-19 11:32:37.937766+08
slot状态正常
postgres=# select * from pg_replication_slots where slot_name='online_archwal_1';
-[ RECORD 1 ]-------+-----------------
slot_name | online_archwal_1
plugin |
slot_type | physical
datoid |
database |
temporary | f
active | t
active_pid | 4285
xmin |
catalog_xmin |
restart_lsn | 2/40021960
confirmed_flush_lsn |
wal_status | reserved
safe_wal_size |
two_phase | f
附, pg_receivewal 命令行帮助:
$ pg_receivewal --help
pg_receivewal receives PostgreSQL streaming write-ahead logs. Usage:
pg_receivewal [OPTION]...
Options:
-D, --directory=DIR receive write-ahead log files into this directory
-E, --endpos=LSN exit after receiving the specified LSN
--if-not-exists do not error if slot already exists when creating a slot
-n, --no-loop do not loop on connection lost
--no-sync do not waitfor changes to be written safely to disk
-s, --status-interval=SECS
time between status packets sent to server (default: 10)
-S, --slot=SLOTNAME replication slot to use
--synchronous flush write-ahead log immediately after writing
-v, --verbose output verbose messages
-V, --version output version information, thenexit
-Z, --compress=METHOD[:DETAIL]
compress as specified
-?, --help show this help, thenexit
Connection options:
-d, --dbname=CONNSTR connection string
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port number
-U, --username=NAME connect as specified database user
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
Optional actions:
--create-slot create a new replication slot (for the slot's name see --slot)
--drop-slot drop the replication slot (for the slot's name see --slot)
Report bugs to <[email protected]>.
PostgreSQL home page: <https://www.postgresql.org/>
参考
《穷鬼玩PolarDB RAC一写多读集群系列 | 在Docker容器中用loop设备模拟共享存储》
《穷鬼玩PolarDB RAC一写多读集群系列 | 如何搭建PolarDB容灾(standby)节点》
《穷鬼玩PolarDB RAC一写多读集群系列 | 共享存储在线扩容》
《穷鬼玩PolarDB RAC一写多读集群系列 | 计算节点 Switchover》
《穷鬼玩PolarDB RAC一写多读集群系列 | 在线备份》
《穷鬼玩PolarDB RAC一写多读集群系列 | 在线归档》
https://www.postgresql.org/docs/current/app-pgreceivewal.html
文末彩蛋:国产数据库周边生态
当然一款数据库要流行起来, 除了自己要强大, 还离不开生态. 用好周边生态工具, 管理水平战胜90%老司机!!! 下面简单介绍一下国产数据库周边生态.
1、管控软件
鸣嵩(前阿里云数据库总经理 / 研究员)等大佬们创业创办的云猿生, 核心产品是KubeBlocks. 他们的理念是让管理数据库和搭积木一样简单, 如果你要管理很多套并且种类(OLTP\OLAP\NoSQL\KV\TS\MQ等)很多的数据库产品, 推荐首选.
https://github.com/apecloud/kubeblocks
PG中文社区核心委员唐成老师的公司乘数开源的Clup, 专用管理PostgreSQL和PolarDB的集群管理软件, 如果你要管理很多套数据库, 推荐选择. 并且Clup还提供了企业版、自研的连接池、分布式存储、一体机、备份平台等, 是企业用户推荐之选.
https://www.csudata.com/
若航老司机开源的pigsty, 集成了300多个PG插件的PG集群和PolarDB集群管理软件, 如果你要管理很多套PG或PolarDB数据库, 且对插件有特别多的需求, 推荐选择.
https://pigsty.cc/zh/
2、审计监控诊断优化
翟总(曾经是我背后的男人)到海信聚好看后研发的 DBdoctor, 采用ebpf技术, 在对数据库几乎没有影响的情况下实时监控数据库和服务器的各项指标, 发现和诊断问题根因非常方便.
https://www.dbdoctor.cn/
天舟老哥的核心产品Bytebase 是位于您和数据库之间的中间件。它是数据库 DevOps 的 GitLab/GitHub,专为开发人员、DBA 和平台工程师打造。
https://bytebase.cc/docs/introduction/what-is-bytebase/
PawSQL, SQL优化和诊断产品.
D-Smart, Oracle老前辈白老大出品, 专注企业级市场, 将业界顶级DBA经验的产品化作品, 产品功能包括数据库监控、诊断、优化等.
https://www.modb.pro/db/567140
3、国产数据库IDE
IDE是开发者的必备工具,例如社区有pgAdmin, 国产IDE则可以看看老程序猿达刚老师的DeskUI:
https://www.deskui.com
4、数据同步&迁移&备份恢复
NineData, 老领导出去创业做的产品, 产品涵盖了数据同步、迁移、备份、比对、devops、chatDBA等.
https://www.ninedata.cloud/home
DSG, 非常老牌的数据库同步迁移企业级产品, 支持各种数据库的异构和同构迁移, 用他们的话说, 没有dsg搞不定的迁移, 比goldengate还牛.
https://www.dsgdata.com/
公开课
如果你对PolarDB学习感兴趣可以阅读这个公开课系列:
除了PolarDB还非常值得关注的几款PG栈国产数据库:
HaloDB(基于PG兼容PostgreSQL、Oracle、MySQL. http://www.halodbtech.com/ )、 IvorySQL(基于开源PG兼容PG、Oracle. https://www.ivorysql.org/zh-cn/ )、 ProtonBase(云原生分布式数仓. https://protonbase.com/ )、 成都文武数据库(https://ww-it.cn)
参考文档点击阅读原文获得
感谢关注我的github (https://github.com/digoal/blog) 及视频号: