ThingWorx 高可用性 > 使用 Pgpool-II 部署 PostgreSQL HA 的示例 > 4. 将用于流式复制的数据库配置为 node0
4. 将用于流式复制的数据库配置为 node0
最佳做法是由专用用户进行复制访问。
步骤 1
创建用户复制程序。
psql
CREATE USER replicator REPLICATION LOGIN CONNECTION LIMIT 5
PASSWORD 'trUf6yuz2?_Gub';
\q
步骤 2
修改 postgresql.conf,使其支持复制。
vim /db/postgres/postgresql.conf
配置以下参数:
node0 的 postgres.conf 参数
参数
节点
1
listen_addresses
*
* 包括所有地址。可根据您的安全要求修改此值。
2
port
5432
3
max_connections
150
4
archive_mode
on
5
wal_level
hot_standy
6
Fsync
on
7
synchronous_commit
on
8
wal_sync_method
open_sync
9
max_wal_sendors
10
10
synchronous_standby_names
'node1,node2'
11
hot_standby
on
12
hot_standby_feedback
on
13
wal_keep_segments
500
14
max_replication_slots
10
15
archive_command
'cp -f %p /db/node0archive/%f</dev/null'
16
heckpoint_segments
3
步骤 3
运行 start_replication 脚本。
start_replication.sh 脚本用于在 node0 上启动复制过程。
vim /db/bin/start_replication.sh
其中包含以下内容:
cp /db/postgres/postgresql.conf /db/bin/postgresql.conf
cp /db/postgres/pg_hba.conf /db/bin/pg_hba.conf
rm -rf /db/postgres
rm -rf /db/thingworx/*
pg_basebackup -h $1 -D /db/postgres -U replicator --xlog-method=stream
-v -P
cp /db/bin/postgresql.conf /db/postgres/postgresql.conf
cp /db/bin/pg_hba.conf /db/postgres/pg_hba.conf
步骤 4
运行 node0 的 retargetMaster.sh 脚本。
创建 script retargetMaster.sh
vim /db/bin/retargetMaster.sh
其中包含以下内容:
#!/bin/bash -x
NEW_MASTER_HOSTNAME=$1
NEW_ARCHIVE_FOLDER=$2
if grep -q "$1" /db/postgres/recovery.conf
then
echo $1 is current target,server does not need a restart, done!
else
#service postgresql stop
/usr/pgsql-10.x/bin/pg_ctl -D /db/postgres stop -m fast
#/db/bin/start_replication.sh $NEW_MASTER_HOSTNAME
# Write out new recovery.conf file
rm /db/postgres/recovery.conf
echo "standby_mode = 'on'" >> /db/postgres/recovery.conf
echo "primary_conninfo = 'port=5432 host=$NEW_MASTER_HOSTNAME user=replicator password=trUf6yuz2?_Gub sslmode=disable sslcompression=1 application_name=node0'" >> /db/postgres/recovery.conf
echo "trigger_file='/db/trigger'" >> /db/postgres/recovery.conf
echo "restore_command = 'cp -f $NEW_ARCHIVE_FOLDER/%f %p>/db/postgres/recovery.conf
echo "archive_cleanup_command = '/usr/bin/pg_archivecleanup $NEW_ARCHIVE_FOLDER %r>/db/postgres/recovery.conf
echo "recovery_target_timeline = 'latest'" >> /db/postgres/recovery.conf
/usr/pgsql-10.x/bin/pg_ctl -D /db/postgres -l /db/postgres.log start
echo target is $1 now
fi
然后,将这些脚本设置为可执行脚本:
chmod a+x /db/bin/start_replication.sh
chmod a+x /db/bin/reargetMaster.sh
步骤 5
更新 pg_hba 设置。
vim /db/postgres/pg_hba.conf
同时更新以下行:
host all all 10.10.0.0/16 trust
host replicator replicator 10.10.0.0/16 trust
根据您的安全要求,上述主机设置可能会有所不同。例如,md5 可用于身份验证,而非信任。