第一次做 postgres 复制——不工作
First time doing postgres replication -- not working
滚动到底部进行更新
这是我第一次在已经复制到服务器的生产服务器上配置复制。我正在添加一个额外的服务器。复制在现有环境中工作,但我添加的附加服务器不工作。因为我不想破坏现有的复制,所以我在主服务器上为附加服务器创建了一个新用户。这是主从版本 9.3.5。
psql -c "CREATE USER rep REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'yourpassword';"
在slave上我创建了一个recovery文件并将它放在data目录下
standby_mode = 'on'
primary_conninfo = 'host=master_IP_address port=5432 user=rep
password=yourpassword'
trigger_file = '/tmp/postgresql.trigger.5432'
在master上,我开始了初始备份。
psql -c "select pg_start_backup('initial_backup');"
已通过 rsync 复制数据。
rsync -cva --inplace --exclude=*pg_xlog* /var/lib/postgresql/9.1/main/ slave_IP_address:/var/lib/postgresql/9.1/main/
rsync 完成后,我发出了以下命令。
psql -c "select pg_stop_backup();"
主服务器 - posgresql.conf
listen_addresses = '*'
max_connections = 1000
unix_socket_directories = ''
shared_buffers = 1024MB
temp_buffers = 1024MB
work_mem = 64MB
maintenance_work_mem = 512MB
max_stack_depth = 31MB
wal_level = hot_standby
wal_buffers = -1
checkpoint_segments = 512
checkpoint_timeout = 1h
# - Archiving -
archive_mode = on
archive_command = 'cd .'
max_wal_senders = 5
主服务器 - pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/8 trust
host all all 192.168.10.10/32 trust
host all all ny-node0.xxx.net trust
# below is the existing replication server that works.
host all all ny-node1.xxx.net trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication postgres trust
host replication postgres 127.0.0.1/8 trust
host replication postgres ny-node1.xxx.net trust
host replication rep IP-OF-NEW-SLAVE/32 trust
host replication postgres IP-OF-MASTER/32 trust
奴隶postgresql.conf
listen_addresses = '*'
max_connections = 100
shared_buffers = 128MB
wal_level = hot_standby
archive_command = 'cd .'
logfile segment
max_wal_senders = 1
hot_standby = on
hot_standby_feedback = on
wal_receiver_timeout = 60s
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_timezone = 'UTC'
datestyle = 'iso, mdy'
timezone = 'UTC'
lc_messages = 'en_US.UTF-8'
lc_monetary = 'en_US.UTF-8'
lc_numeric = 'en_US.UTF-8'
lc_time = 'en_US.UTF-8'
default_text_search_config = 'pg_catalog.english'
奴隶的pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres md5
host replication rep IP-OF-MASTER/32 trust
#host replication postgres ::1/128 md5
我在主服务器和从服务器上重新启动了 postgres,当我在主服务器上创建数据库时,它没有复制到新的从服务器。
我有一些进步(更新)
我调整了从服务器上的 pg_hba.conf,只留下与我为复制创建的新用户关联的行,称为 'rep'。
#local replication rep md5
host replication rep ip_of_slave/32 trust
host replication rep ip_of_master/32 trust
我还调整了我的 recovery.conf 并将其放在数据目录中。
由于我主从之间有信任关系,所以我删除了recovery.conf.
中的密码
standby_mode = 'on'
primary_conninfo = 'host=ip_of_master port=5432 user=rep'
trigger_file = '/tmp/postgresql.trigger.5432'
现在我的日志显示:
LOG: database system was shut down in recovery at 2019-09-02 20:02:04 UTC
LOG: entering standby mode
LOG: contrecord is requested by 26/78000028
LOG: started streaming WAL from primary at 26/78000000 on timeline 7
LOG: contrecord is requested by 26/78000028
FATAL: terminating walreceiver process due to administrator command
LOG: contrecord is requested by 26/78000028
LOG: contrecord is requested by 26/78000028
LOG: contrecord is requested by 26/78000028
LOG: contrecord is requested by 26/78000028
LOG: contrecord is requested by 26/78000028
看来你在将recovery.conf
放入数据目录之前启动了从服务器。在这种情况下,PostgreSQL 将在恢复后作为独立数据库出现,而不是成为备用服务器。
一旦发生这种情况,您必须丢弃奴隶并重新开始。
正确的运算顺序是:
SELECT pg_start_backup('label');
执行rsync
SELECT pg_stop_backup();
将recovery.conf
和standby_mode = 'on'
放入slave的数据目录
启动slave
滚动到底部进行更新
这是我第一次在已经复制到服务器的生产服务器上配置复制。我正在添加一个额外的服务器。复制在现有环境中工作,但我添加的附加服务器不工作。因为我不想破坏现有的复制,所以我在主服务器上为附加服务器创建了一个新用户。这是主从版本 9.3.5。
psql -c "CREATE USER rep REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'yourpassword';"
在slave上我创建了一个recovery文件并将它放在data目录下
standby_mode = 'on'
primary_conninfo = 'host=master_IP_address port=5432 user=rep
password=yourpassword'
trigger_file = '/tmp/postgresql.trigger.5432'
在master上,我开始了初始备份。
psql -c "select pg_start_backup('initial_backup');"
已通过 rsync 复制数据。
rsync -cva --inplace --exclude=*pg_xlog* /var/lib/postgresql/9.1/main/ slave_IP_address:/var/lib/postgresql/9.1/main/
rsync 完成后,我发出了以下命令。
psql -c "select pg_stop_backup();"
主服务器 - posgresql.conf
listen_addresses = '*'
max_connections = 1000
unix_socket_directories = ''
shared_buffers = 1024MB
temp_buffers = 1024MB
work_mem = 64MB
maintenance_work_mem = 512MB
max_stack_depth = 31MB
wal_level = hot_standby
wal_buffers = -1
checkpoint_segments = 512
checkpoint_timeout = 1h
# - Archiving -
archive_mode = on
archive_command = 'cd .'
max_wal_senders = 5
主服务器 - pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/8 trust
host all all 192.168.10.10/32 trust
host all all ny-node0.xxx.net trust
# below is the existing replication server that works.
host all all ny-node1.xxx.net trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication postgres trust
host replication postgres 127.0.0.1/8 trust
host replication postgres ny-node1.xxx.net trust
host replication rep IP-OF-NEW-SLAVE/32 trust
host replication postgres IP-OF-MASTER/32 trust
奴隶postgresql.conf
listen_addresses = '*'
max_connections = 100
shared_buffers = 128MB
wal_level = hot_standby
archive_command = 'cd .'
logfile segment
max_wal_senders = 1
hot_standby = on
hot_standby_feedback = on
wal_receiver_timeout = 60s
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_timezone = 'UTC'
datestyle = 'iso, mdy'
timezone = 'UTC'
lc_messages = 'en_US.UTF-8'
lc_monetary = 'en_US.UTF-8'
lc_numeric = 'en_US.UTF-8'
lc_time = 'en_US.UTF-8'
default_text_search_config = 'pg_catalog.english'
奴隶的pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres md5
host replication rep IP-OF-MASTER/32 trust
#host replication postgres ::1/128 md5
我在主服务器和从服务器上重新启动了 postgres,当我在主服务器上创建数据库时,它没有复制到新的从服务器。
我有一些进步(更新)
我调整了从服务器上的 pg_hba.conf,只留下与我为复制创建的新用户关联的行,称为 'rep'。
#local replication rep md5
host replication rep ip_of_slave/32 trust
host replication rep ip_of_master/32 trust
我还调整了我的 recovery.conf 并将其放在数据目录中。 由于我主从之间有信任关系,所以我删除了recovery.conf.
中的密码standby_mode = 'on'
primary_conninfo = 'host=ip_of_master port=5432 user=rep'
trigger_file = '/tmp/postgresql.trigger.5432'
现在我的日志显示:
LOG: database system was shut down in recovery at 2019-09-02 20:02:04 UTC
LOG: entering standby mode
LOG: contrecord is requested by 26/78000028
LOG: started streaming WAL from primary at 26/78000000 on timeline 7
LOG: contrecord is requested by 26/78000028
FATAL: terminating walreceiver process due to administrator command
LOG: contrecord is requested by 26/78000028
LOG: contrecord is requested by 26/78000028
LOG: contrecord is requested by 26/78000028
LOG: contrecord is requested by 26/78000028
LOG: contrecord is requested by 26/78000028
看来你在将recovery.conf
放入数据目录之前启动了从服务器。在这种情况下,PostgreSQL 将在恢复后作为独立数据库出现,而不是成为备用服务器。
一旦发生这种情况,您必须丢弃奴隶并重新开始。
正确的运算顺序是:
SELECT pg_start_backup('label');
执行
rsync
SELECT pg_stop_backup();
将
recovery.conf
和standby_mode = 'on'
放入slave的数据目录启动slave