2台机器IP 192.168.18.9 192.168.18.11
从作为备份数据用,主故障时不能自动切换,只能手动切换
1. 2台机器均部署
一些操作系统的配置参照PG单机部署及卸载
useradd postgres
解压后的软件目录 /data/postgresql-17.1
mkdir /data/pg17
chown -R postgres.postgres /data/pg17
安装依赖
yum -y install bzip2 gcc gcc-c++ epel-release llvm5.0 llvm5.0-devel clang libicu-devel perl-ExtUtils-Embed zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-devel flex readline-devel
cd /data/postgresql-17.1
chmod +x configure
./configure --prefix=/data/pg17 --with-pgport=5432 --with-readline
##readline库允许psql记住用户输入的每个命令,用户可以使用箭头键回忆和编辑之前的命令。
make && make install
cd /data/pg17
mkdir pgdata
chown -R postgres.postgres /data/pg17/pgdata
su - postgres
vim .bash_profile
新添加配置
export PGHOME=/data/pg17
export PGDATA=/data/pg17/pgdata
export PGPORT=5432
export PGPASSWORD=123456
export PATH=
P
G
H
O
M
E
/
b
i
n
:
PGHOME/bin:
PGHOME/bin:PATH
export MANPATH=
P
G
H
O
M
E
/
s
h
a
r
e
/
m
a
n
:
PGHOME/share/man:
PGHOME/share/man:MANPATH
export LD_LIBRARY_PATH=
P
G
H
O
M
E
/
l
i
b
:
PGHOME/lib:
PGHOME/lib:LD_LIBRARY_PATH
export SCRIPTS_DIR=$SCRIPTS_DIR
export LANG=en_US.UTF-8
export DATE=date +“%Y%m%d%H%M”
source .bash_profile
initdb -D $PGDATA -U postgres --encoding=UTF8 --lc-collate=C --lc-ctype=en_US.utf8 --data-checksums
暂不启动
配置自启动 但暂不启动,参照PG自启动
2、修改主18.9上的pg_hba.conf
对任意IP使用password登录,本机trust
从机18.11 可password 或 trust
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 password
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all 192.168.18.11/32 password
3、修改主18.9的postgresql.conf
listen_addresses = '*'
max_connections = 60000
archive_mode = on
archive_command = 'cp %p /data/archivedir/%f'
max_wal_senders = 10 ##来自后备服务器的并发连接的最大数目
max_replication_slots = 10
wal_level = replica
hot_standby = on
wal_log_hints = on
wal_compression = on
max_wal_size=50GB
在使用一个archive_command脚本时,最好启用logging_collector(默认on)。任何从该脚本被写到stderr的消息将出现在数据库服务器日志中,这允许在复杂配置失败后能更容易被诊断。
4、启主库
主18.9执行systemctl start posrgresv17
5、创建同步用户
使用psql进入数据库,创建同步用户CREATE ROLE repl WITH REPLICATION LOGIN;
配置密码 \password repl
或直接 CREATE ROLE repl WITH REPLICATION LOGIN password ‘repl’;
6、在主18.9配置 PgArchiveClean.sh
cd /data/pg17/
vim PgArchiveClean.sh
#!/bin/bash
fileName=$(ls -lt /data/pg17/archivedir/ | head -n 2 | tail -1f | awk ‘{print $20,$9}’)
echo “-----date +%Y%m%d
-----” $fileName
/data/pg17/bin/pg_archivecleanup /data/pg17/archivedir/ $fileName
7、归档清理定时任务配置
在主18.9配置postgres的crontab -e ,每天12时30分删除前一日archive log
30 12 * * * sh /data/pg17/PgArchiveClean.sh >> /data/pg17/PgArchiveClean.log
8、备份数据到从库
在从18.11执行rm /data/pg_data/* -rf并同步18.9数据
pg_basebackup -h 10.51.107.26 -U postgres -D /data/pg_data -X stream -P -v >/tmp/pg_basebackup.log
9、配置从库
在18.11配置如下
在/data/pg17/pgdata下新建standby.signal
standby_mode=on
primary_conninfo=‘host=192.168.18.9 port=5432 user=repl password=repl’
recovery_target_timeline=‘latest’
修改postgresql.conf
primary_conninfo=‘host=192.168.18.9 port=5432 user=repl password=repl’
recovery_target_timeline=‘latest’
10、启备库
在备库18.11上root登录,启动备库
systemctl start postgresqlv17
11、主库查看同步信息
18.9上进入数据库查看
select * from pg_stat_replication;
应有一行记录
12、配置操作系统参数
主从机器在 /etc/sysctl.conf配置
kernel.pid_max=65535 ##间接控制系统中进程的数量,防止恶意攻击
net.ipv4.tcp_fin_timeout=5 ##秒
并使用/sbin/sysctl -p生效