无法让我的从站 - 复制服务器启动

Det*_*tox 4 postgresql replication

我的复制服务器将无法启动。我已按照此处的说明进行操作:http :
//opensourcedbms.com/dbms/setup-replication-with-postgres-9-2-on-centos-6redhat-el6fedora/

以及包括 Postgres Wiki 在内的其他几个地方,因为它们都有相同的信息。

这是发生的事情:
我对/9.2/data文件夹进行完整备份并将其移动到复制/从服务器,解压缩它。我可以启动 PostgreSQL 和 pgAdmin 并毫无问题地访问所有数据。

然后我转到有关编辑从服务器的pg_hba.conf和的说明postgresql.conf。我尝试启动它,但它失败了(红色错误 [失败]。我在任何地方都找不到任何日志来提示我为什么。

我什至验证postmaster.pid了数据文件夹中没有。

另外,我找不到任何日志文件。我需要在配置中“激活”一个日志文件吗?

因此,如果有人想对我含糊的描述一无所知,我很想听听任何建议。如果有帮助,我可以将我的 conf 文件放在 pastebin 上。

bma*_*bma 18

设置双机热备的步骤

首先,确定 Postgresql 数据目录和WAL(Write Ahead Log)目录的挂载点。为提高性能,$PGDATApg_xlog目录应位于不同的卷上。

在下面的示例中,它们的定义如下:

  • $PGDATA 是“主”集群,位于 /pgdata/9.3/data,在(例如)192.168.10.0
  • “从”(又名“热备用”)将(例如)位于 /pgdata/9.3/data,在(例如)192.168.10.1
  • 从站上的 WAL 暂存区将是 /pgdata/WAL_Archive。这是 WAL 段从archive_command主机上发送到的地方。理想情况下,位于与 $PGDATA 不同的卷上。

假设:

  1. 主集群已启动并正常运行。
  2. 还没有从集群。
  3. 您正在以“postgres”操作系统用户(不是 root!)的身份执行所有操作
  4. 两台服务器之间的 ssh 工作正常
  5. 您至少使用 PostgreSQL 9.2
  6. pg_hba.conf 和 postgresql.conf 位于 $PGDATA/。如果没有,请更改以下说明以匹配您的位置。
  7. 您正在单独的服务器上设置从站。
  8. 如果服务器不在同一个网络上(例如,不同的托管),请在传输前使用主服务器上的“archive_command” rsync 中的“-z”标志进行压缩。
  9. 这些命令适用于 Linux,但替换一些命令也适用于 Windows。

在从站上,创建暂存目录以保存主站的日志文件

mkdir -p /pgdata/WAL_Archive
chown postgres:postgres /pgdata/WAL_Archive
Run Code Online (Sandbox Code Playgroud)

在 master 上,编辑 $PGDATA/postgresql.conf

wal_level = hot_standby
archive_mode = on
                    ## /pgdata/WAL_Archive is a staging directory on the slave
archive_command = 'rsync -W -az %p postgres@$SLAVE_IP_HERE:/pgdata/WAL_Archive/'
max_wal_senders = 5
wal_keep_segments = 5000   # If you have the room, to help the pg_basebackup
                           # not fail due to WAL segments being removed from master.
                           # For clusters will very little traffic, 100 is probably fine
Run Code Online (Sandbox Code Playgroud)

在主服务器上,创建复制角色,该角色将通过pg_basebackup复制到从服务器。为“复制”角色设置密码

psql -U postgres -d postgres -c "CREATE USER replication WITH replication ENCRYPTED PASSWORD 'changeme' LOGIN"
Run Code Online (Sandbox Code Playgroud)

修改master $PGDATA/pg_hba.conf 并为slave的IP启用复制用户

# TYPE DATABASE USER ADDRESS METHOD
#hostssl    replication     replication     $SLAVE_IP_HERE/32       md5
host    replication     replication     $SLAVE_IP_HERE/32       md5
Run Code Online (Sandbox Code Playgroud)

重新启动主集群以获取对 postgresql.conf 的更改。这是作为集群超级用户完成的。

例如:

pg_ctl -D $PGDATA restart -m fast
## The master cluster MUST be restarted before the pg_basebackup command is executed.
Run Code Online (Sandbox Code Playgroud)

在从服务器上,从 $HOME 发出 pg_basebackup 命令以从主服务器开始设置热备份。

## --host=IP_OF_MASTER  -> The master's IP
## --pgdata=$PGDATA     -> The slave's $PGDATA directory
## --xlog-method=stream -> Opens a second connection to the master to stream the WAL segments rather than pulling them all at the end
## --password will prompt for the replication role's password
## Without compression, "stream" gets the changes via the same method as Streaming Replication

time pg_basebackup --pgdata=$PGDATA --host=IP_OF_MASTER --port=5432 --username=replication --password --xlog-method=stream --format=plain --progress --verbose

## Alternate version with compression, note "--xlog --gzip --format=tar"
#time pg_basebackup --pgdata=$PGDATA --host=IP_OF_MASTER --port=5432 --username=replication --password --xlog --gzip --format=tar --progress --verbose
Run Code Online (Sandbox Code Playgroud)

在从站上,在 pg_basebackup 成功完成后,编辑 $PGDATA/postgresql.conf

hot_standby = on #off               # "on" allows queries during recovery
max_standby_archive_delay = 15min   # max delay before canceling queries,
                                    # set to hours if backups will be taken from here
max_standby_streaming_delay = 15min # max delay before canceling queries
hot_standby_feedback = on #off
Run Code Online (Sandbox Code Playgroud)

在从站上,创建$PGDATA/recovery.conf

standby_mode = on

## To promote the slave to a live database, issue "touch /tmp/promote_db"
## Warning: If multiple slaves share the same /tmp/ directory,
##          then the trigger file must be named uniquely, else multiple slaves
##          could attempt to be promoted in the presence of the trigger file.
trigger_file = '/tmp/promote_db_slave'

## Host can be the master's IP or hostname
primary_conninfo = 'host=IP_OF_MASTER port=5432 user=replication password=CHANGEME'

## Log the standby WAL segments applied to a standby.log file
## TODO: Add the standby.log to a log rotator
## The paths must be explicitly defined, including the path to pg_archivecleanup
restore_command = 'cp /pgdata/WAL_Archive/%f "%p" 2>>/pgdata/9.3/data/pg_log/standby.log'

## XXX: If multiple slaves share the staging WAL directory,
##      do not use pg_archivecleanup as WAL segments could be removed
##      before being applied to other slaves.
archive_cleanup_command = '/usr/pgsql-9.3/bin/pg_archivecleanup /pgdata/WAL_Archive %r'

## On hot standby clusters, set to 'latest' to switch to the newest timeline in the archive
recovery_target_timeline = 'latest'
Run Code Online (Sandbox Code Playgroud)

从站现在应该准备好启动了。使用任何最有效的方法在从站上启动集群。

pg_ctl -D $PGDATA start
Run Code Online (Sandbox Code Playgroud)

以下是我在一遍又一遍地重新创建备用数据库进行测试时运行的命令。我将它们添加到名为 /root/recreate_standby.sh 的脚本中。以 root 身份运行,但这不是必需的 - 如果您以 postgres 身份运行,则删除“sudo su - postgres -c”命令。

#!/bin/bash

## This script runs on the standby.
## Executed as root, else remove the "sudo - postgres -c" commands.
## Assumes you have a valid recovery.conf saved at
## $PGDATA/../recovery.conf.bkp

export PGDATA=/path/to/data/dir     ## Must be set correctly
export PGPORT=5432
export MASTER=192.168.x.x            ## IP or host entry for the master Postgresql server
export PGBIN=/usr/pgsql-9.3/bin

service postgresql-9.3 stop -m immediate

if [ $? != 0 ]; then
    service postgresql-9.3 start
    echo "Could not shut down PostgreSQL. Aborting."
    exit 1
fi

rm -rf $PGDATA

if [ $? != 0 ]; then
    echo "Could not remove the PostgreSQL $PGDATA dir. Aborting."
    exit 1
fi

## If the replication role is not set to "trust" in the master's
## pg_hba.conf file, the password will need to be passed into the command below,
## and "--no-password" will need to be removed or revised to be "--password"
su - postgres -c "$PGBIN/pg_basebackup --pgdata=$PGDATA --host=$MASTER --port=$PGPORT --username=replication --no-password --xlog-method=stream --format=plain --progress --verbose"

su - postgres -c "cp -p $PGDATA/../recovery.conf.bkp $PGDATA/recovery.conf"

service postgresql-9.3 start

su - postgres -c "$PGBIN/pg_isready -U postgres -p $PGPORT -t2"

while [ $? != 0 ]; do
  echo "Sleep 1 second, check if slave is up yet. If not, sleep again."
  sleep 1;
  su - postgres -c "$PGBIN/pg_isready -U postgres -p $PGPORT -t2"
done

su - postgres -c "$PGBIN/psql -d postgres -U postgres -qXc 'select pg_is_in_recovery() as is_pg_in_recovery'"

exit 0
Run Code Online (Sandbox Code Playgroud)

有关更深入的详细信息,请参阅当前的 Postgresql 文档: