PostgreSQL-11.3-主從流複製+手動主備切換

1 摘要

使用PostgreSQL 11.3 建立兩個節點:node1 和 node2; 配置主從流複製,而後作手動切換(failover)。爲了配置過程簡單,兩個節點在同一臺物理機器上。node

首先創建主從同步流複製,起初,node1做爲主節點(Primary),node2做爲從節點(Standby)。sql

接下來,模擬主節點(node1)沒法工做,把從節點(node2)手動切換成主節點。而後把原來的主節點(node1)做爲從節點加入系統。 此時,node2是主 (Primary), node1是從(Standby)。數據庫

最後,模擬主節點(node2)沒法工做,把從節點(node1)手動切換成主節點。而後把node2做爲從節點加入系統。 此時,node1是主 (Primary), node2是從(Standby)。bash

2 建立node1和node2,配置主從流複製

上級目錄是:app

/Users/tom

兩個子目錄,testdb113,testdb113b, 分別屬於兩個節點, node1, node2:dom

testdb113  --> node1
 testdb113b --> node2

2.1 建立主節點(Primary) node1

mkdir testdb113
initdb -D ./testdb113
pg_ctl -D ./testdb113 start

2.1.1 node1: 建立數據庫帳號,用於流複製

  • 建立帳號 'replicauser',該帳號存在於node1和node2 。
CREATE ROLE replicauser WITH REPLICATION LOGIN ENCRYPTED PASSWORD 'abcdtest';

2.1.2 node1: 建立歸檔目錄

mkdir ./testdb113/archive

2.1.3 node1: 在配置文件中(postgresql.conf)設定參數

#for the primary 
wal_level = replica 
synchronous_commit = remote_apply 
archive_mode = on 
archive_command = 'cp %p /Users/tom/testdb113/archive/%f'
max_wal_senders =  10 
wal_keep_segments = 10 
synchronous_standby_names = 'pgslave001'

#for the standby
hot_standby = on

2.1.4 node1: 編輯文件pg_hba.conf,設置合法地址:

  • add following to then end of pg_hba.conf
host     replication    replicauser          127.0.0.1/32            md5
host     replication    replicauser          ::1/128                 md5

2.1.5 node1: 預先編輯文件:recovery.done

  • 注意: 該文件後來纔會用到。當node1成爲從節點時候,須要把recovery.done重命名爲recovery.conf
standby_mode = 'on'
recovery_target_timeline = 'latest'
primary_conninfo = 'host=localhost port=6432 user=replicauser password=abcdtest application_name=pgslave001'
restore_command = 'cp /Users/tom/testdb113b/archive/%f %p'
trigger_file = '/tmp/postgresql.trigger.5432'

2.2 建立和配置從節點(node2)

mkdir testdb113b
pg_basebackup  -D ./testdb113b
chmod -R 700 ./testdb113b

2.2.1 node2: 編輯文件postgresql.conf,設定參數

  • 這個文件來自node1(因爲使用了pg_basebackup),只須要更改其中的個別參數。
#for the primary 
port = 6432 
wal_level = replica 
synchronous_commit = remote_apply 
archive_mode = on 
archive_command = 'cp %p /Users/tom/testdb113b/archive/%f'
max_wal_senders =  2 
wal_keep_segments = 10 
synchronous_standby_names = 'pgslave001'


#for the standby
hot_standby = on

2.2.2 node2: 創建歸檔目錄

  • 確保歸檔目錄存在
mkdir ./testdb113b/archive

2.2.3 node2: 檢查/編輯文件pg_hba.conf

  • 這個文件來自node1,不須要編輯

2.2.4 node2: 建立/編輯文件recovery.conf

  • PG啓動時,若是文件recovery.conf存在,則工做在recovery模式,看成從節點。若是當前節點升級成主節點,文件recovery.conf將會被重命名爲recovery.done。
standby_mode = 'on'
recovery_target_timeline = 'latest'
primary_conninfo = 'host=localhost port=5432 user=replicauser password=abcdtest application_name=pgslave001'
restore_command = 'cp /Users/tom/testdb113/archive/%f %p'
trigger_file = '/tmp/postgresql.trigger.6432'

2.3 簡單測試

2.3.1 從新啓動主節點(node1):

pg_ctl -D ./testdb113 restart

2.3.2 啓動從節點(node2)

pg_ctl -D ./testdb113b start

2.3.3 在node1上作數據更改

  • node1支持數據的寫和讀
psql -d postgres
postgres=# create table test ( id int, name varchar(100));
CREATE TABLE
postgres=# insert into test values(1,'1');
INSERT 0 1

2.3.4 節點node2讀數據

  • node2是從節點(Standby),只能讀,不能寫。
psql -d postgres -p 6432
postgres=# select * from test;
 id | name 
----+------
  1 | 1
(1 row)

postgres=# insert into test values(1,'1');
ERROR:  cannot execute INSERT in a read-only transaction
postgres=#

3 模擬主節點node1不能工做時,把從節點node2提高到主節點

3.1 中止主節node1

  • node1中止後,只有node2工做,僅僅支持數據讀。
pg_ctl -D ./testdb113 stop

3.1.1 嘗試鏈接到節點node1時失敗

  • 由於node1中止了。
Ruis-MacBook-Air:tom$ psql -d postgres
psql: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

3.1.2 嘗試鏈接到node2,成功;插入數據到node2,失敗

  • node2在工做,可是隻讀
Ruis-MacBook-Air:~ tom$ psql -d postgres -p 6432
psql (11.3)
Type "help" for help.

postgres=# insert into test values(1,'1');
ERROR:  cannot execute INSERT in a read-only transaction
postgres=#

3.2 node2: 把node2提高到主

  • 提高後,node2成爲了主(Primary),既能讀,也能寫。
touch /tmp/postgresql.trigger.6432

3.3 node2: 插入數據到node2,阻塞等待

  • node2是主節點(Primary),可以成功插入數據。可是因爲設置了同步流複製,node2必須等待某個從節點把數據更改應用到從節點,而後返回相應的LSN到主節點。
postgres=# insert into test values(2,'2');

3.4 node1: 建立文件recovery.conf

  • 使用以前創建的文件recovery.done做爲模版,快速建立recovery.conf。
mv recovery.done recovery.conf

3.5 node1: 啓動node1,做爲從節點(Standby)

  • 啓動後,系統中又有兩個節點:node2是主,node1是從。
pg_ctl -D ./testdb113 start

3.6 插入數據到node2(見3.3)成功返回

  • 因爲節點node1做爲從節點加入系統,應用主節點的數據更改,而後返回相應WAL的LSN到主節點,使主節點等到了迴應,從而事務處理得以繼續。
postgres=# insert into test values(2,'2');
INSERT 0 1
postgres=#

3.7 分別在node1和node2讀取數據

  • 因爲兩個節點都只是數據讀,並且流複製正常工做,全部讀取到相同的結果
postgres=# select * from test;
 id | name 
----+------
  1 | 1
  2 | 2
(2 rows)

4 模擬node2沒法工做時,提高node1成爲主(Primary)

4.1 中止node2

pg_ctl -D ./testdb113b stop

4.1.1 嘗試訪問node2,失敗

  • 由於node2已經中止了.
Ruis-MacBook-Air:~ tom$ psql -d postgres -p 6432
psql: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

4.1.2 node1: 鏈接到node1,成功;插入數據到node1,失敗

  • node1只讀
Ruis-MacBook-Air:~ tom$ psql -d postgres
psql (11.3)
Type "help" for help.

postgres=# insert into test values(3,'3');
ERROR:  cannot execute INSERT in a read-only transaction
postgres=#

4.2 node1: 提高node1,成爲主(Primary)

touch /tmp/postgresql.trigger.5432

4.3 node1: 插入數據到node1,阻塞等待

  • node1成功插入數據,可是要等待從節點的 remote-apply.
postgres=# insert into test values(3,'3');

4.4 node2: 建立文件recovery.conf

mv recovery.done recovery.conf

4.5 node2: 啓動node2,做爲從節點(Standby)

pg_ctl -D ./testdb113b start

4.6 node1: 插入數據(見4.3)成功返回。

postgres=# insert into test values(3,'3');

INSERT 0 1

4.7 分別在node1和node2讀取數據

  • 因爲兩個節點都只是數據讀,並且流複製正常工做,全部讀取到相同的結果
postgres=# select * from test;
 id | name 
----+------
  1 | 1
  2 | 2
  3 | 3
(3 rows)

5 自動化解決方案

當主節點不能工做時,須要把一個從節點提高成爲主節點。 若是是手工方式提高,可使用trigger文件,也可使用命令'pg_ctl promote' 。socket

目前也有不少種自動化監控和切換的方案,在網上都能搜索到。或者,能夠本身動手寫一個自動化監控和切換的方案。post

相關文章
相關標籤/搜索