天天看點

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.205 NanoPI-005

10.10.10.206 NanoPI-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

添加下面内容

host replication 在資料庫裡建立的同步用的使用者名 備庫IP位址或域名/32 trust

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

添加下面内容

host replication 在資料庫裡建立的同步用的使用者名 主庫IP位址或域名/32 trust或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)

繼續閱讀