共計 7772 個字符,預計需要花費 20 分鐘才能閱讀完成。
這篇文章主要介紹了如何使用 rsync 實現 postgres 日志傳送 standby 服務器,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。
日志傳送 standby 服務器
連續歸檔可以配合隨時準備取代失效主服務器的一個或多個備份服務器,用于創建一個高可用性 (HA) 集群。這個能力通常被稱為溫備份或日志傳送
從一個數據庫服務器移動 WAL 到另一個服務器通常被稱為日志傳送(LogShipping)。PostgreSQL 實現了基于文件的日志傳送,意思是 WAL 記錄每次移動一個完整的文件(WAL 段)。也可以基于記錄的日志傳送,
日志傳送是異步的,也就是 WAL 記錄在事務提交之后才被傳送,可以使用 archive_timeout 來設置日志傳送間隔時間(應該是最長間隔時間)
在啟動,standby 服務器調用 restore_command 命令開始恢復在 wal 歸檔位置有效的所有的 WAL,一旦恢復完可用 WAL,restore_command 就失敗,將嘗試從 pg_wal 目錄下恢復可用的 WAL。如果那也失敗了,并且已經配置了流復制,則嘗試連接到主服務器,從在歸檔或 pg_wal 中找到的最后一條有效的記錄開始 WAL 流復制。如果那也失敗了,或沒有配置流復制,或連接斷開,備服務器再次回到步驟 1,嘗試從歸檔里恢復文件。循環嘗試從歸檔、pg_wal、連續流復制通道,直到服務器停止或通過觸發器文件觸發失效切換。
實驗使用兩臺主機,都安裝 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 服務器創建之前歸檔 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 會認識發生變化的文件,并同步變化的部分。
感謝你能夠認真閱讀完這篇文章,希望丸趣 TV 小編分享的“如何使用 rsync 實現 postgres 日志傳送 standby 服務器”這篇文章對大家有幫助,同時也希望大家多多支持丸趣 TV,關注丸趣 TV 行業資訊頻道,更多相關知識等著你來學習!