日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

postgresql主从备份_PostgreSQL主从流复制与手动主备切换架构

發布時間:2024/4/13 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 postgresql主从备份_PostgreSQL主从流复制与手动主备切换架构 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

導讀

使用PostgreSQL 11.3 創建兩個節點:node1 和 node2; 配置主從流復制,然后做手動切換(failover)。為了配置過程簡單,兩個節點在同一臺物理機器上。

首先建立主從同步流復制,起初,node1作為主節點(Primary),node2作為從節點(Standby)。

接下來,模擬主節點(node1)無法工作,把從節點(node2)手動切換成主節點。然后把原來的主節點(node1)作為從節點加入系統。 此時,node2是主 (Primary), node1是從(Standby)。

最后,模擬主節點(node2)無法工作,把從節點(node1)手動切換成主節點。然后把node2作為從節點加入系統。 此時,node1是主 (Primary), node2是從(Standby)。

創建node1和node2,配置主從流復制

上級目錄是:

/Users/tom

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

testdb113 --> node1

testdb113b --> node2

創建主節點(Primary) node1

mkdir testdb113

initdb -D ./testdb113

pg_ctl -D ./testdb113 start

node1: 創建數據庫賬號,用于流復制,創建賬號 'replicauser',該賬號存在于node1和node2 。

CREATE ROLE replicauser WITH REPLICATION LOGIN ENCRYPTED PASSWORD 'abcdtest';

node1: 創建歸檔目錄

mkdir ./testdb113/archive

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

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\

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'

創建和配置從節點(node2)

mkdir testdb113b

pg_basebackup -D ./testdb113b

chmod -R 700 ./testdb113b

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

node2: 建立歸檔目錄,確保歸檔目錄存在

mkdir ./testdb113b/archive

node2: 檢查/編輯文件pg_hba.conf,這個文件來自node1,不需要編輯

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'

簡單測試

重新啟動主節點(node1):

pg_ctl -D ./testdb113 restart

啟動從節點(node2)

pg_ctl -D ./testdb113b start

在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

節點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=#

模擬主節點node1不能工作時,把從節點node2提升到主節點

停止主節node1,node1停止后,只有node2工作,僅僅支持數據讀。

pg_ctl -D ./testdb113 stop

嘗試連接到節點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"?

嘗試連接到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=#

node2: 把node2提升到主

提升后,node2成為了主(Primary),既能讀,也能寫。

touch /tmp/postgresql.trigger.6432

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

node2是主節點(Primary),能夠成功插入數據。但是由于設置了同步流復制,node2必須等待某個從節點把數據更改應用到從節點,然后返回相應的LSN到主節點。

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

node1: 創建文件recovery.conf

使用之前建立的文件recovery.done作為模版,快速創建recovery.conf。

mv recovery.done recovery.conf

node1: 啟動node1,作為從節點(Standby)

啟動后,系統中又有兩個節點:node2是主,node1是從。

pg_ctl -D ./testdb113 start

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

由于節點node1作為從節點加入系統,應用主節點的數據更改,然后返回相應WAL的LSN到主節點,使主節點等到了回應,從而事務處理得以繼續。

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

INSERT 0 1

postgres=#

分別在node1和node2讀取數據

由于兩個節點都只是數據讀,而且流復制正常工作,所有讀取到相同的結果

ostgres=# select * from test;

id | name

----+------

1 | 1

2 | 2

(2 rows)

模擬node2無法工作時,提升node1成為主(Primary)

停止node2

pg_ctl -D ./testdb113b stop

嘗試訪問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"?

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=#

node1: 提升node1,成為主(Primary)

touch /tmp/postgresql.trigger.5432

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

node1成功插入數據,但是要等待從節點的 remote-apply.

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

node2: 創建文件recovery.conf

mv recovery.done recovery.conf

node2: 啟動node2,作為從節點(Standby)

pg_ctl -D ./testdb113b start

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

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

INSERT 0 1

分別在node1和node2讀取數據

由于兩個節點都只是數據讀,而且流復制正常工作,所有讀取到相同的結果

postgres=# select * from test;

id | name

----+------

1 | 1

2 | 2

3 | 3

(3 rows)

自動化解決方案

當主節點不能工作時,需要把一個從節點提升成為主節點。 如果是手工方式提升,可以使用trigger文件,也可以使用命令'pg_ctl promote' 。

目前也有很多種自動化監控和切換的方案,在網上都能搜索到。或者,可以自己動手寫一個自動化監控和切換的方案。

總結

以上是生活随笔為你收集整理的postgresql主从备份_PostgreSQL主从流复制与手动主备切换架构的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。