PostgreSQL HA

兩種主從複製方式log-shippingstreaming replication,以下是Streaming Replication的搭建步驟:

Replication

Master配置

Step 1 .更改用戶postgresrepl用戶的密碼

[root@bogon ~]# su - postgres
-bash-4.2$  psql -U postgres
psql (11.6)
Type "help" for help.

postgres=# ALTER USER postgres with encrypted password 'One.00000';
ALTER ROLE
postgres=# create role repl login replication encrypted password 'One.00000';
CREATE ROLE

Step 2. 編輯pg_hba.conf

vim /var/lib/pgsql/11/data/pg_hba.conf

host    replication     repl            192.168.0.0/24         md5
host    all             repl            192.168.0.0/24         trust

Step 3. 編輯默認的postgresql配置文件postgresql.conf

vim /var/lib/pgsql/11/data/postgresql.conf 

# - Connection Settings -
listen_addresses = '*'    # what IP address(es) to listen on;
port = 5432 

# - Settings -
wal_level = replica                     # minimal, replica, or logical

# - Sending Servers -
# Set these on the master and on any standby that will send replication data.
max_wal_senders = 10        # max number of walsender processes                              
wal_keep_segments = 1024        # in logfile segments; 0 disables
wal_sender_timeout = 60s        # in milliseconds; 0 disables

Step 4. 重啓服務

systemctl restart postgresql-11

Slave配置

Step 1. 從遠程讀取主節點備份

從數據庫不進行初始化,只進行安裝步驟,並下載主節點備份

[root@bogon ~]# pg_basebackup -h 192.168.0.169 -U repl -D /var/lib/pgsql/11/data/ -X stream -P
Password: 
24599/24599 kB (100%), 1/1 tablespace

Step 2. 編輯從節點postgresql.conf

編輯從節點的postgresql配置文件postgresql.conf

# - Standby Servers -

# These settings are ignored on a master server.

hot_standby = on                        # "off" disallows queries during recovery
                                        # (change requires restart)
max_standby_archive_delay = 30s # max delay before canceling queries
                                        # when reading WAL from archive;
                                        # -1 allows indefinite delay
max_standby_streaming_delay = 30s       # max delay before canceling queries
                                        # when reading streaming WAL;
                                        # -1 allows indefinite delay
wal_receiver_status_interval = 10s      # send replies at least this often
                                        # 0 disables
hot_standby_feedback = on               # send info from standby to prevent
                                        # query conflicts

Step 3. 編輯recovery.conf

$> vim /var/lib/pgsql/11/data/recovery.conf

primary_conninfo = 'host=192.168.0.169 port=5432 user=repl password=One.00000' 
recovery_target_timeline = 'latest'
standby_mode = on

Step 4. 重啓服務

chown -R postgres:postgres /var/lib/pgsql/11/data
systemctl start postgresql-11

驗證

  1. 驗證從服務器是否連接
postgres=# select client_addr,sync_state from pg_stat_replication;
  client_addr  | sync_state 
---------------+------------
 192.168.0.170 | async

  1. 驗證數據是否同步
    PostgreSQL是遵循SQL規範的,因此你可以像MySQL那樣建立普通表並插入樣例數據。

基於文件的日誌傳送

file-based log-shipping
主服務器以異步方式將WAL(write-ahead-log)發送的從服務器。WAL文件在事務提交之後才被傳送,因此主從之間會有一定的數據丟失風險。

流式複製

The step that turns a file-based log-shipping standby into streaming replication standby is setting the primary_conninfo setting to point to the primary server. Set listen_addresses and authentication options (see pg_hba.conf) on the primary so that the standby server can connect to the replication pseudo-database on the primary server

Reference List

  1. https://www.postgresql.org/download/linux/redhat/
  2. https://www.postgresql.org/docs/11/warm-standby.html

配置優化

Reference List

  1. PostgreSQL配置優化
  2. PostgreSQL Documentation: Server Configuration
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章