Redhat LINUX 9.3 + PG 16.1 搭建主备流复制

2023-12-18 09:28

本文主要是介绍Redhat LINUX 9.3 + PG 16.1 搭建主备流复制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一直想搭建一个PG流复制,最近正好有一个新环境,操作系统是最新的,rhel 9.3,数据库是最新的 pg 16.1,借鉴了网上的步骤,尤其是小工到专家的内容,在此谢过。

1.安装环境

1)IP:
主:192.168.133.151 server1
备:192.168.133.152 server2


2)操作系统版本:
[postgres@server2 data]$ cat /etc/redhat-release 
Red Hat Enterprise Linux release 9.3 (Plow)

3)数据库版本:

postgres@[local]:5432/postgres-11653#=select version();
                                                    version                                                     
----------------------------------------------------------------------------------------------------------------
 PostgreSQL 16.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.4.1 20230605 (Red Hat 11.4.1-2.1.0.1), 64-bit
(1 row)

2.创建主备流复制

2.1 修改 /etc/hosts 文件(主备)

vim /etc/hosts

192.168.133.151 server1
192.168.133.152 server2

2.2 主库设置


1)建立同步用户

postgres=# create role rep1 login replication encrypted password 'rep1';

2) 配置复制认证配置

vim $PGDATA/pg_hba.conf

host    replication     rep1            server2                 md5


3) 修改$PGDATA/postgres.conf

listen_addresses = '*'
port=5432
wal_level = replica
max_wal_senders=10
archive_mode = on
archive_timeout=1800

archive_command = 'cp %p /home/postgres/arch/%f'
restore_command = 'cp /home/postgres/arch/%f %p'
recovery_target_timeline = 'latest'
full_page_writes = on
wal_log_hints = on

logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d.log'

--下面参数设置大一些,否则,后面主备切换,可能出现找不到日志文件的问题
min_wal_size = 800MB
wal_keep_size = 1024

4).重启主库,让参数生效

pg_ctl restart

2.3 备库设置

1)初始化,直接从主库备份就行

pg_basebackup -h server1 -p 5432 -U repl -X stream -R -Fp -P -D /pgdata/data

-h:指定要连接的服务器的主机名或IP地址。
-U:指定连接使用的用户名。
-Fp:指定备份的格式。在这种情况下,备份以纯文本格式存储。
-P:指定在备份过程中显示进度信息。
-X stream:指定备份的流式传输方法。
-R:指定包括所有必需的WAL文件以进行一致性备份。
-D:指定备份存储的目录。

2) 修改参数,加上下面参数

hot_standby = on #在备份的同时允许查询,默认值
max_standby_streaming_delay = 30s #可选,流复制最大延迟
wal_receiver_status_interval = 10s #可选,从向主报告状态的最大间隔时间
hot_standby_feedback = on #可选,查询冲突时向主反馈


3)配置$PGDATA/data/pg_hba.conf
host    replication     rep1            server1                 md5 #这里修改主机名为主机的

4) 重启让配置生效
pg_ctl start


3.验证

3.1 查看主备进程

1)主库端

[postgres@server1 data]$ ps -ef|grep wal
postgres    3003    2998  0 21:57 ?        00:00:00 postgres: walwriter 
postgres    3063    2998  0 22:08 ?        00:00:00 postgres: walsender rep1 192.168.133.152(49190) streaming 0/260021C8
postgres    3080    1518  0 22:13 pts/0    00:00:00 grep --color=auto wal
[postgres@server1 data]$ 

2)备库端

[postgres@server2 data]$ ps -ef|grep wal
postgres   11355   11345  0 22:08 ?        00:00:00 postgres: walreceiver streaming 0/260021C8
postgres   11373    9351  0 22:14 pts/0    00:00:00 grep --color=auto wal
[postgres@server2 data]$ 

3.2 主库切换日志

postgres@[local]:5432/postgres-3100#=select pg_switch_wal();
 pg_switch_wal 
---------------
 0/260021E0
(1 row)

postgres@[local]:5432/postgres-3100#=\! ls -ltr /pgdata/data/pg_wal
总用量 458768
-rw------- 1 postgres postgres       41 12月 15 23:16 00000002.history
-rw------- 1 postgres postgres 16777216 12月 17 20:09 00000002000000000000000D
-rw------- 1 postgres postgres 16777216 12月 17 20:22 00000002000000000000000E
-rw------- 1 postgres postgres 16777216 12月 17 20:22 00000002000000000000000F
-rw------- 1 postgres postgres 16777216 12月 17 20:25 000000020000000000000010
-rw------- 1 postgres postgres 16777216 12月 17 20:28 000000020000000000000011
-rw------- 1 postgres postgres 16777216 12月 17 20:28 000000020000000000000012.partial
-rw------- 1 postgres postgres       84 12月 17 20:29 00000003.history
-rw------- 1 postgres postgres 16777216 12月 17 20:34 000000030000000000000012
-rw------- 1 postgres postgres 16777216 12月 17 21:04 000000030000000000000013
-rw------- 1 postgres postgres 16777216 12月 17 21:26 000000030000000000000014
-rw------- 1 postgres postgres 16777216 12月 17 21:29 000000030000000000000015
-rw------- 1 postgres postgres 16777216 12月 17 21:29 000000030000000000000016
-rw------- 1 postgres postgres 16777216 12月 17 21:30 000000030000000000000017
-rw------- 1 postgres postgres 16777216 12月 17 21:30 000000030000000000000018
-rw------- 1 postgres postgres 16777216 12月 17 21:31 000000030000000000000019
-rw------- 1 postgres postgres 16777216 12月 17 21:31 00000003000000000000001A
-rw------- 1 postgres postgres 16777216 12月 17 21:33 00000003000000000000001B
-rw------- 1 postgres postgres 16777216 12月 17 21:33 00000003000000000000001C
-rw------- 1 postgres postgres 16777216 12月 17 21:47 00000003000000000000001D
-rw------- 1 postgres postgres 16777216 12月 17 21:47 00000003000000000000001E
-rw------- 1 postgres postgres 16777216 12月 17 21:50 00000003000000000000001F
-rw------- 1 postgres postgres 16777216 12月 17 21:50 000000030000000000000020
-rw------- 1 postgres postgres 16777216 12月 17 21:53 000000030000000000000021
-rw------- 1 postgres postgres 16777216 12月 17 21:53 000000030000000000000022
-rw------- 1 postgres postgres 16777216 12月 17 21:57 000000030000000000000023
-rw------- 1 postgres postgres 16777216 12月 17 21:58 000000030000000000000024
-rw------- 1 postgres postgres 16777216 12月 17 21:58 000000030000000000000025
-rw------- 1 postgres postgres      341 12月 17 21:58 000000030000000000000025.00000028.backup
-rw------- 1 postgres postgres 16777216 12月 17 22:17 000000030000000000000026
drwx------ 2 postgres postgres     4096 12月 17 22:17 archive_status
-rw------- 1 postgres postgres 16777216 12月 17 22:17 000000030000000000000027
postgres@[local]:5432/postgres-3100#=

--备库

[postgres@server2 data]$ ls -ltr /pgdata/data/pg_wal
总用量 49156
-rw------- 1 postgres postgres       84 12月 17 21:58 00000003.history
-rw------- 1 postgres postgres 16777216 12月 17 21:58 000000030000000000000025
drwx------ 2 postgres postgres       72 12月 17 22:17 archive_status
-rw------- 1 postgres postgres 16777216 12月 17 22:17 000000030000000000000026
-rw------- 1 postgres postgres 16777216 12月 17 22:17 000000030000000000000027

3.3 查看当前备库状态

[postgres@server2 data]$ psql
psql (16.1)
Type "help" for help.

postgres@server2:5432/postgres-11485#=select pg_is_in_recovery();
 pg_is_in_recovery 
-------------------
 t
(1 row)


postgres@server1:5432/postgres-3210#=select pg_is_in_recovery();
 pg_is_in_recovery 
-------------------
 f
(1 row)


t :true,意味着处于 recovery 状态
f :false,意味着处于正常服务状态


3.4 主库查询

postgres@server1:5432/postgres-3210#=select pid, usename,client_addr,state,sync_state,reply_time from pg_stat_replication;
 pid  | usename |   client_addr   |   state   | sync_state |          reply_time           
------+---------+-----------------+-----------+------------+-------------------------------
 3188 | rep1    | 192.168.133.152 | streaming | async      | 2023-12-17 22:31:28.775596+08
(1 row)

postgres@server1:5432/postgres-3210#=select pid, usename,client_addr,state,sync_state,reply_time from pg_stat_replication;
-[ RECORD 1 ]------------------------------
pid         | 3188
usename     | rep1
client_addr | 192.168.133.152
state       | streaming
sync_state  | async
reply_time  | 2023-12-17 22:31:38.761116+08

sync_state表示同步模式
sent_lsn表示发送日志的起点
reply_time表示应用日志的起点

postgres@server1:5432/postgres-3210#=select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 3188
usesysid         | 16409
usename          | rep1
application_name | walreceiver
client_addr      | 192.168.133.152
client_hostname  | server2
client_port      | 59278
backend_start    | 2023-12-17 22:25:07.174465+08
backend_xmin     | 
state            | streaming
sent_lsn         | 0/28000C88
write_lsn        | 0/28000C88
flush_lsn        | 0/28000C88
replay_lsn       | 0/28000C88
write_lag        | 
flush_lag        | 
replay_lag       | 
sync_priority    | 0
sync_state       | async
reply_time       | 2023-12-17 22:33:18.809739+08


3.5 备库查询

postgres@server2:5432/postgres-11485#=select * from pg_stat_wal_receiver;-[ RECORD 1 ]---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pid                   | 11386
status                | streaming
receive_start_lsn     | 0/28000000
receive_start_tli     | 3
written_lsn           | 0/28000C88
flushed_lsn           | 0/28000C88
received_tli          | 3
last_msg_send_time    | 2023-12-17 22:35:08.850593+08
last_msg_receipt_time | 2023-12-17 22:35:08.85238+08
latest_end_lsn        | 0/28000C88
latest_end_time       | 2023-12-17 22:30:08.735565+08
slot_name             | 
sender_host           | server1
sender_port           | 5432
conninfo              | user=rep1 password=******** channel_binding=disable dbname=replication host=server1 port=5432 client_encoding=GBK fallback_application_name=walreceiver sslmode=disable sslcompression=0 sslcertmode=disable sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres gssdelegation=0 target_session_attrs=any load_balance_hosts=disable

--备库日志

023-12-17 22:25:07.215 CST [11386] LOG:  started streaming WAL from primary at 0/28000000 on timeline 3
2023-12-17 22:25:08.324 CST [11347] LOG:  restartpoint starting: time
2023-12-17 22:25:08.331 CST [11347] LOG:  restartpoint complete: wrote 0 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.001 s, total=0.007 s; sync files=0, longest=0.000 s, average=0.000 s; distance=16383 kB, estimate=16389 kB; lsn=0/28000028, redo lsn=0/28000028
2023-12-17 22:25:08.331 CST [11347] LOG:  recovery restart point at 0/28000028
2023-12-17 22:30:08.225 CST [11347] LOG:  restartpoint starting: time
2023-12-17 22:30:08.332 CST [11347] LOG:  restartpoint complete: wrote 2 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.103 s, sync=0.001 s, total=0.107 s; sync files=2, longest=0.001 s, average=0.001 s; distance=2 kB, estimate=14751 kB; lsn=0/28000BD8, redo lsn=0/28000BA0
2023-12-17 22:30:08.332 CST [11347] LOG:  recovery restart point at 0/28000BA0
2023-12-17 22:30:08.332 CST [11347] DETAIL:  Last completed transaction was at log time 2023-12-17 22:27:29.986132+08.


3.6 测试数据同步

1)主库创建表:

postgres@server1:5432/postgres-3210#=create table emp(emp int);
CREATE TABLE
postgres@server1:5432/postgres-3210#=insert into emp values(100);
INSERT 0 1
postgres@server1:5432/postgres-3210#=select * from emp;
 emp 
-----
 100
(1 row)

2) 备库验证

[postgres@server2 pg_log]$ psql -h server2
Password for user postgres: 
psql (16.1)
Type "help" for help.

postgres@server2:5432/postgres-11497#=\d
             List of relations
 Schema |      Name      | Type  |  Owner   
--------+----------------+-------+----------
 public | emp            | table | postgres
 public | pg_buffercache | view  | postgres
(2 rows)

postgres@server2:5432/postgres-11497#=select * from emp;
 emp 
-----
 100
(1 row)

通过以上可以看出,主库新增数据已经同步到备库

4.主备切换

这里模拟主库出现问题

4.1 停止主库

[postgres@server1 data]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@server1 data]$ 
[postgres@server1 data]$ 
[postgres@server1 data]$ pg_ctl status
pg_ctl: no server running

4.2 提升备库为主

pg_ctl promote

postgres@server2 pg_log]$ pg_ctl promote
waiting for server to promote.... done
server promoted
[postgres@server2 pg_log]$ pg_ctl status
pg_ctl: server is running (PID: 11345)
/u01/app/postgres/bin/postgres
[postgres@server2 pg_log]$ pg_controldata |grep stat
Database cluster state:               in production
[postgres@server2 pg_log]$ 

[postgres@server2 data]$ ls -ltr standby*
ls: 无法访问 'stand*': 没有那个文件或目录
[postgres@server2 data]$ 

[postgres@server2 data]$ ps -ef|grep wal
postgres   11569   11345  0 22:44 ?        00:00:00 postgres: walwriter 
postgres   11592    9351  0 22:46 pts/0    00:00:00 grep --color=auto wal
[postgres@server2 data]$ 

4.3 创建standby.signal文件

在新备库上(原主库) 完成

只需要创建文件即可,不需要添加内容

[postgres@server1 data]$ touch standby.signal
[postgres@server1 data]$ 
[postgres@server1 data]$ ls -ltr standby.signal
-rw-r--r-- 1 postgres postgres 0 12月 17 22:49 standby.signal


4.4 调整新备库 postgresql.auto.conf内容

在新备库的 postgresql.auto.conf 文件中添加如下内容

primary_conninfo = 'user=rep1 password=rep1 passfile=''/home/postgres/.pgpass'' channel_binding=disable host=server2 port=5432 client_encoding=GBK sslmode=disable sslcompression=0 sslcertmode=disable sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres gssdelegation=0 target_session_attrs=any load_balance_hosts=disable'

4.5 启动新备库


[postgres@server1 data]$ pg_ctl restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2023-12-17 22:53:08.352 CST [3340] LOG:  redirecting log output to logging collector process
2023-12-17 22:53:08.352 CST [3340] HINT:  Future log output will appear in directory "pg_log".
 done
server started

4.6 验证


新备库
[postgres@server1 data]$ pg_controldata |grep state
Database cluster state:               in archive recovery

新主库
[postgres@server2 data]$ pg_controldata |grep stat
Database cluster state:               in production


[postgres@server2 data]$ psql
psql (16.1)
Type "help" for help.

postgres@[local]:5432/postgres-11653#=\x
Expanded display is on.
postgres@[local]:5432/postgres-11653#=select pid, usename,client_addr,state,sync_state,reply_time from pg_stat_replication;
-[ RECORD 1 ]------------------------------
pid         | 11644
usename     | rep1
client_addr | 192.168.133.151
state       | streaming
sync_state  | async
reply_time  | 2023-12-17 22:55:48.476883+08

postgres@[local]:5432/postgres-11653#=select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 11644
usesysid         | 16409
usename          | rep1
application_name | walreceiver
client_addr      | 192.168.133.151
client_hostname  | server1
client_port      | 47196
backend_start    | 2023-12-17 22:53:08.395347+08
backend_xmin     | 
state            | streaming
sent_lsn         | 0/290001B8
write_lsn        | 0/290001B8
flush_lsn        | 0/290001B8
replay_lsn       | 0/290001B8
write_lag        | 
flush_lag        | 
replay_lag       | 
sync_priority    | 0
sync_state       | async
reply_time       | 2023-12-17 22:56:18.489045+08


到此,切换完成。

这篇关于Redhat LINUX 9.3 + PG 16.1 搭建主备流复制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/507899

相关文章

linux-基础知识3

打包和压缩 zip 安装zip软件包 yum -y install zip unzip 压缩打包命令: zip -q -r -d -u 压缩包文件名 目录和文件名列表 -q:不显示命令执行过程-r:递归处理,打包各级子目录和文件-u:把文件增加/替换到压缩包中-d:从压缩包中删除指定的文件 解压:unzip 压缩包名 打包文件 把压缩包从服务器下载到本地 把压缩包上传到服务器(zip

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

搭建Kafka+zookeeper集群调度

前言 硬件环境 172.18.0.5        kafkazk1        Kafka+zookeeper                Kafka Broker集群 172.18.0.6        kafkazk2        Kafka+zookeeper                Kafka Broker集群 172.18.0.7        kafkazk3

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

【Linux 从基础到进阶】Ansible自动化运维工具使用

Ansible自动化运维工具使用 Ansible 是一款开源的自动化运维工具,采用无代理架构(agentless),基于 SSH 连接进行管理,具有简单易用、灵活强大、可扩展性高等特点。它广泛用于服务器管理、应用部署、配置管理等任务。本文将介绍 Ansible 的安装、基本使用方法及一些实际运维场景中的应用,旨在帮助运维人员快速上手并熟练运用 Ansible。 1. Ansible的核心概念

Linux服务器Java启动脚本

Linux服务器Java启动脚本 1、初版2、优化版本3、常用脚本仓库 本文章介绍了如何在Linux服务器上执行Java并启动jar包, 通常我们会使用nohup直接启动,但是还是需要手动停止然后再次启动, 那如何更优雅的在服务器上启动jar包呢,让我们一起探讨一下吧。 1、初版 第一个版本是常用的做法,直接使用nohup后台启动jar包, 并将日志输出到当前文件夹n

[Linux]:进程(下)

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:Linux学习 贝蒂的主页:Betty’s blog 1. 进程终止 1.1 进程退出的场景 进程退出只有以下三种情况: 代码运行完毕,结果正确。代码运行完毕,结果不正确。代码异常终止(进程崩溃)。 1.2 进程退出码 在编程中,我们通常认为main函数是代码的入口,但实际上它只是用户级