PostgreSQL的HA解决方案-1主从和备份(master/slave and backup)

来源:互联网 发布:淘宝试用网站 编辑:程序博客网 时间:2024/06/07 19:40

一.部署说明

1.1 实施环境

本文档实验环境如下:

PGSQL主机: 192.168.1.45

PGSQL备机: 192.168.1.50

 

软件和系统版本

Pgsql 版本: pgsql 9.2.4

Linux 版本: Redhat 5.8

 

1.2 文档说明

  本文档详细说明了PG的双机扩展,使pgsql数据库具备双机热备流复制功能。注意,双机热备中存在主从关系,这样才能保证数据的一直性,主机操作都可以,但从机只能用作查询。所以热备的意义在于分担主机的查询压力,备份主机数据,当主机出问题后,从机稍加修改就可以变为主机。

  为了试验减少不必要的麻烦,本系列试验中,各个主机之间的通信,都是设置为无密码访问,但对于实际生产,需要根据实际需要,配置密码,增强集群的安全性。

二.配置步骤

  1.1 (主从一样)。编辑data/pg_hba.conf 访问控制文件。    

    在配置pgsql之前,首先要设置两台服务器使得postgres用户可以无密码相互访问。 配置过程一定要分清主从机。因为实验的两台虚拟机所在网段都是192.168.1.0/24.所以,新增如下:

# TYPE  DATABASE        USER            ADDRESS                 METHOD# "local" is for Unix domain socket connections onlylocal   all             all                                     trust# IPv4 local connections:host    all             all             127.0.0.1/32            trusthost    all             all             192.168.1.0/24          trust# IPv6 local connections:host    all             all             ::1/128                 trust# Allow replication connections from localhost, by a user with the# replication privilege.#local   replication     postgres                                trust#host    replication     postgres        127.0.0.1/32            trusthost    replication     postgres        192.168.1.0/24           trust#host    replication     postgres        ::1/128                 trust

  1.2 (主从一样)。编辑data/postgresql.conf 数据库配置文件。

    主从机的数据库postgresql.conf配置相同,主要是方便接下来的将主机的data文件直接备份还原到从机,并且也方便将来主从切换。请对照默认参数设置,设置相应参数,一些参数的设置,如

max_connections = 100       #最大连接数的设置,当超过最大链接时,数据库拒绝访问。

shared_buffers = 1280MB      #共享内存,主要是和服务器的硬件和实际的项目需求相关,理论上在硬件资源允许的情况下,此值越大越好。

wal_level = hot_standby       #开启主从热备模式

fsync = on              #异步数据同步

full_page_writes = on

checkpoint_segments = 32      #关于脏数据写回的
archive_mode = on                    #打开归档模式

archive_command ='cp %p /opt/bjca3/pgsql/data/archive/%f ' #将归档文件保存在data/archive目录下
max_wal_senders = 2         #根据从机的个数设置,实验中只有一个从机,所以只设置为1

wal_keep_segments = 250       

hot_standby = on             #在配置中,并不是每个配置都起作用的,数据库会根据当前的主从机的属性,自动判断哪些是主机的配置,哪些是从机的配置,配置文档中也有说明。
max_standby_archive_delay = 300s

max_standby_streaming_delay = 300s

wal_receiver_status_interval = 10s

hot_standby_feedback = on 

log_line_prefix = '[%t] '

    详细的配置及含义请看下边的配置文件。

 View Code

  1.3 此时主从数据库的配置是相同的,所以可以将主库直接备份,然后还原至从库。其实对于从库,配置本身很简单,只是因为考虑到接下来的主从切换麻烦,所以讲主从配置成一样的。

此时可以使用PG自带的pg_basebackup工具,对主机进行远程基础备份。

  在从机中执行:pg_basebackup -F p -D $PGDATA -x -v -h db1 -w

  将主机的数据库远程备份并恢复到从机中。其中db1是我添加在系统hosts里的,对于不想添加的,可以直接使用Ip地址。其实hosts中的文件就是给出不同IP的别名,方便使用。其他参数请--help.

  1.4 备份还原到从机后,需要添加另外一个配置文件:recovery.conf,此时才把主机与从机区分开来。在PGHOME目录下的share文件夹中有实例文件,将之拷贝过来,修改即可。

    执行cp /usr/local/pgsql/share/recovery.conf.sample /usr/local/pgsql/data/recovery.conf. 

    主要配置:

standby_mode = 'on'          #开启从机模式

trigger_file = '/usr/local/pgsql/data/pg.trigger'#注意路径是可以自定义的,但是当主机失效,从机变为主机是,为产生该文件,但当重新恢复为从机自己写的shell脚本要删除该文件,注意权限问题。

primary_conninfo = 'host=db1'

  1.5 此时可以启动从机的数据库了。此时会报如下错误:

FATAL:  data directory "/usr/local/pgsql/data" has group or world access
DETAIL:  Permissions should be u=rwx (0700).

  所以,此时要修改data的权限为700.修改之后启动,就没问题了。此时查看启动日志:data/pg_log中的日志如下:

LOG:  creating missing WAL directory "pg_xlog/archive_status"LOG:  entering standby modeLOG:  redo starts at 0/4000020LOG:  consistent recovery state reached at 0/40000C8LOG:  database system is ready to accept read only connectionsLOG:  streaming replication successfully connected to primary

  说明,此时从机已经开启了从机模式,只读模式等待连接,并且已经连接到主机的数据库。

 

  1.6验证数据同步。

    ①在主库中创建表,查看从库中是否有同样的数据表即可。

    ②当然也可以主机中在psql中执行:select * from pg_stat_replication;

pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | state | sent_location | write_locat
ion | flush_location | replay_location | sync_priority | sync_state 
-------+----------+----------+------------------+--------------+-----------------+-------------+-------------------------------+-----------+---------------+------------
----+----------------+-----------------+---------------+------------
22877 | 10 | postgres | walreceiver | 192.168.1.50 | | 47967 | 2015-09-18 16:09:13.711784+08 | streaming | 0/5016334 | 0/5016334 
| 0/5016334 | 0/5016334 | 1 | sync

    ③在主机中运行命令:top -c -u postgres

12707 postgres 15 0 4612 1368 1108 S 0.0 0.1 0:00.09 bash 
13190 postgres 18 0 4992 1256 1072 T 0.0 0.1 0:00.02 vi pg_hba.conf 
17668 postgres 15 0 281m 16m 15m S 0.0 1.6 0:00.03 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data 
17669 postgres 18 0 9124 660 356 S 0.0 0.1 0:00.00 postgres: logger process 
17671 postgres 16 0 281m 3164 2808 S 0.0 0.3 0:00.34 postgres: checkpointer process 
17672 postgres 15 0 281m 2544 2208 S 0.0 0.2 0:00.01 postgres: writer proces
17673 postgres 15 0 281m 840 504 S 0.0 0.1 0:00.00 postgres: wal writer process 
17674 postgres 18 0 282m 1680 984 S 0.0 0.2 0:00.00 postgres: autovacuum launcher process 
17675 postgres 18 0 9168 688 356 S 0.0 0.1 0:00.00 postgres: archiver process last was 000000010000000000000004 
17676 postgres 16 0 9168 796 424 S 0.0 0.1 0:00.03 postgres: stats collector process 
22877 postgres 18 0 282m 2176 1360 S 0.0 0.2 0:00.00 postgres: wal sender process postgres 192.168.1.50(47967) streaming 0/5016334 
25439 postgres 15 0 2432 1068 816 R 0.0 0.1 0:00.19 top -c -u postgres

    ④在从机中运行命令:top -c -u postgres

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
12446 postgres 15 0 4612 1384 1120 S 0.0 0.1 0:00.08 bash 
19711 postgres 15 0 4612 1376 1116 S 0.0 0.1 0:00.02 bash 
20040 postgres 15 0 4612 1360 1124 S 0.0 0.1 0:00.01 bash 
20152 postgres 15 0 4612 1388 1124 S 0.0 0.1 0:00.05 bash 
21809 postgres 18 0 281m 16m 15m S 0.0 1.6 0:00.05 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data 
21810 postgres 22 0 9120 652 352 S 0.0 0.1 0:00.00 postgres: logger process 
21811 postgres 15 0 282m 1784 1420 S 0.0 0.2 0:00.00 postgres: startup process recovering 000000010000000000
21812 postgres 16 0 281m 2988 2640 S 0.0 0.3 0:00.00 postgres: checkpointer process 
21813 postgres 15 0 281m 2532 2200 S 0.0 0.2 0:00.00 postgres: writer process 
21814 postgres 16 0 9164 780 408 S 0.0 0.1 0:00.00 postgres: stats collector process 
21815 postgres 15 0 282m 1768 1244 S 0.0 0.2 0:00.28 postgres: wal receiver process streaming 0/50163B8 
24569 postgres 15 0 2432 1072 816 R 0.0 0.1 0:00.14 top -c -u postgres

  都可以证明已经正常启动。

原创粉丝点击