PostgreSQL repmgr + VIP + logical replication 高可用性與邏輯同步架構 安裝實做版本 PostgreSQL 14.2 repmgr 5.3 (2) PostgreSQL、repmgr 安裝與 PostgreSQL 資料庫設定

本篇要來說明從source code怎麼安裝PostgreSQL以及repmgr

Step 1. 編輯 /etc/hosts 主機資訊

[root@pgrep1 ~]# cat /etc/hosts
172.30.0.21 pgrep1
172.30.0.22 pgrep2

[root@pgrep2 ~]# cat /etc/hosts
172.30.0.21 pgrep1
172.30.0.22 pgrep2

Step 2. 從PostgreSQL官網下載 source code,放到/pgbin底下解tar,並將它安裝至$PGHOME下

[root@pgrep2 ~]# cd /pgbin
[root@pgrep2 pgbin]# mkdir pghome_1
[root@pgrep2 pgbin]# tar xvf postgresql-14.2.tar.gz

Step 3. 進到解tar資料夾下,執行./configure及make;筆者把預設pgport 5432改為5699,並將binary目錄對應到/pgbin/pghome_1 (類似Oracle $ORACLE_HOME的裝法)

[root@pgrep2 pgbin]# cd postgresql-14.2
[root@pgrep2 pgbin]# ./configure --prefix=/pgbin/pghome_1 --with-pgport=5699
[root@pgrep2 pgbin]# make & make install

Step 4. 從repmgr官網下載 source code,放到/pgbin底下解tar,並將它安裝至$PGHOME下

[pgadm@pgrep1 pgbin]$ tar xvf repmgr-5.3.1.tar.gz
[pgadm@pgrep1 pgbin]$ cd repmgr-5.3.1
[pgadm@pgrep1 repmgr-5.3.1]$ ./configure --prefix=/pgbin/pghome_1
[pgadm@pgrep1 repmgr-5.3.1]$ make && make install

Step 5. 將/pgbin & /pgdata資料夾權限改成pgadm的權限

[root@pgrep1 ~]# chown pgadm.postgres /pgdata

-- 這裡帶-R是因為/pgbin底下還有pghome_1的資料夾,因此一併修改
[root@pgrep1 ~]# chown -R pgadm.postgres /pgbin

Step 6. 修改pgadm的環境變數 .bash_profile

[root@pgrep1 ~]# su - pgadm
[pgadm@pgrep1 ~]$ vim .bash_profile

export PGHOME=/pgbin/pghome_1
export PGDATA=/pgdata/dbdata
export PGPORT=5699
export PATH=$PATH:$PGHOME/bin

[pgadm@pgrep1 ~]$ source ~/.bash_profile

步驟1~6,每台要建成Cluster PostgreSQL主機都需要執行


以下步驟只需要找其中1台PostgreSQL主機,作為Cluster Master使用

Step 7. 初始化資料庫

[pgadm@pgrep1 ~]$ initdb -D $PGDATA
The files belonging to this database system will be owned by user "pgadm".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are enabled.

creating directory /pgdata/dbdata ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Taipei
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /pgdata/dbdata -l logfile start

Step 8. 啟動PostgreSQL服務

[pgadm@pgrep1 ~]$ pg_ctl -D $PGDATA -l logfile start
waiting for server to start.... done
server started

Step 9. 修改資料庫pgadm管理者密碼

[pgadm@pgrep1 ~]$ psql postgres
postgres=# alter user pgadm with password 'password';
ALTER ROLE

Step 10. 設定本機superuser keyless登入

[pgadm@pgrep1 ~]$ vim .pgpass 
pgrep1:5699:postgres:pgadm:password
[pgadm@pgrep1 ~]$ chmod 0600 .pgpass 
[pgadm@pgrep1 ~]$ ls -l .pgpass
-rw-------. 1 pgadm postgres 36 Mar 18 16:38 .pgpass

Step 11. 設定postgresql.conf

[pgadm@pgrep1 ~]$ cd /pgdata/dbdata
[pgadm@pgrep1 dbdata]$ vim postgresql.conf
# log 相關設定
logging_collector = on
log_directory = '/pgdata/pglog'
log_file_mode = 0640
log_rotation_age = 1d
log_rotation_size = 200MB
log_truncate_on_rotation = on

# 運行設定
listen_addresses = '*'
port = 5699
wal_log_hints = on
hot_standby = on
max_wal_senders = 10
max_replication_slots = 10
wal_keep_size = 1024 --old parameter name is wal_keep_segments
archive_mode = on
# archive_command 依需要,調整成匯出至共用目錄(ex: NAS)
archive_command = '/bin/true'

# Cluster設定
cluster_name = 'repmgrclu'
shared_preload_libraries = 'repmgr'

Step 12. 設定pg_hba.conf (類似於連線防火牆)

[pgadm@pgrep1 ~]$ egrep -v "^#" $PGDATA/pg_hba.conf | egrep "md5" 
host    all             all            0.0.0.0/0                md5

Step 13. 重啟資料庫

[pgadm@pgrep1 ~]$ pg_ctl restart -D $PGDATA -l logfile
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started


PostgreSQL repmgr + VIP + logical replication 高可用性與邏輯同步架構 安裝實做版本 PostgreSQL 14.2 repmgr 5.3 (1) 架構介紹與前置作業

PostgreSQL repmgr + VIP + logical replication 高可用性與邏輯同步架構 安裝實做版本 PostgreSQL 14.2 repmgr 5.3 (2) PostgreSQL、repmgr 安裝與 PostgreSQL 資料庫設定

PostgreSQL repmgr + VIP + logical replication 高可用性與邏輯同步架構 安裝實做版本 PostgreSQL 14.2 repmgr 5.3 (3) repmgr Cluster設定

PostgreSQL repmgr + VIP + logical replication 高可用性與邏輯同步架構 安裝實做版本 PostgreSQL 14.2 repmgr 5.3 (4) repmgr switchover/failover 與 VIP 權限設定

PostgreSQL repmgr + VIP + logical replication 高可用性與邏輯同步架構 安裝實做版本 PostgreSQL 14.2 repmgr 5.3 (5) repmgr auto failover

PostgreSQL repmgr + VIP + logical replication 高可用性與邏輯同步架構 安裝實做版本 PostgreSQL 14.2 repmgr 5.3 (6) logical replication

PostgreSQL repmgr + VIP + logical replication 高可用性與邏輯同步架構 安裝實做版本 PostgreSQL 14.2 repmgr 5.3 (7) Config log rotation & systemd & selinux

張貼留言

0 留言