阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

InnoDB undo tablespace使用及原理详解

128次阅读
没有评论

共计 7052 个字符,预计需要花费 18 分钟才能阅读完成。

日志传送 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 会认识发生变化的文件,并同步变化的部分。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计7052字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中