如何在PostgreSQL实例上配置流复制

时间:2020-03-05 15:24:45  来源:igfitidea点击:

在本文中,我将在不同主机上向我们展示PostgreSQL安装及其Streaming Replication配置的两个PostgreSQL实例。
我们的目标是在从主机上安装Postgres 9.5,在从属主机上安装Postgres 9.5,并在它们之间配置流复制。

安装和配置

首先,我们将在运行CentOS的主主机上安装和准备PostgreSQL 9.5.
首先,我们需要使用rpm命令对CentOS-Release包进行查询以显示CentOS版本:

rpm --query centos-release

在我们的示例中,我们将以x86_64架构考虑Centos 6.8版。
现在,我们需要安装rpm包以运行yum install命令:

rpm -i https://yum.postgresql.org/9.5/redhat/rhel-6.8-x86_64/pgdg-redhat95-9.5-3.noarch.rpm

请通过运行检查一切都在正确的地方:

yum list postgresql95*

现在,我们可以安装PostgreSQL 9.5:

yum install postgresql95-server.x86_64

可以根据需求安装其他包。

安装PostgreSQL-9.5服务器后,需要初始化和配置数据库。
第一个命令(仅需要一次)是初始化pgdata中的数据库。

/etc/init.d/postgresql-9.5 initdb

或者

service postgresql-9.5 initdb

如果我们希望在操作系统启动时自动启动PostgreSQL,请执行以下操作:

chkconfig postgresql-9.5 on

此时我们已准备好开始服务,但之前我们将更新/var/lib/pgsql/9.5/data/postgresql.conf文件。
请取消注释listens_addresses行并与localhost一起键入ip地址。

listen_addresses = 'your IP, 127.0.0.1'

然后,请修改/var/lib/pgsql/9.5/data/pg_hba.conf文件如下:

# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all your IP md5

要控制数据库服务,请使用

service postgresql-9.5 [command]

其中[命令]可以是:

开始启动数据库停止停止数据库重新启动停止/启动数据库。
通常用于读取对核心配置文件的更改。
保留数据库运行时重新加载重新加载PG_HBA.conf文件

请启动服务,我们在主机主机上使用PostgreSQL 9.5安装完成。

下一步,我们将在从属主机上安装PostgreSQL 9.5.
步骤几乎相同。
首先,我们需要安装RPM包。

提醒:应在另一个主机,所谓的从主机上执行以下步骤。

rpm -i https://yum.postgresql.org/9.5/redhat/rhel-6.8-x86_64/pgdg-redhat95-9.5-3.noarch.rpm

并安装PostgreSQL 9.5

yum install postgresql95-server.x86_64

然后初始化它

service postgresql-9.5 initdb

使其在启动时自动启动

chkconfig postgresql-9.5 on

并修改配置文件/var/lib/pgsql/9.5/data/postgresql.conf如下:

listen_addresses = 'your IP, 127.0.0.1'

接下来,更新/var/lib/pgsql/9.5/data/pg_hba.conf文件:

# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all your IP md5

现在我们已准备好开始服务

service postgresql-9.5 start

复制配置

Streaming Replication提供了持续运送的功能,并将WAL Xlog(写入前瞻性日志)记录持续到某个数量的备用服务器以保持当前。
此功能已添加到PostOridQL 9.0中。
好的,所以我们安装了两个不同的PostgreSQL实例。
现在我们需要创建一个名为Replication的用户,并在主机上使用复制权限。

好的,所以我们安装了两个不同的PostgreSQL实例。
现在我们需要创建一个名为Replication的用户,并在主机上使用复制权限。

su - postgres
psql
CREATE ROLE replication WITH REPLICATION PASSWORD 'password' LOGIN

在主主机上设置连接和身份验证,以便待机(从机主机)可以成功连接到主机上的Replication Pseudo-Database。
编辑/var/lib/pgsql/9.5/data/pg_hba.conf.

host replication replication slave host IP md5

现在我们需要更新Masters Config文件/var/lib/pgsql/9.5/data/postgresql.conf:

# Add settings for extensions here
max_connections = 200
shared_buffers = 15GB
effective_cache_size = 45GB
work_mem = 39321kB
maintenance_work_mem = 2GB
checkpoint_segments = 32
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
#logging settings
log_directory = '/var/log/pg_log95'
log_filename = 'postgresql-%d_%H%M%S.log'
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 0
log_min_duration_statement = 30000
log_connections = true
tcp_keepalives_idle = 30
# Streaming replication
# To enable read-only queries on a standby server, wal_level must be set to
# "hot_standby". But you can choose "archive" if you never connect to the
# server in standby mode.
wal_level = hot_standby
# Set the maximum number of concurrent connections from the standby servers.
max_wal_senders = 5
# Enable WAL archiving on the primary to an archive directory accessible from
# the standby. If wal_keep_segments is a high enough number to retain the WAL
# segments required for the standby server, this is not necessary.
archive_mode = on
archive_command = 'rsync -a %p postgres@[SLAVE IP]:/db/psql95_wal_archive/%f'

我们需要将数据目录移动到从属主机上

mv /var/lib/pgsql/9.5/data /var/lib/pgsql/9.5/data_bkp

现在,我们需要通过将主机主机数据目录复制到从主机来进行基本备份。
我们可以在从机主机上使用pg_basebackup命令进行。

pg_basebackup -h [MASTER IP] -D /var/lib/pgsql/9.5/data -P -U replication --xlog-method=stream

然后,我们需要在从属主机上修改/var/lib/pgsql/9.3/data/postgresql.conf

# Add settings for extensions here
max_connections = 200
shared_buffers = 15GB
effective_cache_size = 45GB
work_mem = 39321kB
maintenance_work_mem = 2GB
checkpoint_segments = 32
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
#logging settings
log_directory = '/var/log/pg_log93'
log_filename = 'postgresql-%d_%H%M%S.log'
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 0
log_min_duration_statement = 30000
# Streaming replication
hot_standby = on

在从属主机/var/lib/pgsql/9.5/data/recovery.conf中创建一个恢复命令文件。
流复制需要以下参数

# Specifies whether to start the server as a standby. In streaming replication,
# this parameter must to be set to on.
standby_mode = 'on'
# Specifies a connection string which is used for the standby server to connect
# with the primary.
primary_conninfo = 'host=[MASTER IP] user=replication password=[PASSWORD]'
# Specifies a trigger file whose presence should cause streaming replication to
# end (i.e., failover).
trigger_file = '/var/lib/pgsql/9.5/data/trigger_file'
# Specifies a command to load archive segments from the WAL archive. If
# wal_keep_segments is a high enough number to retain the WAL segments
# required for the standby server, this Jan not be necessary. But
# a large workload can cause segments to be recycled before the standby
# is fully synchronized, requiring you to start again from a new base backup.
restore_command = 'cp /db/psql95_wal_archive/%f "%p"'
archive_cleanup_command = '/usr/pgsql-9.5/bin/pg_archivecleanup /db/psql95_wal_archive/%r'

请在/var/lib/pgsql/9.5/data的两个主机上确认,其子目录是postgres用户。
此外,我们还需要在从主机上创建目录/db/psql93_wal_archive/withgres所有者。
在重新启动PostgreSQL实例之前,最后一步是在Postgres用户的从主机上生成RSA密钥,并将其复制到Master

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa postgres@[MASTER IP]

现在,我们已准备好在主机上重新启动Postgres服务,然后在从站上重新启动Postgres服务。