点击上方“IT那活儿”公众号,关注后了解更多内容,不管IT什么活儿,干就完了!!!
主机信息
IP3:192.168.21.152 witness PG-3
安装PostgreSQL
参数调整:
hot_standby=on
SSH免密 两台主机均做
PG-1:
vim /etc/hosts
192.168.21.151 PG-1
192.168.21.150 PG-2
192.168.21.152 PG-3
ssh-keygen
ssh-copy-id -i .ssh/idrsa.pub postgres@PG-2
ssh-copy-id -i .ssh/idrsa.pub postgres@PG-3
PG-2:
vim /etc/hosts
192.168.21.151 PG-1
192.168.21.150 PG-2
192.168.21.152 PG-3
ssh-keygen
ssh-copy-id -i .ssh/idrsa.pub postgres@PG-1
ssh-copy-id -i .ssh/idrsa.pub postgres@PG-3
PG-3:
vim /etc/hosts
192.168.21.151 PG-1
192.168.21.150 PG-2
192.168.21.152 PG-3
ssh-keygen
ssh-copy-id -i .ssh/idrsa.pub postgres@PG-1
ssh-copy-id -i .ssh/idrsa.pub postgres@PG-2
安装repmgr ,三台主机都安装
tar -xzvf repmgr.4.2.tar.gz
cd repmgr.4.2
./configure
make
make install
配置pg_hba.conf和创建repmgr用户和库
initdb -A scram-sha-256 -D /data/pg133 -E utf8 --wal-segsize=1024 -U postgres -W -k
create database repmgr;
create user repmgr replication login superuser password 4Replic%;
alter user repmgr set search_path to repmgr,"$user",public;
shared_preload_libraries=repmgr;
vi /home/postgres/.pgpass
ip:port:repmgr:repmgr:repmgr
ip:port:replication:repmgr:repmgr #注意该行必须有,否则在执行switchover的时候,standby可以正常提升为primary,但旧primary 无法自动跟随新主,每台主机上都要进行配置。
repmgr集群搭建
vim /etc/remgr/repmgr.conf
node_id=1
node_name=PG-1.PG
conninfo=host=PG-1.PG port=5432 user=repmgr dbname=repmgr
data_directory=/data/pg134
replication_user=repmgr
replication_type=physical
use_replication_slots=true
location=my-repmgr
#witness settings
witness_sync_interval=10
#logging settings
log_level=INFO
log_facility=STDERR
log_file=/opt/repmgr/repmgr.log
log_status_interval=2
#Environment/Command Settings
pg_bindir=/opt/pg134/bin
repmgr_bindir=/opt/pg134/bin
passfile=/home/postgres/.pgpass
use_primary_conninfo_password=false
#Standby promote_Settings
promote_check_timeout=60
promote_check_interval=1
#Standby Follow Settings
primary_follow_timeout=60
standby_follow_timeout=15
#Standby Switchover Settings
shutdown_check_timeout=60
standby_reconnect_timeout=60
wal_receive_check_timeout=30
#Node Rejoin Settings
node_rejoin_timeout=60
#Failover And Monitoring Settings
failover=automatic
priority=100
connection_check_type=ping
reconnect_attempts=5
reconnect_interval=2
promote_command=/opt/pg134/bin/repmgr standby promote -f /opt/repmgr/repmgr.conf --log-to-file
follow_command=/opt/pg134/bin/repmgr standby follow -f /opt/repmgr/repmgr.conf --upstream-node-id=%n --log-to-file
primary_notification_timeout=60
repmgrd_standby_startup_timeout=30
monitoring_history=yes
monitor_interval_secs=2
degraded_monitoring_timeout=-1
async_query_timeout=60
repmgrd_pid_file=/opt/repmgr/repmgrd.pid
standby_disconnect_on_failover=true
sibling_nodes_disconnect_timeout=30
child_node_connected_min_count=1
child_nodes_check_interval=2
child_nodes_disconnect_timeout=10
child_nodes_connected_include_wintess=true
#child_nodes_disconnect_command = /opt/pg134/bin/pg_ctl stop -D /data/pg134
primary_visibility_consensus=true
#Service Control Commands
service_start_command=/opt/pg134/bin/pg_ctl start -w -D /data/pg134
service_stop_command=/opt/pg134/bin/pg_ctl stop -w -D /data/pg134
service_restart_command=/opt/pg134/bin/pg_ctl restart -w -D /data/pg134
service_reload_command=/opt/pg134/bin/pg_ctl reload -w -D /data/pg134
repmgrd_service_start_command=> /opt/repmgr/repmgrd.pid && /opt/pg134/bin/repmgrd -f /opt/repmgr/repmgr.conf -d -p /opt/repmgr/repmgrd.pid
repmgrd_service_stop_command=kill -9 `cat /opt/repmgr/repmgrd.pid`
repmgr -f /etc/repmgr/repmgr.conf cluster show
repmgrd -f /etc/repmgr/repmgr.conf --verbose --monitoring-history > /var/log/repmgr/repmgr.log 2>&1 &
或者
repmgr -f /etc/repmgr/repmgr.conf daemon start
vim /etc/repmgr/repmgr.conf
node_id=2
node_name=PG-2.PG
conninfo=host=PG-2.PG port=5432 user=repmgr dbname=repmgr
data_directory=/data/pg134
replication_user=repmgr
replication_type=physical
use_replication_slots=true
location=my-repmgr
#witness settings
witness_sync_interval=10
#logging settings
log_level=INFO
log_facility=STDERR
log_file=/opt/repmgr/repmgr.log
log_status_interval=2
#Environment/Command Settings
pg_bindir=/opt/pg134/bin
repmgr_bindir=/opt/pg134/bin
passfile=/home/postgres/.pgpass
use_primary_conninfo_password=false
#Standby promote_Settings
promote_check_timeout=60
promote_check_interval=1
#Standby Follow Settings
primary_follow_timeout=60
standby_follow_timeout=15
#Standby Switchover Settings
shutdown_check_timeout=60
standby_reconnect_timeout=60
wal_receive_check_timeout=30
#Node Rejoin Settings
node_rejoin_timeout=60
#Failover And Monitoring Settings
failover=automatic
priority=100
connection_check_type=ping
reconnect_attempts=5
reconnect_interval=2
promote_command=/opt/pg134/bin/repmgr standby promote -f /opt/repmgr/repmgr.conf --log-to-file
follow_command=/opt/pg134/bin/repmgr standby follow -f /opt/repmgr/repmgr.conf --upstream-node-id=%n --log-to-file
primary_notification_timeout=60
repmgrd_standby_startup_timeout=30
monitoring_history=yes
monitor_interval_secs=2
degraded_monitoring_timeout=-1
async_query_timeout=60
repmgrd_pid_file=/opt/repmgr/repmgrd.pid
standby_disconnect_on_failover=true
sibling_nodes_disconnect_timeout=30
child_node_connected_min_count=1
child_nodes_check_interval=2
child_nodes_disconnect_timeout=10
child_nodes_connected_include_wintess=true
child_nodes_disconnect_command = /opt/pg134/bin/pg_ctl stop -D /data/pg134
primary_visibility_consensus=true
#Service Control Commands
service_start_command=/opt/pg134/bin/pg_ctl start -w -D /data/pg134
service_stop_command=/opt/pg134/bin/pg_ctl stop -w -D /data/pg134
service_restart_command=/opt/pg134/bin/pg_ctl restart -w -D /data/pg134
service_reload_command=/opt/pg134/bin/pg_ctl reload -w -D /data/pg134
repmgrd_service_start_command=> /opt/repmgr/repmgrd.pid && /opt/pg134/bin/repmgrd -f /opt/repmgr/repmgr.conf -d -p /opt/repmgr/repmgrd.pid
repmgrd_service_stop_command=kill -9 `cat /opt/repmgr/repmgrd.pid`
repmgr -f /etc/repmgr/repmgr.conf -U repmgr -d repmgr -h 192.168.28.166 standby clone --dry-run -c
--dry-run选项用于检查。
repmgr -f /etc/repmgr/repmgr.conf -U repmgr -d repmgr -h 192.168.28.166 standby clone -c
克隆从主PostgreSQL的数据目录中的文件节点,使用postgresql的pg_basebackup,会自动创建一个recovery.conf文件。
pg_ctl -D /data/pgdata start
repmgr -f /etc/repmgr/repmgr.conf standby register
repmgr -f /etc/repmgr/repmgr.conf daemon start
或
repmgrd -f /etc/repmge/repmgr.conf -d -p /data/repmgr/repmgrd.pid
initdb -A scram-sha-256 -D /data/pg133 -E utf8 --wal-segsize=1024 -U postgres -W -k
create database repmgr;
create user repmgr with login superuser replication password 4Replic%;
alter user repmgr set search_path to repmgr,"$user",public;
数据库配置参数:
shared_preload_libraries=repmgr;
数据库重启。
vi /etc/repmgr/repmgr.conf
node_id=3
node_name=PG-3.PG
conninfo=host=PG-3.PG port=5432 user=repmgr dbname=repmgr
data_directory=/data/pg134
replication_user=repmgr
replication_type=physical
use_replication_slots=true
location=my-repmgr
#witness settings
witness_sync_interval=10
#logging settings
log_level=INFO
log_facility=STDERR
log_file=/opt/repmgr/repmgr.log
log_status_interval=2
#Environment/Command Settings
pg_bindir=/opt/pg134/bin
repmgr_bindir=/opt/pg134/bin
passfile=/home/postgres/.pgpass
use_primary_conninfo_password=false
#Standby promote_Settings
promote_check_timeout=60
promote_check_interval=1
#Standby Follow Settings
primary_follow_timeout=60
standby_follow_timeout=15
#Standby Switchover Settings
shutdown_check_timeout=60
standby_reconnect_timeout=60
wal_receive_check_timeout=30
#Node Rejoin Settings
node_rejoin_timeout=60
#Failover And Monitoring Settings
failover=automatic
priority=100
connection_check_type=ping
reconnect_attempts=5
reconnect_interval=2
promote_command=/opt/pg134/bin/repmgr standby promote -f /opt/repmgr/repmgr.conf --log-to-file
follow_command=/opt/pg134/bin/repmgr standby follow -f /opt/repmgr/repmgr.conf --upstream-node-id=%n --log-to-file
primary_notification_timeout=60
repmgrd_standby_startup_timeout=30
monitoring_history=yes
monitor_interval_secs=2
degraded_monitoring_timeout=-1
async_query_timeout=60
repmgrd_pid_file=/opt/repmgr/repmgrd.pid
standby_disconnect_on_failover=true
sibling_nodes_disconnect_timeout=30
child_nodes_check_interval=2
child_node_connected_min_count=1
child_nodes_disconnect_timeout=10
child_nodes_connected_include_wintess=true
child_nodes_disconnect_command = /opt/pg134/bin/pg_ctl stop -D /data/pg134
primary_visibility_consensus=true
#Service Control Commands
service_start_command=/opt/pg134/bin/pg_ctl start -w -D /data/pg134
service_stop_command=/opt/pg134/bin/pg_ctl stop -w -D /data/pg134
service_restart_command=/opt/pg134/bin/pg_ctl restart -w -D /data/pg134
service_reload_command=/opt/pg134/bin/pg_ctl reload -w -D /data/pg134
repmgrd_service_start_command=> /opt/repmgr/repmgrd.pid && /opt/pg134/bin/repmgrd -f /opt/repmgr/repmgr.conf -d -p /opt/repmgr/repmgrd.pid
repmgrd_service_stop_command=kill -9 `cat /opt/repmgr/repmgrd.pid`
repmgr -f /etc/repmgr/repmgr.conf witness register -h PG-1 -U repmgr -d repmgr
注意:-h连接的是primary节点,当发生故障切换时,见证节点会自动连接到最新的primary节点。
repmgr -f /etc/repmgr/repmgr.conf daemon start
或
repmgrd -f /etc/repmge/repmgr.conf -d -p /data/repmgr/repmgrd.pid
基于同步的模式
7.1 在现有环境开启同步复制
#!/bin/bash
DBPATH=/opt/pg134/bin
DBPORT=5432
DBHOST=127.0.0.1
/opt/pg134/bin/pg_ctl stop -D /data/pg134
$DBPATH/psql -U postgres -p $PGPORT -h $DBHOST <checkpoint;
alter system set synchronous_standby_names to ;
select pg_reload_conf();
q
EOF
使用复制槽
在现有环境中使用复制槽:
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/129225.html
摘要:相比自建,其可靠性更高,方便运维维护。宋体经过审慎考虑,用户同时选用三种数据库,针对性的满足不同目标。宋体宋体其中,相比于在上的快速高效是其优势,也是用户选型的重要砝码。PostgreSQL UDB用在大数据分析上,查询效率更高。相比自建,其可靠性更高,方便运维维护。 — 31会议运维经理 汤雷 如何用好PostgreSQL? PostgreSQL是业内一款十分流行的开源数...
摘要:经过对比选型,用户同时选用三种数据库来针对性的满足不同目标。基于能够带来的这些特性优势,用户选择了。相比自建,其可靠性更高,方便运维维护。整个过程中用户不需要任何人工干预和配置修改,真正做到自动容灾。 据DB-Engines 最新发布的2019年8月份数据库流行度排行榜(如下图)显示,名列前茅的MySQL和PostgreSQL数据库的流行趋势与去年同期相比依然稳增不减。 showImg...
阅读 1247·2023-01-11 13:20
阅读 1555·2023-01-11 13:20
阅读 1008·2023-01-11 13:20
阅读 1675·2023-01-11 13:20
阅读 3968·2023-01-11 13:20
阅读 2510·2023-01-11 13:20
阅读 1305·2023-01-11 13:20
阅读 3474·2023-01-11 13:20