1 摘要
使用PostgreSQL 11.3 創建兩個節點:node1 和 node2; 配置主從流複製,然後做手動切換(failover)。爲了配置過程簡單,兩個節點在同一臺物理機器上。
首先建立主從同步流複製,起初,node1作爲主節點(Primary),node2作爲從節點(Standby)。
接下來,模擬主節點(node1)無法工作,把從節點(node2)手動切換成主節點。然後把原來的主節點(node1)作爲從節點加入系統。 此時,node2是主 (Primary), node1是從(Standby)。
最後,模擬主節點(node2)無法工作,把從節點(node1)手動切換成主節點。然後把node2作爲從節點加入系統。 此時,node1是主 (Primary), node2是從(Standby)。
2 創建node1和node2,配置主從流複製
上級目錄是:
/Users/tom
兩個子目錄,testdb113,testdb113b, 分別屬於兩個節點, node1, node2:
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 standbyhot_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 ./testdb113bchmod -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 standbyhot_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 TABLEpostgres=# 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 transactionpostgres=#
3 模擬主節點node1不能工作時,把從節點node2提升到主節點
3.1 停止主節node1
node1停止後,只有node2工作,僅僅支持數據讀。焦作國醫胃腸醫院__優質服務:http://jz.lieju.com/zhuankeyiyuan/37572153.htm
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),既能讀,也能寫。焦作國醫胃腸醫院胃鏡檢查多少錢_平價收費 百姓放心:http://jz.lieju.com/zhuankeyiyuan/37571889.htm
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 1postgres=#
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' 。