你好,游客 登录 注册 搜索
背景:
阅读新闻

PostgreSQL9.6主从部署详解

[日期:2017-03-24] 来源:Linux社区  作者:kaliarch [字体:大 中 小]

实验环境

名称

IP

系统

Master

172.17.10.190

CentOS 6.5

Slave

172.17.10.189

CentOS 6.5

1.yun安装

1
2
rpm -ivh https://download.postgresql.org/pub/repos/yum/9.6/RedHat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm
yum install postgresql96.x86_64 postgresql96-server.x86_64 -y

2.主从配置

2.1 主数据库配置

启动master

1
2
3
4
/etc/init.d/postgresql-9.6 initdb
/etc/init.d/postgresql-9.6 start
su - postgres
psql

授权

1
create role repl login replication encrypted password '51idc.com';

编辑hba文件

/var/lib/pgsql/9.6/data/pg_hba.conf

新增

1
2
host    replication     repl            172.17.10.0/24         md5
host    all            repl            172.17.10.0/24         trust

编辑配置文件

/var/lib/pgsql/9.6/data/postgresql.conf

1
2
3
4
5
6
7
8
listen_addresses = 172.17.10.190
wal_level = hot_standby  #热备模式
max_wal_senders= 6 #可以设置最多几个流复制链接,差不多有几个从,就设置多少
wal_keep_segments = 10240  #重要配置 
wal_send_timeout = 60s 
max_connections = 512 #从库的 max_connections要大于主库
archive_mode = on #允许归档 
archive_command = 'cp %p /url/path%f'   #根据实际情况设置

2.2 从数据库配置

1
su - postgres

如果开始为启动数据库可忽略下一步

1
2
3
rm -rf /var/lib/pgsql/9.6/data/*  #开始没有启动从数据库,这一步可以省略 
pg_basebackup -h 172.17.10.190 -U repl -D /var/lib/pgsql/9.6/data -X stream -P
cp /usr/pgsql-9.6/share/recovery.conf.sample /var/lib/pgsql/9.6/data/recovery.conf

修改配置文件recovery.conf

1
2
3
4
standby_mode = on
primary_conninfo = 'host=172.17.10.190 port=5432 user=repl password=51idc.com'
trigger_file = '/var/lib/pgsql/9.6/data/trigger.kenyon'    #主从切换时后的触发文件
recovery_target_timeline = 'latest'

配置postgresql.conf文件

1
2
3
4
5
6
7
listen_addresses = 172.17.10.189
wal_level = hot_standby 
max_connections = 1000 #一般从的最大链接要大于主的。 
hot_standby = on #说明这台机器不仅仅用于数据归档,也用于查询 
max_standby_streaming_delay = 30s 
wal_receiver_status_interval = 10s #多久向主报告一次从的状态。 
hot_standby_feedback = on #如果有错误的数据复制,是否向主进行范例

检测

1
select client_addr,sync_state from pg_stat_replication;

wKioL1jT9aOCAZVTAACJLn0Arvk876.png

查看主从状态

1
select * from pg_stat_replication;

wKiom1jT9oqDxXLKAADvjuYegcw403.png

脚本监控主从

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# mail  xuel@51idc.com
data=`date +%Y-%M-%d" "%H:%m`
netstat -lntup|grep 5432 && ps -ef|grep postmaster
if [ $? -eq 0 ];then
for IP in 172.17.10.188 172.17.10.189
do
/usr/bin/psql -h 172.17.10.190 -p 5432 -U repl -d postgres --command "select * from pg_stat_replication"|grep $IP
if [ "$?" != "0" ];then
echo
 "postgresql master-slave status is error! please login check!"|mail -r 
"xuel@51idc.com" -s "postgresql master-slave status is error" 
xuel@51idc.com \
&& echo "$data postgresql postgresql master-slave status is error!">>/var/log/postgresql-error.log
fi
done
else
echo
 "postgresql master-slave status is error! please login check!"|mail -r 
"xuel@51idc.com" -s "postgresql master-slave status is error" 
xuel@51idc.com \
&& echo "$data postgresql postgresql master-slave status is error!">>/var/log/postgresql-error.log
fi

2.3主从切换

主库查看进程为sender

wKiom1jUiKTwsu0UAALvaWigL5E515.png

备库

wKioL1jUiRjB8XPAAAJNDLZCTIU269.png

停止主库

wKioL1jUiVGSZNWiAAEmy2IYoj8840.png

查看slave的日志

wKioL1jT-KujDMuiAANHbW6b5yI968.png

创建触发文件,切换主

1
touch trigger.kenyon

wKiom1jT-Q3RDgNUAAUdPTrS_YI464.png

查看slave的日志,面前已经切换为主

wKiom1jT-UnyC2mnAAIvlmww7-I077.png

使用pg_controldata

wKioL1jT-XXiXGkoAABQpY5XhfM716.png

wKioL1jT-bDAuyz9AANjzAsM3a4422.png

备机状态为: in archive recovery

主库状态为:in production

Ubuntu 16.04 下安装 PostgreSQL 和 phpPgAdmin  http://www.linuxidc.com/Linux/2016-08/134260.htm

Linux下RPM包方式安装PostgreSQL  http://www.linuxidc.com/Linux/2016-03/128906.htm

Linux下安装PostgreSQL  http://www.linuxidc.com/Linux/2016-12/138765.htm

Linux下PostgreSQL安装部署指南  http://www.linuxidc.com/Linux/2016-11/137603.htm

Linux下安装 PostgreSQL 并设置基本参数  http://www.linuxidc.com/Linux/2016-11/137324.htm

Fedota 24 将数据库升级到 PostgreSQL 9.5  http://www.linuxidc.com/Linux/2016-11/137374.htm

CentOS 6.5下PostgreSQL服务部署  http://www.linuxidc.com/Linux/2017-01/139144.htm

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

本文永久更新链接地址���http://www.linuxidc.com/Linux/2017-03/142145.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款