一、安裝環境
1.CentOS-7.5-x86_64
2.PostgreSQL-9.6
二、數據庫安裝
1.host文件添加服務器地址
[root@master ~]# vim /etc/hosts
192.168.124.5 master
192.168.124.6 slave
192.168.124.11 vip
2.數據庫安裝
在主備兩臺服務器安裝數據庫
[root@master ~]# mv /root/postgresql-9.6.2-1-linux-x64.run /opt
[root@master ~]# cd /opt
[root@master opt]# chmod +x postgresql-9.6.2-1-linux-x64.run
[root@master opt]# ./postgresql-9.6.2-1-linux-x64.run
按默認安裝,編碼選擇zh_CN.utf8
3.修改postgres用戶密碼
[root@master ]# passwd postgres
4.設置環境變量
[root@master ~]# vim /etc/profile
export PATH=/opt/PostgreSQL/9.6/bin:$PATH
export PGDATA=/opt/PostgreSQL/9.6/data
保存後運行source /etc/profile
三、數據庫主機配置
1.創建用戶
[root@master ~]# su - postgres
-bash-4.2$ psql
postgres=# create role repl login replication encrypted password 'repl’;
如果成功的則返回CREATE ROLE
postgres=# \q
-bash-4.2$ exit
2.編輯pg_hba.conf文件
[root@master ~]# cd $PGDATA
[root@master ~]# vim pg_hba.conf
local all all md5
# IPv4 local connections:
host replication repl 192.168.124.6/24 md5
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 md5
3.編輯postgresql.conf
[root@master ~]# cd $PGDATA
先備份postgresql.conf:
[root@master data]# cp postgresql.conf postgresql.conf.bak
[root@master data]# vim postgresql.conf
wal_level = replica
max_wal_senders = 2
wal_keep_segments = 256 #10240 16m/個
wal_send_timeout = 60s
max_connections = 100
archive_mode = on
wal_log_hints = on #上一行要空一行,也就是編輯插入一個回車
log_destination = 'csvlog'
logging_collector = on
log_filename = postgresql-%Y-%m-%d.log
hot_standby = on
保存
4.編輯recovery.done
主庫剛安裝完編輯的recovery文件爲recovery.done
[root@master data]# cp /opt/PostgreSQL/9.6/share/postgresql/recovery.conf.sample recovery.done
[root@master data]# chown postgres:postgres recovery.done
[root@master data]# vi recovery.done
recovery_target_timeline = 'latest'
primary_conninfo='host=slave port=5432 user=repl password=repl' #沒有空格
trigger_file='/opt/PostgreSQL/9.6/data/standby' #沒有空格
standby_mode = on
重新啓動主數據庫服務
[root@master data]# systemctl restart postgresql-9.6.service
四、數據庫備機配置
1.從主庫備份數據庫到備庫
停止數據庫服務
[root@slave ]# systemctl stop postgresql-9.6.service
[root@slave ]# cd /opt/PostgreSQL/9.6
[root@slave 9.6 ]# rm -rf data
[root@slave 9.6 ]# mkdir data
[root@slave 9.6 ]# chown postgres:postgres data
[root@slave 9.6 ]# chmod -R 0700 data
[root@slave 9.6 ]# su - postgres
-bash-4.2$ pg_basebackup -D $PGDATA -Fp -Xs -v -P -h master -p 5432 -U repl
輸入密碼:repl
2.編輯pg_hba.conf文件
[root@slave ~]# cd $PGDATA
[root@slave ~]# vim pg_hba.conf
local all all md5
# IPv4 local connections:
host replication repl 192.168.124.5/24 md5
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 md5
3.編輯recovery.conf
[root@slave data]# mv recovery.done recovery.conf
[root@slave data]# chown postgres:postgres recovery.conf
[root@slave data]# vim recovery.conf
primary_conninfo = 'host=master port=5432 user=repl password=repl'
host=slave修改爲master,修改完需確認hosts文件已經添加了master
[root@slave data]# systemctl start postgresql-9.6.service
五、主備切換
1.驗證主備狀態
[root@slave ~]# pg_controldata -D $PGDATA | grep cluster
Database cluster state: in archive recovery
生產: in production 備機: in archive recovery
[root@master ~]# ps -aef|grep postgres
在主機使用sql查詢:select * from pg_stat_replication
2.slave切換爲主機
[root@slave ~]# touch $PGDATA/standby
[root@slave ~]# pg_controldata -D $PGDATA | grep cluster
Database cluster state: in production
此時原備機106變爲主機,revcovery.conf自動改變爲recovery.done
3.master切換爲備機
[root@master ~]# systemctl stop postgresql-9.6.service
[root@slave ~]# cd /opt/PostgreSQL/9.6/data/
[root@slave data]# mv recovery.done recovery.conf
[root@master ~]# systemctl start postgresql-9.6.service
4.master恢復爲主機
[root@master ~]# touch $PGDATA/standby
此時master恢復爲主機
5.slave恢復爲備機
在slave服務器,停止postgresql服務
[root@slave ]# systemctl stop postgresql-9.6.service
[root@slave data]# mv recovery.done recovery.conf
[root@slave ]# systemctl start postgresql-9.6.service
驗證主備狀態,此時slave恢復爲備機