如何在 PostgreSQL 實例上配置流式複制

在本文中,我將向您展示 Postgresql 安裝及其在不同主機上的兩個 PostgreSQL 實例的流式複製配置。 我們的目標是在 master 主機上安裝 Postgres 9.5,在 slave 主機上安裝 Postgres 9.5,並在它們之間配置流複製。

安裝和配置

首先,我們將在運行 CentOS 的主控主機上安裝和準備 PostgreSQL 9.5。 首先,我們需要使用 rpm 命令查詢 centos-release 包來顯示 CentOS 版本:

rpm --query centos-release

在我們的 example 我們將考慮使用 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 文件。 請取消對 listen_addresses 行的註釋並輸入您的 IP 地址和 localhost。

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]

在哪裡 [command] 可:

start – 啟動數據庫
停止 – 停止數據庫
重新啟動 – 停止/啟動數據庫。 通常用於讀取對核心配置文件的更改。
重新加載 – 在保持數據庫運行的同時重新加載 pg_hba.conf 文件

請啟動該服務,我們就完成了在 master 主機上安裝 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

複製配置

流式複制提供了將 WAL XLOG(預寫日誌)記錄持續傳送和應用到一定數量的備用服務器以保持最新的能力。 此功能已添加到 PostgresSQL 9.0。
好的,所以我們在不同的主機上安裝了兩個不同的 PostgreSQL 實例。 現在我們需要在 master 主機上創建一個具有 REPLICATION 權限的名為 replication 的用戶。

好的,所以我們在不同的主機上安裝了兩個不同的 PostgreSQL 實例。 現在我們需要在 master 主機上創建一個具有 REPLICATION 權限的名為 replication 的用戶。

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

在 master 主機上設置連接和認證,以便備用(從主機)可以成功連接到 primary 上的複制偽數據庫。 編輯 /var/lib/pgsql/9.5/data/pg_hba.conf

host replication replication slave host IP md5

現在你需要更新主配置文件 /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'

我們需要移動slave主機上的數據目錄

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

然後你需要在slave主機上修改/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 may 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 用戶。 此外,您需要在從屬主機上使用 postgres 所有者創建目錄 /db/psql93_wal_archive/。
而重啟 PostgreSQL 實例前的最後一步是在 slave 主機上為 postgres 用戶生成 RSA 密鑰並將其複製到 master

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

現在,您已準備好先在 master 上重新啟動 postgres 服務,然後在 slave 上重新啟動。

結論

我們配置了流式複制,與基於文件的日誌傳送相比,它允許備用服務器保持更新。 備用數據庫連接到主數據庫,主數據庫在生成 WAL 記錄時將它們流式傳輸到備用數據庫,而無需等待 WAL 文件被填充。