如何使用rsync實現postgres日志傳送standby服務器-創(chuàng)新互聯(lián)

這篇文章主要介紹了如何使用rsync實現postgres日志傳送standby服務器,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供日土網站建設、日土做網站、日土網站設計、日土網站制作等企業(yè)網站建設、網頁設計與制作、日土企業(yè)網站模板建站服務,十多年日土做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。

日志傳送standby服務器

    連續(xù)歸檔可以配合隨時準備取代失效主服務器的一個或多個備份服務器, 用于創(chuàng)建一個高可用性(HA)集群。這個能力通常被稱為溫備份或日志傳送

    從一個數據庫服務器移動 WAL 到另一個服務器通常被稱為日志傳送(LogShipping)。PostgreSQL 實現了基于文件的日志傳送,意思是 WAL 記錄每次移動一個完整的文件(WAL 段)。 也可以基于記錄的日志傳送,

    日志傳送是異步的,也就是 WAL 記錄在事務提交之后才被傳送,可以使用 archive_timeout 來設置日志傳送間隔時間(應該是最長間隔時間)

    在啟動,standby 服務器調用 restore_command 命令開始恢復在 wal 歸檔位置有效的所有的 WAL, 一旦恢復完可用 WAL,restore_command 就失敗, 將嘗試從 pg_wal 目錄下恢復可用的 WAL。 如果那也失敗了,并且已經配置了流復制,則嘗試連接到主服務器, 從在歸檔或 pg_wal 中找到的最后一條有效的記錄開始 WAL 流復制。如果那也失敗了, 或沒有配置流復制,或連接斷開,備服務器再次回到步驟 1,嘗試從歸檔里恢復文件。 循環(huán)嘗試從歸檔、pg_wal、連續(xù)流復制通道,直到服務器停止或通過觸發(fā)器文件觸發(fā)失效切換。

實驗使用兩臺主機,都安裝postgresql-10.7,已配置流復制

  • 主庫:192.168.56.25 m1  使用rsync命令傳送wal文件到m7

  • 叢庫:192.168.56.5 m7  在此機上配置rsync做為服務運行,接收叢m1傳送過來的wal文件

叢庫的配置,設置hot_standby使standby庫接收連接。省略standby庫從主庫基礎備份還原過程

[postgres@localhost data]$ cat recovery.conf
standby_mode = 'on'
restore_command = 'cp /usr/local/pg/arch_bak/%f %p'
recovery_target_timeline = 'latest'
postgres=# show  hot_standby;
 hot_standby
-------------
 on
(1 row)

m1上配置rsync服務器,使用rsync-3.1.3版本,省略rsyn安裝過程

[root@localhost ~]# cat /etc/rsyncd.conf 
uid=postgres
gid=postgres
use chroot = no
max connections = 5
read only = false   
pid file = /var/run/rsyncd.pid
log file = /var/log/rsync.log
transfer logging = yes
log format = %t %a %m %f %b
timeout = 300
[arch_bak]
path = /usr/local/pg/arch_bak
auth users = tridge, susan
secrets file = /etc/rsyncd.secrets
[wal_bak]
path = /usr/local/pg/data/pg_wal
auth users = tridge, susan
secrets file = /etc/rsyncd.secrets
[root@localhost ~]# cat /etc/rsyncd.secrets
tridge:mypass
susan:herpass

啟動rsync服務器

[root@localhost arch_bak]# /usr/local/bin/rsync --daemon
[root@localhost arch_bak]# ll
total 0
[root@localhost arch_bak]# pwd
/usr/local/pg/arch_bak

從m7同步standby服務器創(chuàng)建之前歸檔wal日志文件

[root@z_leader rsync-3.1.3]# /usr/local/bin/rsync  -av /usr/local/pg/arch/ tridge@192.168.56.5::arch_bak/
Password:
sending incremental file list
./
00000005000000000000000B
00000005000000000000000C.partial
00000006.history
00000006000000000000000C
00000006000000000000000D
00000006000000000000000E
00000006000000000000000F
000000060000000000000010
sent 117,470,037 bytes  received 179 bytes  18,072,340.92 bytes/sec
total size is 117,440,721  speedup is 1.00

在m1上查看傳輸過的文件,及standby服務器表記錄

[root@localhost arch_bak]# ll
total 114692
-rw------- 1 postgres postgres 16777216 Mar  2 09:02 00000005000000000000000B
-rw------- 1 postgres postgres 16777216 Mar  2 09:08 00000005000000000000000C.partial
-rw------- 1 postgres postgres 16777216 Mar  2 11:15 00000006000000000000000C
-rw------- 1 postgres postgres 16777216 Mar 17 09:51 00000006000000000000000D
-rw------- 1 postgres postgres 16777216 Mar 17 09:55 00000006000000000000000E
-rw------- 1 postgres postgres 16777216 Mar 17 10:13 00000006000000000000000F
-rw------- 1 postgres postgres 16777216 Mar 17 20:02 000000060000000000000010
-rw------- 1 postgres postgres      209 Mar  2 09:08 00000006.history
postgres=# select * from test;
 id | e_name |   e_mail    | d_id
----+--------+-------------+------
  1 | zbs    | 123@126.com |   10
  3 | zbs2   | 124@126.com |   10
  4 | zbs2   | 124@126.com |   10
  2 | zbs1   | 124@126.com |   10
  5 | zbs2   | 124@126.com |   10
  6 | zbs2   | 124@126.com |   10
  7 | zbs2   | 124@126.com |   10
  8 | zbs2   | 124@126.com |   10
(8 rows)

在主庫上插入2兩條記錄,并歸檔

postgres=# select * from test;
 id | e_name |   e_mail    | d_id
----+--------+-------------+------
  1 | zbs    | 123@126.com |   10
  3 | zbs2   | 124@126.com |   10
  4 | zbs2   | 124@126.com |   10
  2 | zbs1   | 124@126.com |   10
  5 | zbs2   | 124@126.com |   10
  6 | zbs2   | 124@126.com |   10
  7 | zbs2   | 124@126.com |   10
  8 | zbs2   | 124@126.com |   10
(8 rows)
postgres=# insert into test values(9,'zbs3','124@126.com',20);
INSERT 0 1
postgres=# insert into test values(10,'zbs3','124@126.com',20);
INSERT 0 1
postgres=# select pg_switch_wal();
 pg_switch_wal
---------------
 0/11000888
(1 row)

在m7上使用rsync命令同步新生成的歸檔

[root@z_leader rsync-3.1.3]# /usr/local/bin/rsync  -av /usr/local/pg/arch/ tridge@192.168.56.5::arch_bak/
Password:
sending incremental file list
./
000000060000000000000011
sent 16,781,696 bytes  received 46 bytes  4,794,783.43 bytes/sec
total size is 134,217,937  speedup is 8.00

在m1上查看是表數據是否同步,在主庫插入的2條記錄成功應用到從庫

postgres=# select * from test;
 id | e_name |   e_mail    | d_id
----+--------+-------------+------
  1 | zbs    | 123@126.com |   10
  3 | zbs2   | 124@126.com |   10
  4 | zbs2   | 124@126.com |   10
  2 | zbs1   | 124@126.com |   10
  5 | zbs2   | 124@126.com |   10
  6 | zbs2   | 124@126.com |   10
  7 | zbs2   | 124@126.com |   10
  8 | zbs2   | 124@126.com |   10
  9 | zbs3   | 124@126.com |   20
 10 | zbs3   | 124@126.com |   20
(10 rows)

到此,使用rsyn傳輸wal歸檔文件到standby服務器使用成功,rsync只輸出了新的文件000000060000000000000011

下面實驗直接同步wal日志文件

m1目前wal目錄

[postgres@localhost pg_wal]$ pwd
/usr/local/pg/data/pg_wal
[postgres@localhost pg_wal]$ ll
total 81948
-rw------- 1 postgres postgres       41 Mar  2 09:24 00000002.history
-rw------- 1 postgres postgres       83 Mar  2 09:24 00000003.history
-rw------- 1 postgres postgres      302 Mar  2 09:24 000000040000000000000009.00000028.backup
-rw------- 1 postgres postgres      125 Mar  2 09:24 00000004.history
-rw------- 1 postgres postgres      167 Mar  2 09:24 00000005.history
-rw------- 1 postgres postgres 16777216 Mar 23 09:38 000000060000000000000010
-rw------- 1 postgres postgres 16777216 Mar 23 09:46 000000060000000000000011
-rw------- 1 postgres postgres 16777216 Mar  2 09:24 000000060000000000000012
-rw------- 1 postgres postgres 16777216 Mar 17 10:05 000000060000000000000013
-rw------- 1 postgres postgres 16777216 Mar 17 10:15 000000060000000000000014
-rw------- 1 postgres postgres      209 Mar  2 09:24 00000006.history
drwx------ 2 postgres postgres     4096 Mar 23 09:46 archive_status

在m7上查看wal目錄,并輸出不同的文件

[root@z_leader pg_wal]# /usr/local/bin/rsync  -av /usr/local/pg/data/pg_wal/ tridge@192.168.56.5::wal_bak/
Password:
sending incremental file list
./
00000002.history
00000003.history
00000004.history
000000040000000000000009.00000028.backup
00000005.history
00000006.history
000000060000000000000011
000000060000000000000012
000000060000000000000013
000000060000000000000014
000000060000000000000015
archive_status/
archive_status/00000002.history.done
archive_status/00000004.history.done
archive_status/000000040000000000000009.00000028.backup.done
archive_status/00000006.history.done
archive_status/000000060000000000000011.done
sent 33,626,454 bytes  received 98,679 bytes  3,967,662.71 bytes/sec
total size is 83,887,007  speedup is 2.49

現在在主庫上再插入兩條記錄

postgres=# insert into test values(11,'zbs3','124@126.com',20);
INSERT 0 1
postgres=# insert into test values(12,'zbs3','124@126.com',20);
INSERT 0 1
postgres=# select * from test;
 id | e_name |   e_mail    | d_id
----+--------+-------------+------
  1 | zbs    | 123@126.com |   10
  3 | zbs2   | 124@126.com |   10
  4 | zbs2   | 124@126.com |   10
  2 | zbs1   | 124@126.com |   10
  5 | zbs2   | 124@126.com |   10
  6 | zbs2   | 124@126.com |   10
  7 | zbs2   | 124@126.com |   10
  8 | zbs2   | 124@126.com |   10
  9 | zbs3   | 124@126.com |   20
 10 | zbs3   | 124@126.com |   20
 11 | zbs3   | 124@126.com |   20
 12 | zbs3   | 124@126.com |   20
(12 rows)

在m7同步wal日志文件到m1

[root@z_leader pg_wal]# /usr/local/bin/rsync  -av /usr/local/pg/data/pg_wal/ tridge@192.168.56.5::wal_bak/
Password:
sending incremental file list
./
000000060000000000000012
000000060000000000000016
archive_status/
sent 16,802,460 bytes  received 24,649 bytes  3,739,357.56 bytes/sec
total size is 83,887,007  speedup is 4.99

在m1上查看是表數據是否同步,在主庫插入的2條記錄成功應用到從庫

postgres=#  select * from test;
 id | e_name |   e_mail    | d_id
----+--------+-------------+------
  1 | zbs    | 123@126.com |   10
  3 | zbs2   | 124@126.com |   10
  4 | zbs2   | 124@126.com |   10
  2 | zbs1   | 124@126.com |   10
  5 | zbs2   | 124@126.com |   10
  6 | zbs2   | 124@126.com |   10
  7 | zbs2   | 124@126.com |   10
  8 | zbs2   | 124@126.com |   10
  9 | zbs3   | 124@126.com |   20
 10 | zbs3   | 124@126.com |   20
 11 | zbs3   | 124@126.com |   20
 12 | zbs3   | 124@126.com |   20
(12 rows)

到此,使用rsyn傳輸wal文件到standby服務器使用成功,rsync會認識發(fā)生變化的文件,并同步變化的部分。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何使用rsync實現postgres日志傳送standby服務器”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)-成都網站建設公司行業(yè)資訊頻道,更多相關知識等著你來學習!

當前題目:如何使用rsync實現postgres日志傳送standby服務器-創(chuàng)新互聯(lián)
網頁地址:http://muchs.cn/article30/dspjso.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網頁設計公司網站收錄、用戶體驗、定制網站、定制開發(fā)App開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網頁設計公司