暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PG12主备切换

原创 多米爸比 2019-12-27
3905

本文通过单机不同端口实例模拟异步流复制主备切换过程,先搭建主备,再做切换,最后再切换回来。
第一次 主(5555) 备(5556)
第二次 主(5556) 备(5555)
第三次 主(5555) 备(5556)
注意:主备的postgresql.conf文件不必非要一样,单机如果配置归档或者WAL路径一样的话会存在问题。

1. 初始化数据目录并启动主库

$ initdb -D data5555 -Upostgres -W

$ vi data5555/postgresql.conf
port=5555
wal_level=replica
archive_mode=on
archive_command='cp %p /opt/archive5555/%f'
max_wal_senders=10
wal_keep_segments=512
hot_standby=on
primary_conninfo='host=127.0.0.1 port=5556 user=repuser'
#restore_command = 'cp /opt/archive5555/%f %p'

$ pg_ctl -D data5555 start &

2. 插入数据

$ psql -p5555 -Upostgres
create user repuser replication login connection limit 5 encrypted password'123456';
create table t(id int,info text);
insert into t values(1,now());
insert into t values(2,now());

3. 基础备份恢复出备库

$ pg_basebackup -D /opt/data5556 -Fp -Xs -v -P -h127.0.0.1 -p5555 -Urepuser

4. 恢复备份

$ vi data5556/postgresql.conf
port=5556
wal_level=replica
archive_mode=on
archive_command = 'cp %p /opt/archive5556/%f'
max_wal_senders=10
wal_keep_segments=512
hot_standby=on
primary_conninfo='host=127.0.0.1 port=5555 user=repuser'
#restore_command = 'cp /opt/archive5556/%f %p'

$ touch data5556/standby.signal

$ pg_ctl -D data5556 start &

5. 查看主备进程

$ ps f -u postgres
  PID TTY      STAT   TIME COMMAND
 4450 pts/0    S+     0:00 -bash
 1639 pts/2    S      0:00 -bash
 4850 pts/2    R+     0:00  \_ ps f -u postgres
 1380 pts/1    S+     0:01 -bash
 4838 ?        Ss     0:00 /opt/pg12/bin/postgres -D data5556
 4839 ?        Ss     0:00  \_ postgres: startup   recovering 000000010000000000000005
 4840 ?        Ss     0:00  \_ postgres: checkpointer   
 4841 ?        Ss     0:00  \_ postgres: background writer   
 4842 ?        Ss     0:00  \_ postgres: stats collector   
 4843 ?        Ss     0:00  \_ postgres: walreceiver   streaming 0/5000060
 4713 ?        Ss     0:00 /opt/pg12/bin/postgres -D data5555
 4715 ?        Ss     0:00  \_ postgres: checkpointer   
 4716 ?        Ss     0:00  \_ postgres: background writer   
 4717 ?        Ss     0:00  \_ postgres: walwriter   
 4718 ?        Ss     0:00  \_ postgres: autovacuum launcher   
 4719 ?        Ss     0:00  \_ postgres: archiver   last was 000000010000000000000004.00000028.backup
 4720 ?        Ss     0:00  \_ postgres: stats collector   
 4721 ?        Ss     0:00  \_ postgres: logical replication launcher  
 4844 ?        Ss     0:00  \_ postgres: walsender repuser 127.0.0.1(32224) streaming 0/5000060

可以看到5555端口启动了walsender,5556启动了walreceiver

6. 主备验证

进入主库,插入数据
$ psql -p 5555 -Upostgres
psql (12.1)
Type "help" for help.

postgres=# insert into t values(3,now());
INSERT 0 1

进入备库,插入数据
[postgres@localhost opt]$ psql -p 5556 -Upostgres
psql (12.1)
Type "help" for help.

postgres=# insert into t values(100,now());
ERROR:  cannot execute INSERT in a read-only transaction
postgres=# select * from t;
 id |             info              
----+-------------------------------
  1 | 2019-12-27 16:14:41.991745+08
  2 | 2019-12-27 16:14:43.136463+08
  3 | 2019-12-27 16:19:45.787916+08
(3 rows)

可以看出主库可读写,备库只读,可查到主库传过来的数据。

7. 主备切换(5556切换为主库,5555切换为备库)

先关闭主库
$ pg_ctl -D data5555 stop

备库提升
$  pg_ctl promote -D data5556

触发主库进入备库模式
$ touch data5555/standby.signal

原主库启动
$ pg_ctl -D data5555 start

查看进程
$ ps f -u postgres
  PID TTY      STAT   TIME COMMAND
 4450 pts/0    S+     0:00 -bash
 1639 pts/2    S      0:00 -bash
 5159 pts/2    R+     0:00  \_ ps f -u postgres
 1380 pts/1    S+     0:01 -bash
 5152 ?        Ss     0:00 /opt/pg12/bin/postgres -D data5555
 5153 ?        Ss     0:00  \_ postgres: startup   recovering 000000020000000000000004
 5154 ?        Ss     0:00  \_ postgres: checkpointer   
 5155 ?        Ss     0:00  \_ postgres: background writer   
 5156 ?        Ss     0:00  \_ postgres: stats collector   
 5157 ?        Ss     0:00  \_ postgres: walreceiver   streaming 0/40001B8
 5133 ?        Ss     0:00 /opt/pg12/bin/postgres -D data5556
 5135 ?        Ss     0:00  \_ postgres: checkpointer   
 5136 ?        Ss     0:00  \_ postgres: background writer   
 5137 ?        Ss     0:00  \_ postgres: stats collector   
 5141 ?        Ss     0:00  \_ postgres: walwriter   
 5142 ?        Ss     0:00  \_ postgres: autovacuum launcher   
 5143 ?        Ss     0:00  \_ postgres: archiver   last was 000000010000000000000004.partial
 5144 ?        Ss     0:00  \_ postgres: logical replication launcher  
 5158 ?        Ss     0:00  \_ postgres: walsender repuser 127.0.0.1(20564) streaming 0/40001B8

可以看到5555端口启动了walreceiver,5556启动了walsender,与上面正好相反。

测试

原主库(现备库)做插入测试
$ psql -p 5555 -Upostgres
psql (12.1)
Type "help" for help.

postgres=# insert into t values(100,now());
ERROR:  cannot execute INSERT in a read-only transaction
postgres=# 

原备库(现主库)做插入测试
[postgres@localhost opt]$ psql -p 5556 -Upostgres
psql (12.1)
Type "help" for help.

postgres=# insert into t values(4,now());
INSERT 0 1

主备切换完成

8. 主备再次切换(5556切换为备库,5555切换为主库)

$ pg_ctl -D data5556 stop
$ pg_ctl promote -D data5555
$ touch data5556/standby.signal
$ pg_ctl -D data5556 start

查看进程

$ ps f -u postgres
  PID TTY      STAT   TIME COMMAND
 4450 pts/0    S+     0:00 -bash
 1639 pts/2    S      0:00 -bash
 5193 pts/2    R+     0:00  \_ ps f -u postgres
 1380 pts/1    S+     0:01 -bash
 5186 ?        Ss     0:00 /opt/pg12/bin/postgres -D data5556
 5187 ?        Ss     0:00  \_ postgres: startup   recovering 000000030000000000000005
 5188 ?        Ss     0:00  \_ postgres: checkpointer   
 5189 ?        Ss     0:00  \_ postgres: background writer   
 5190 ?        Ss     0:00  \_ postgres: stats collector   
 5191 ?        Ss     0:00  \_ postgres: walreceiver   streaming 0/50001B8
 5152 ?        Ss     0:00 /opt/pg12/bin/postgres -D data5555
 5154 ?        Ss     0:00  \_ postgres: checkpointer   
 5155 ?        Ss     0:00  \_ postgres: background writer   
 5156 ?        Ss     0:00  \_ postgres: stats collector   
 5177 ?        Ss     0:00  \_ postgres: walwriter   
 5178 ?        Ss     0:00  \_ postgres: autovacuum launcher   
 5179 ?        Ss     0:00  \_ postgres: archiver   last was 000000020000000000000005.partial
 5180 ?        Ss     0:00  \_ postgres: logical replication launcher  
 5192 ?        Ss     0:00  \_ postgres: walsender repuser 127.0.0.1(32326) streaming 0/50001B8

可以看到5555端口启动了walsender,5556启动了walreceiver。

测试

$ psql -p 5556 -Upostgres
psql (12.1)
Type "help" for help.

postgres=# insert into t values(5,now());
ERROR:  cannot execute INSERT in a read-only transaction
postgres=# 

$ psql -p 5555 -Upostgres
psql (12.1)
Type "help" for help.

postgres=# insert into t values(5,now());
INSERT 0 1

主备再次切换完成

最后修改时间:2022-10-23 09:07:00
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论