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



测试:建表插入数据

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

上一篇:ansible-playbook脚本-从被管理机收集指定日期标识的文件


下一篇:你的下一个桌面会是云桌面吗?阿里无影云桌面简单测评。