postgresql双机热备(流复制)及主备切换

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: postgresql双机热备(流复制)及主备切换

一、环境

ip:192.168.11.68(主)

192.168.11.70(备)

数据库版本:postgresql9.4.4


二、双机热备的过程

两台系统之中安装postgresql (过程这里就不写了)

1.配置主节点

[root@node1 ~]# su - postgres

[postgres@node1 ~]#cd /opt/pgsql/data

[postgres@node1 data]#vi postgresql.conf

--------------------------------------------------修改以下参数:

#wal_level = minimal

#max_wal_senders = 0

//wal 发送进程数,有几个备库就设置为几

#wal_keep_segments = 0

//WAL日志文件的大小,设置一个足够大的值,以防主库生成WAL日志太快,日志还没有来得及传送到standby,就会循环覆盖

#hot_standby = off

//开启日志记录功能

#logging_collector = off

#log_directory = 'pg_log' //日志目录

#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' //日志名


--------------------------------------------------最终修改为:

wal_level = hot_standby

max_wal_senders = 1

wal_keep_segments = 500

hot_standby = on

logging_collector = on

log_directory = 'pg_log'

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'


[postgres@node1 data]$ psql

Password:

psql (postgresql 9.4.4)

Type "help" for help.


postgres=# create user standby SUPERUSER LOGIN password 'standby';

CREATE ROLE


//配置pg_hba.conf,添加如下:

# IPv4 local connections:

host all all 127.0.0.1/32 md5

host replication standby 192.168.0.0/24 md5


重启数据库,使参数生效

[postgres@node1 data]# pg_ctl restart -m fast


2.进行基础备份


//开启备份功能

[postgres@node1 data]$psql -c "select pg_start_backup('backup0001')"

//将data目录下的数据远程拷贝到备库的data目录下

[postgres@node1 data]$scp -r /opt/pgsql/data/* 192.168.11.70:/opt/pgsql/data/

//关闭备份功能

[postgres@node1 data]$psql -c "select pg_stop_backup()"

注意: 没有启用WAL归档;您必须确保所有的WAL段已通过其他的方法拷贝,这样才能完成备份

pg_stop_backup

----------------

0/110000E0

(1 row)


3.配置备节点

//删除data目录下的postmaster.pid文件

[postgres@node2 ~]$ cd /opt/pgsql/data

[postgres@node2 data]$ rm -rf postmaster.pid

//在data目录下新建recovery.conf文件

[postgres@node2 data]$ vi recovery.conf

standby_mode='on'

recovery_target_timeline = 'latest'

primary_conninfo='host=192.168.11.70 port=5866 user=standby password=standby'

trigger_file='opt/pgsql/trigger_node'


启动数据库

[postgres@node2 data]$ pg_ctl start


在主节点上创建表测试



[postgrs@node1 ~]$ psql

psql (9.4.4)

Type "help" for help.


postgres=# create table a (a int);

CREATE TABLLE

postgres=# insert into a values (123);

INSERT 0 1

postgres=#select * from a ;

a

-----

123

(1 rows)


在备节点上查看数据是否同步


[postgrs@node2 ~]$ psql

psql (9.4.4)

Type "help" for help.

List of relations

Schema | Name | Type | Owner

----------------+------+-------+--------

public | a | table | postgres

postgres=#select * from a ;

a

-----

123

(1 rows)


数据同步,流复制配置成功!


三、主备切换

首先要把系统的网卡添加一个eth0:1的网络

方法:

[root@node1 network-scripts]# cd /etc/sysconfig/network-scripts/

[root@node1 network-scripts]# ls

ifcfg-eth0 ifdown ifdown-ippp ifdown-post ifdown-sit ifup-aliases ifup-ippp ifup-plip ifup-ppp ifup-tunnel net.hotplug

ifdown-bnep ifdown-ipv6 ifdown-ppp ifdown-tunnel ifup-bnep ifup-ipv6 ifup-plusb ifup-routes ifup-wireless network-functions

ifcfg-lo ifdown-eth ifdown-isdn ifdown-routes ifup ifup-eth ifup-isdn ifup-post ifup-sit init.ipv6-global network-functions-ipv6

[root@node1 network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1

[root@node1 network-scripts]# vi ifcfg-eth0:1


DEVICE=eth0:1 #修改名字

HWADDR=00:0c:29:a2:99:01

TYPE=Ethernet

UUID=2f2ab65e-e86d-4897-8fcd-0601f6fd7f46

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=none

IPADDR=192.168.11.40 #选择一个ip地址

NETMASK=255.255.255.0

GATEWAY=192.168.11.0

IPV6INIT=no

USERCTL=no


重启网络服务

[root@node1 network-scripts]# service network restart

Shutting down interface eth0: Device state: 3 (disconnected)

[ OK ]

Shutting down loopback interface: [ OK ]

Bringing up loopback interface: [ OK ]

Bringing up interface eth0: Active connection state: activated

Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/3

[ OK ]

[root@node1 network-scripts]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:A2:99:01

inet addr:192.168.11.68 Bcast:192.168.11.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fea2:9901/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:141173 errors:0 dropped:0 overruns:0 frame:0

TX packets:29124 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:198747523 (189.5 MiB) TX bytes:2168654 (2.0 MiB)


eth0:1 Link encap:Ethernet HWaddr 00:0C:29:A2:99:01

inet addr:192.168.11.40 Bcast:192.168.11.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1


lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:636 errors:0 dropped:0 overruns:0 frame:0

TX packets:636 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:139280 (136.0 KiB) TX bytes:139280 (136.0 KiB)

备库做同样的操作,完成后将eth0:1关闭

[root@node2 ~]# ifdown eth0:1

[root@node2 network-scripts]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:A2:99:01

inet addr:192.168.11.70 Bcast:192.168.11.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fea2:9901/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:141173 errors:0 dropped:0 overruns:0 frame:0

TX packets:29124 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:198747523 (189.5 MiB) TX bytes:2168654 (2.0 MiB)

lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:636 errors:0 dropped:0 overruns:0 frame:0

TX packets:636 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:139280 (136.0 KiB) TX bytes:139280 (136.0 KiB)


利用ip地址漂移实现主备切换

切换前观察ip地址情况和数据库的主备情况

ip地址:主端

[root@node1 ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:EA:AF:6E

inet addr:192.168.11.68 Bcast:192.168.11.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:feea:af6e/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:29953 errors:0 dropped:0 overruns:0 frame:0

TX packets:141299 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:2174739 (2.0 MiB) TX bytes:198989432 (189.7 MiB)


eth0:1 Link encap:Ethernet HWaddr 00:0C:29:EA:AF:6E

inet addr:192.168.11.40 Bcast:192.168.11.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1


lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:308 errors:0 dropped:0 overruns:0 frame:0

TX packets:308 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:55374 (54.0 KiB) TX bytes:55374 (54.0 KiB)

备端

[root@node2 ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:EA:AF:6E

inet addr:192.168.11.70 Bcast:192.168.11.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:feea:af6e/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:29953 errors:0 dropped:0 overruns:0 frame:0

TX packets:141299 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:2174739 (2.0 MiB) TX bytes:198989432 (189.7 MiB)


lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:308 errors:0 dropped:0 overruns:0 frame:0

TX packets:308 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:55374 (54.0 KiB) TX bytes:55374 (54.0 KiB)


数据库情况

(./pg_controldata /postgresql/pgsql_data/   #查看数据库状态 ,需要加数据库目录)

主端

[postgres@node1 ~]$ pg_controldata

pg_control version number: 922

Catalog version number: 201204301

Database system identifier: 6377220794669856238

Database cluster state:in production

pg_control last modified: Mon 23 Jan 2017 09:13:29 AM CST

Latest checkpoint location: 0/12015CD0

Prior checkpoint location: 0/12015C70

Latest checkpoint's REDO location: 0/12015CD0

Latest checkpoint's TimeLineID: 2

Latest checkpoint's full_page_writes: on

Latest checkpoint's NextXID: 0/1822

Latest checkpoint's NextOID: 16397

Latest checkpoint's NextMultiXactId: 1

Latest checkpoint's NextMultiOffset: 0

Latest checkpoint's oldestXID: 1802

Latest checkpoint's oldestXID's DB: 1

Latest checkpoint's oldestActiveXID: 0

Time of latest checkpoint: Mon 23 Jan 2017 09:11:41 AM CST

Minimum recovery ending location: 0/0

Backup start location: 0/0

Backup end location: 0/0

End-of-backup record required: no

Current wal_level setting: hot_standby

Current max_connections setting: 100

Current max_prepared_xacts setting: 0

Current max_locks_per_xact setting: 64

Maximum data alignment: 8

Database block size: 8192

Blocks per segment of large relation: 131072

WAL block size: 8192

Bytes per WAL segment: 16777216

Maximum length of identifiers: 64

Maximum columns in an index: 32

Maximum size of a TOAST chunk: 1996

Date/time type storage: 64-bit integers

Float4 argument passing: by value

Float8 argument passing: by value


[postgres@node2 ~]$ pg_controldata

pg_control version number: 922

Catalog version number: 201204301

Database system identifier: 6377220794669856238

Database cluster state:in archive recovery

pg_control last modified: Mon 23 Jan 2017 09:13:09 AM CST

Latest checkpoint location: 0/12015D30

Prior checkpoint location: 0/12015C10

Latest checkpoint's REDO location: 0/12015D30

Latest checkpoint's TimeLineID: 3

Latest checkpoint's full_page_writes: on

Latest checkpoint's NextXID: 0/1822

Latest checkpoint's NextOID: 16397

Latest checkpoint's NextMultiXactId: 1

Latest checkpoint's NextMultiOffset: 0

Latest checkpoint's oldestXID: 1802

Latest checkpoint's oldestXID's DB: 1

Latest checkpoint's oldestActiveXID: 0

Time of latest checkpoint: Mon 23 Jan 2017 09:13:09 AM CST

Minimum recovery ending location: 0/0

Backup start location: 0/0

Backup end location: 0/0

End-of-backup record required: no

Current wal_level setting: hot_standby

Current max_connections setting: 100

Current max_prepared_xacts setting: 0

Current max_locks_per_xact setting: 64

Maximum data alignment: 8

Database block size: 8192

Blocks per segment of large relation: 131072

WAL block size: 8192

Bytes per WAL segment: 16777216

Maximum length of identifiers: 64

Maximum columns in an index: 32

Maximum size of a TOAST chunk: 1996

Date/time type storage: 64-bit integers

Float4 argument passing: by value

Float8 argument passing: by value

备端情况



首先,关闭主库


[postgres@node1 data]$ pg_ctl stop -m fast

[root@node1 ~]# ifdown eth0:1

[root@node1 ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:EA:AF:6E

inet addr:192.168.11.68 Bcast:192.168.11.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:feea:af6e/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:30248 errors:0 dropped:0 overruns:0 frame:0

TX packets:141525 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:2199137 (2.0 MiB) TX bytes:199102358 (189.8 MiB)


lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:348 errors:0 dropped:0 overruns:0 frame:0

TX packets:348 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:60247 (58.8 KiB) TX bytes:60247 (58.8 KiB)


备库

[root@node2 ~]# ifup eth0:1

Active connection state: activating

Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/1

state: activated

Connection activated

[root@node2 ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:EA:AF:6E

inet addr:192.168.11.70 Bcast:192.168.11.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:feea:af6e/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:29953 errors:0 dropped:0 overruns:0 frame:0

TX packets:141299 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:2174739 (2.0 MiB) TX bytes:198989432 (189.7 MiB)


eth0:1 Link encap:Ethernet HWaddr 00:0C:29:EA:AF:6E

inet addr:192.168.11.40 Bcast:192.168.11.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1


lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:308 errors:0 dropped:0 overruns:0 frame:0

TX packets:308 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:55374 (54.0 KiB) TX bytes:55374 (54.0 KiB)


备库进行切换

[postgres@node2 ~]$ pg_ctl promote -D /data/pgdata(要加数据库目录)

server promoting


原备库状态

[highgo@node2 ~]$ pg_controldata

pg_control version number: 922、

Catalog version number: 201204301

Database system identifier: 6377220794669856238

Database cluster state:in production

pg_control last modified: Mon 23 Jan 2017 09:13:09 AM CST

Latest checkpoint location: 0/12015D30

Prior checkpoint location: 0/12015C10

Latest checkpoint's REDO location: 0/12015D30

Latest checkpoint's TimeLineID: 3

Latest checkpoint's full_page_writes: on

Latest checkpoint's NextXID: 0/1822

Latest checkpoint's NextOID: 16397

Latest checkpoint's NextMultiXactId: 1

Latest checkpoint's NextMultiOffset: 0

Latest checkpoint's oldestXID: 1802

Latest checkpoint's oldestXID's DB: 1

Latest checkpoint's oldestActiveXID: 0

Time of latest checkpoint: Mon 23 Jan 2017 09:13:09 AM CST

Minimum recovery ending location: 0/0

Backup start location: 0/0

Backup end location: 0/0

End-of-backup record required: no

Current wal_level setting: hot_standby

Current max_connections setting: 100

Current max_prepared_xacts setting: 0

Current max_locks_per_xact setting: 64

Maximum data alignment: 8

Database block size: 8192

Blocks per segment of large relation: 131072

WAL block size: 8192

Bytes per WAL segment: 16777216

Maximum length of identifiers: 64

Maximum columns in an index: 32

Maximum size of a TOAST chunk: 1996

Date/time type storage: 64-bit integers

Float4 argument passing: by value

Float8 argument passing: by value



[postgres@node2 ~]$ ps -ef | grep postgres

postgres 2538 1 0 09:11 pts/0 00:00:00 /opt/pgsql/bin/postgres

postgres 2539 2538 0 09:11 ? 00:00:00 postgres: logger process

postgres 2541 2538 0 09:11 ? 00:00:00 postgres: checkpointer process

postgres 2542 2538 0 09:11 ? 00:00:00 postgres: writer process

postgres 2543 2538 0 09:11 ? 00:00:00 postgres: stats collector process

postgres 2625 2538 0 09:13 ? 00:00:00postgres: wal writer process

postgres 2626 2538 0 09:13 ? 00:00:00 postgres: autovacuum launcher process

postgres 2432 2425 0 09:06 ? 00:00:00 postgres: stats collector process

postgrs 2435 2425 0 09:06 ? 00:00:00postgres: archiver process last was 000000.4history

root 3348 1992 0 10:06 pts/0 00:00:00 su - postgres

postgres 3349 3348 0 10:06 pts/0 00:00:00 -bash

postgres 3395 3349 0 10:13 pts/0 00:00:00 ps -ef

postgres 3396 3349 0 10:13 pts/0 00:00:00 grep postgres


在原主库配置recovery.conf文件

[postgres@node1 data]$ vi recovery.conf

standby_mode='on'

recovery_target_timeline = 'latest'

primary_conninfo='host=192.168.11.68 port=5866 user=standby password=standby'

trigger_file='opt/pgsql/trigger_node'



启动原主库

[postgres@node2 ~]$ pg_ctl start

[postgres@node2 ~]$ ps -ef | grep postgres

postgres 2169 1 0 09:04 pts/0 00:00:00 /opt/pgsql/bin/postgres

postgres 2170 2169 0 09:04 ? 00:00:00 postgres: logger process

postgres 2171 2169 0 09:04 ? 00:00:00 postgres: startup process waiting for 000000020000000000000012

postgres 2172 2169 0 09:04 ? 00:00:00 postgres: checkpointer process

postgres 2173 2169 0 09:04 ? 00:00:00 postgres: writer process

postgres 2175 2169 0 09:04 ? 00:00:00 postgres: stats collector process

root 2241 1992 0 09:06 pts/0 00:00:00 su - postgres

postgres 2242 2241 0 09:06 pts/0 00:00:00 -bash

postgres 2283 2169 0 09:07 ? 00:00:00postgres: wal receiver process

postgres 2288 2242 0 09:09 pts/0 00:00:00 ps -ef

postgres 2289 2242 0 09:09 pts/0 00:00:00 grep postgres



测试:建表插入数据

能实现主备同步说明切换成功!

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
SQL 关系型数据库 MySQL
Ruoyi从mysql切换到postgresql的几个坑
本文详细介绍基于ruoyi的数据库从mysql切换到postgresql详细步骤。
1144 0
Ruoyi从mysql切换到postgresql的几个坑
|
弹性计算 网络协议 容灾
PostgreSQL 时间点恢复(PITR)在异步流复制主从模式下,如何避免主备切换后PITR恢复(备库、容灾节点、只读节点)走错时间线(timeline , history , partial , restore_command , recovery.conf)
标签 PostgreSQL , 恢复 , 时间点恢复 , PITR , restore_command , recovery.conf , partial , history , 任意时间点恢复 , timeline , 时间线 背景 政治正确非常重要,对于数据库来说亦如此,一个基于流复制的HA架构的集群,如果还有一堆只读节点,当HA集群发生了主备切换后,这些只读节点能否与新的主节点保持
1825 0
|
SQL 存储 关系型数据库
PostgreSQL 流复制搭建主从环境,同步和异步的解释,压力测试,主从角色切换|学习笔记
快速学习PostgreSQL 流复制搭建主从环境,同步和异步的解释,压力测试,主从角色切换
PostgreSQL 流复制搭建主从环境,同步和异步的解释,压力测试,主从角色切换|学习笔记
|
关系型数据库 测试技术 数据库
PostgreSQL pg_rewind,时间线修复,脑裂修复,flashback - 从库开启读写后,回退为只读从库。异步主从发生角色切换后,主库rewind为新主库的从库
PostgreSQL pg_rewind,时间线修复,脑裂修复,flashback - 从库开启读写后,回退为只读从库。异步主从发生角色切换后,主库rewind为新主库的从库
2202 1
|
SQL Oracle 关系型数据库
PostgreSQL pg_rewind,时间线修复,脑裂修复,flashback - 从库开启读写后,回退为只读从库。异步主从发生角色切换后,主库rewind为新主库的从库
标签 PostgreSQL , pg_rewind , 主从切换 , 时间线修复 , 脑裂修复 , 从库开启读写后,回退为只读从库 , 异步主从发生角色切换后,主库rewind为新主库的从库 背景 1、PG物理流复制的从库,当激活后,可以开启读写,使用pg_rewind可以将从库回退为只读从库的角色。而不需要重建整个从库。 2、当异步主从发生角色切换后,主库的wal目录中可能还有没完全
1137 0
|
关系型数据库 数据库 PostgreSQL
PostgreSQL 流复制的主备切换
概述 流复制的主库和备库的角色不是静态存在的,在维护过程中可以对二者进行角色切换。例如当主库硬件故障或主库需要调整参数需要重启系统时,通常要进行流复制的主备切换。 如何判断主备角色 在通常的主备架构下(一主多备,级联除外)有以下五种方法判断主备角色 操作系统上查看wal发送进程或wal接收线程 ...
10450 0