--在主从复制环境中,如果从库不小心打开了读写模式(相当单节点的一个数据),比如
touch /usr/local/postgresql/9.3.4/5434/pgsql.recovery.trigger
--此时从节点已经于主机点脱离,此时再把这个节点改为从节点时,由于从的timeline高于主,故该节点不能再变成从节点了
[postgres@rudy_01 5434]$ ls | grep recovery
recovery.done
[postgres@rudy_01 5434]$ pg_ctl stop -m fast -D /usr/local/postgresql/9.3.4/5434
[postgres@rudy_01 5434]$ mv recovery.done recovery.conf
[postgres@rudy_01 5434]$ pg_ctl start -m fast -D /usr/local/postgresql/9.3.4/5434 -l serverlog
--从log日志中可以看到如下错误
FATAL,XX000,"highest timeline 14 of the primary is behind recovery timeline 15"
--如果要想把该节点变成从节点需要同同步主机点的数据到从节点
--本次操作以rsync方式进行,注意,如果如果数据库很大的话, rsync的数据比对过程非常漫长, 并且要消耗大量的io资源
--在之前的从节点停止数据库实例
pg_ctl stop -m fast -D /usr/local/postgresql/9.3.4/5434
--在主机点执行 rsync_standby.sh脚本
#/bin/sh -x
PRIMARY_PORT=5433
STANDBY_PORT=5434
SOURCE_CLUSTER=/usr/local/postgresql/9.3.4/5433
DEST_CLUSTER=/usr/local/postgresql/9.3.4/5434
PGCTL=/usr/local/postgresql/9.3.4/bin/pg_ctl
recovery_node_host_name=rudy_01
primary_host_name=rudy
psql -p $PRIMARY_PORT -c "SELECT pg_start_backup('file_based_log_shipping', true)" postgres
/usr/bin/rsync -C -a -c --delete --exclude postmaster.pid \
--exclude postgresql.trigger.* --exclude postmaster.opts --exclude pg_log \
--exclude recovery.conf --exclude recovery.done \
--exclude pg_xlog \
$SOURCE_CLUSTER/ $recovery_node_host_name:$DEST_CLUSTER/
ssh -T $recovery_node_host_name /bin/rm -rf $DEST_CLUSTER/pg_xlog
ssh -T $recovery_node_host_name /bin/mkdir $DEST_CLUSTER/pg_xlog
ssh -T $recovery_node_host_name /bin/chmod 700 $DEST_CLUSTER/pg_xlog
ssh -T $recovery_node_host_name /bin/rm -rf $DEST_CLUSTER/recovery.done
ssh -T $recovery_node_host_name "/bin/cat > $DEST_CLUSTER/recovery.conf <<EOF
standby_mode = on
primary_conninfo = 'port=$PRIMARY_PORT user=repuser host=$primary_host_name'
trigger_file = '$DEST_CLUSTER/pgsql.recovery.trigger'
recovery_target_timeline = 'latest'
EOF"
ssh -T $recovery_node_host_name "sed -i 's/$PRIMARY_PORT/$STANDBY_PORT/g' $DEST_CLUSTER/postgresql.conf"
psql -p $PRIMARY_PORT -c "SELECT pg_stop_backup()" postgres
ssh -T $recovery_node_host_name $PGCTL -w -D $DEST_CLUSTER start 2>/dev/null 1>/dev/null < /dev/null &
时间: 2024-11-03 22:04:04