1. 程式人生 > >Postgresql 10 流複製配置過程-實測

Postgresql 10 流複製配置過程-實測

我是用的兩個NanoPI裝的armbian系統,就是一個小型的ubuntu小型系統,真是太完美了,就是晶片溫度有些高,完全可以當是做UBUNTU最小系統來使用,要用什麼也都可以apt-get就可以搞定,這是後話了(-_-)

所以用了兩臺小板兒作了這麼個postgresql流複製玩,該操作對於普通LINUX同樣適用,因為我的Postgresql是下載的原始碼進行的安裝,安裝過程很簡單,待再另起一個文件吧,等整理完放這上一個連結

我也是初學都,所以在網上參考了好多設定方法,最後多次測試後,終於設定成功了,而且主從切換也測試完成,10版本的在來回切換時會自動同步資料,不需要之前那樣自制時間線什麼的操作了,寫下一是記錄,二是有需要的可以參考,謝謝


一 主備機器規劃

主機名 | IP | 角色 | 埠
:----:|:----:|:----:|:----:|:----:|:----:
NanoPI-005|10.10.10.205|Master|5432
NanoPI-006|10.10.10.206|Slave|5432
前提:分別在兩臺主機上安裝好pg資料庫,我是使用用的原始碼編譯安裝的10版本。


二 建立流複製
2.1 設定host
master,slave兩節點都要操作。
[[email protected] ~]# vim /etc/hosts
#編輯內容如下:
10.10.10.205NanoPI-005
10.10.10.206NanoPI-006


2.2 在主庫設定
2.2.1先初始化新資料庫
[email protected]
:~$initdb -D ~/data/

2.2.2啟動資料庫並建立同步使用者
[email protected]:~$pg_ctl -D ~/data/ -l ~/log/pglog.log start
[email protected]:~$psql
postgres=#create role 同步用的使用者名稱 login replication encrypted password '密碼';
CREATE ROLE
postgres=#\q--退出psql

2.2.3配置~/data/pg_hba.conf
新增下面內容
hostreplication在資料庫裡建立的同步用的使用者名稱備庫IP地址或域名/32trust

2.2.4配置~/data/postgres.conf
查詢並修改成以下內容
改監控埠:
Listen_adresses = ‘*’
wal_level = hot_standby /10以後的版本為replica 主從設定為熱血模式,流複製必選
max_wal_senders=2/預設是10也可以流複製允許連線程序,最好多點,我2沒成,設定成10好了
wal_keep_segments =64
max_connections = 100預設引數,非主從配置相關引數,表示到資料庫的連線數

2.2.5重啟主庫服務,以更新配置
[email protected]
:~$pg_ctl -D ~/data/ -l ~/log/pglog.log restart

2.3 在從庫設定
2.3.1不需要初始化,直接從主庫備份就行,如有DATA直接刪掉或改名掉
[email protected]:~$pg_basebackup -h 主庫地址10.10.10.205 -p 5432 -U 資料庫中建立的同步用的使用者名稱 -F p -P -D ~/data/
可能得輸入密碼

備註:
-h,主庫主機,-p,主庫服務埠;
-U,複製使用者;
-F,p是預設輸出格式,輸出資料目錄和表空間相同的佈局,t表示tar格式輸出;
-P,同--progress,顯示進度;
-D,輸出到指定目錄;

2.3.2從庫修改配置檔案
[email protected]:~$vi ~/data/postgresql.conf
註釋掉以下內容
wal_level,
max_wal_senders 
wal_keep_segments等引數

開啟以下內容
hot_standby = on   #在備份的同時允許查詢
max_standby_streaming_delay = 30s#可選,流複製最大延遲
wal_receiver_status_interval = 10s#可選,從向主報告狀態的最大間隔時間
hot_standby_feedback = on#可選,查詢衝突時向主反饋
max_connections = 1000#預設引數,非主從配置相關引數,表示到資料庫的連線數,一般從庫做主要的讀服務時,設定值需要高於主庫

2.3.3配置~/data/pg_hba.conf
新增下面內容
hostreplication在資料庫裡建立的同步用的使用者名稱主庫IP地址或域名/32trust或md5
#在從庫中維護的主庫IP地址是為了以後切換使用

2.3.4建立恢復檔案recovery.conf
[email protected]:~$cp /usr/local/postgres/share/recovery.conf.sample ~/data/recovery.conf

修改檔案中的引數
[email protected]:~$vi ~/data/postgresql.conf
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=主庫地址10.10.10.205 port=5432 user=資料庫中建立的同步用的使用者名稱 password=密碼'

備註:
vim ~/data/recovery.conf    #在做基礎備份時,也可通過-R引數在備份結束後自動生產一個recovery.conf檔案
standby_mode = on  #指明從庫身份
primary_conninfo = 'host=10.10.10.205 port=5432 user=同步用的使用者名稱 password=密碼'  #連線到主庫資訊
recovery_target_timeline = 'latest'     #同步到最新資料
指定觸發檔案,檔案存在時,將觸發從庫提升為主庫,前提是必須設定”standby_mode = on”;如果不設定此引數,也可採用”pg_ctl promote“觸發從庫切換成主庫


#trigger_file = ‘/postgres/data/trigger_activestandby’
因為主庫採用的是md5認證,這裡需要密碼認證。

2.3.5啟動從庫資料服務
[email protected]:~$pg_ctl -D ~/data/ -l ~/log/pglog.log start

2.4 驗證主從配置
2.4.1檢視主庫sender程序
[email protected]:~$ps -ef|grep postgres
檢視有wal sender process 資料庫中建立的同步用的使用者名稱 從庫地址

2.4.2檢視從庫sender程序
[email protected]:~$ps -ef|grep postgres
檢視有wal receiver process

2.4.3用SQL看主從狀態
[email protected]:~$psql
postgres=#select * from pg_stat_replication;
主庫會顯示sync_state為async 地址是從庫地址
從庫查狀態無記錄

2.5 流複製資料同步測試
分別啟動master,slave資料庫
在master上建立一個數據庫和臨時表


[email protected]:~$ psql
psql (9.6.1)
Type "help" for help.
postgres=# \password  #建立資料庫密碼
#建立測試資料庫
postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create table tt(id serial not null,name text);
CREATE TABLE
test=# insert into tt(name) values ('china');
INSERT 0 1


在slave上查詢剛才建立的表和資料,判定是否有資料同步
[email protected]:~$ psql
psql (9.6.1)
Type "help" for help.


postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# select * from tt;
 id | name  
----+-------
  1 | china
(1 row)


2.6 主從切換方式


2.6.1主庫備庫狀態檢視


主庫
[email protected]:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               in production


備庫
[email protected]:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               in archive recovery


2.6.2停主庫服務
[email protected]:~$ pg_ctl stop -m fast
waiting for server to shut down.... done
server stopped
[email protected]:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               shut down


2.6.3啟用備庫
[email protected]:~$ pg_ctl promote
waiting for server to promote...... done
server promoted
[email protected]:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               in production


2.6.4測試在啟用的備庫中寫入資料
[email protected]:~$ psql
psql (10.3)
Type "help" for help.


postgres=# \d
        List of relations
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | test | table | postgres
(1 row)


postgres=# select * from test;
 id | name  | salary
----+-------+--------
  2 | LiShi |  12000
(1 row)


postgres=# insert into  test values (1,'Hallo',20000);
INSERT 0 1
postgres=# select * from test;
 id | name  | salary
----+-------+--------
  2 | LiShi |  12000
  1 | Hallo |  20000
(2 rows)


postgres=#\q
[email protected]:~$ ll data/recovery*
-rw-r--r-- 1 postgres postgres 5824 Apr 24 14:19 data/recovery.done
#原recovery.conf自動變成了recovery.done


2.6.5模擬此時原主庫已修復,需增加檔案recovery.conf
vi data/recovery.conf
standby_mode='on'
recovery_target_timeline = 'latest'
primary_conninfo='host=當前主庫(也就是以前從庫)的IP地址 port=5432 user=同步用的使用者名稱 password=密碼'
#例如:primary_conninfo='host=10.10.10.206 port=5432 user=repl password=repl1234'


#儲存後啟動原主庫,此時變為了現備庫的狀態
[email protected]:~$ pg_ctl -D data -l log/pglog.log start
waiting for server to start.... done
server started


#檢視剛啟動的原主庫狀態,已經變成備庫
[email protected]:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               in archive recovery


#檢視新備庫中資料是否與現主庫內容相同,9.6以後的版本應該會自動同步差異
[email protected]:~$ psql
psql (10.3)
Type "help" for help.


postgres=# \d
        List of relations
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | test | table | postgres
(1 row)


postgres=# select * from test;
 id | name  | salary
----+-------+--------
  2 | LiShi |  12000
  1 | Hallo |  20000
(2 rows)