日志传送 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会认识发生变化的文件,并同步变化的部分。
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx