为了使repmgr正常运行,需要对postgresql.conf进行配置
# 监听所有网段,默认为 localhost ,只监听本地的连接
listen_addresses = '*'
# 数据库监听端口设置为 5432
port = 5432
# 最大的流复制连接数,一般为备数据库连接数量,同时 pg_basebackup 需要通过流复制连接进行备份
max_wal_senders = 32
# 最多使用的复制槽数量
max_replication_slots = 32
# 最少保留的 WAL 日志数量
wal_keep_segments = 512
# WAL 日志级别,级别越高记录的日志越多,数据库至少配置为 replica,PostgreSQL 9.5和更早版本为hot_standby或者logical
wal_level = 'replica' 或 'logical'
# 开启热备模式,备数据库接受读连接
hot_standby = on
# 备数据库查询事务反馈给主数据库,防止因为数据变化导致查询中断,repmgr需要能够连接到它管理的每台服务器。
hot_standby_feedback = on
# 开启归档
archive_mode = on
# 归档命令,将 WAL 日志拷贝到归档目录 xxxx/archive 下
archive_command = 'test ! -f xxxx/archive/%f && cp %p xxxx/archive/%f'
# 数据页变动时,将整个数据页写入 WAL 日志,执行 sys_rewind 时需要
full_page_writes = on
# 执行 sys_rewind 时需要
wal_log_hints = on
# 开启数据库日志收集
logging_collector = on
# 日志格式为 csv 格式
log_destination = 'csvlog'
# 日志文件存储路径为数据目录下的 log 目录
log_directory = 'log'
# 预加载 lib 库文件,需要将 repmgr 库写入此变量,可以写多个库文件,以 ',' 进行分隔
shared_preload_libraries = 'repmgr'
举例
1、数据库配置
listen_addresses = '*' port = 5432 max_wal_senders = 32 max_replication_slots = 32 wal_keep_segments = 512 wal_level = 'replica' hot_standby = on hot_standby_feedback = on archive_mode = on archive_command = 'test ! -f /var/lib/pgsql/12/archive/%f && cp %p /var/lib/pgsql/12/archive/%f' full_page_writes = on wal_log_hints = on logging_collector = on log_destination = 'csvlog' log_directory = 'log' shared_preload_libraries = 'repmgr'
----------------
pg_hba.conf
host replication all 0.0.0.0/0 trus
2、主机repmgr.conf
node_id=1 node_name='node1' conninfo='host=192.168.101.9 port=5432 user=postgres dbname=postgres' data_directory='/var/lib/pgsql/12/data'
3、查询主机状态
[postgres@localhost bin]$ ./repmgr cluster show ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string ----+-------+---------+-----------+----------+----------+----------+----------+------------------------------------------------------------- 1 | node1 | primary | * running | | default | 100 | 1 | host=192.168.101.9 port=5432 user=postgres dbname=postgres
4、备机repmgr.conf
node_id=2 node_name='node2' conninfo='host=192.168.101.7 port=5432 user=postgres dbname=postgres' data_directory='/home/postgres/pgsql/12/data''
5、克隆备机,调用的pg_basebackup进行备机创建
[postgres@localhost bin]$ ./repmgr standby clone -h 192.168.101.9 -Upostgres NOTICE: destination directory "/home/postgres/pgsql/12/data" provided INFO: connecting to source node DETAIL: connection string is: host=192.168.101.9 user=postgres DETAIL: current installation size is 24 MB NOTICE: checking for available walsenders on the source node (2 required) NOTICE: checking replication connections can be made to the source server (2 required) INFO: creating directory "/home/postgres/pgsql/12/data"... NOTICE: starting backup (using pg_basebackup)... HINT: this may take some time; consider using the -c/--fast-checkpoint option INFO: executing: /usr/pgsql-12/bin/pg_basebackup -l "repmgr base backup" -D /home/postgres/pgsql/12/data -h 192.168.101.9 -p 5432 -U postgres -X stream NOTICE: standby clone (using pg_basebackup) complete NOTICE: you can now start your PostgreSQL server HINT: for example: pg_ctl -D /home/postgres/pgsql/12/data start HINT: after starting the server, you need to register this standby with "repmgr standby register"
6、启动并注册备机
#启动
./pg_ctl -D /home/postgres/pgsql/12/data start
#注册 [postgres@localhost bin]$ ./repmgr standby register INFO: connecting to local node "node2" (ID: 2) INFO: connecting to primary database WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID 1) INFO: standby registration complete NOTICE: standby node "node2" (ID: 2) successfully registered
7、集群状态检查