阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

PostgreSQL9.6主从部署详解

130次阅读
没有评论

共计 3395 个字符,预计需要花费 9 分钟才能阅读完成。

实验环境

名称

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;

PostgreSQL9.6 主从部署详解

查看主从状态

1
select * from pg_stat_replication;

PostgreSQL9.6 主从部署详解

脚本监控主从

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

PostgreSQL9.6 主从部署详解

备库

PostgreSQL9.6 主从部署详解

停止主库

PostgreSQL9.6 主从部署详解

查看 slave 的日志

PostgreSQL9.6 主从部署详解

创建触发文件,切换主

1
touch trigger.kenyon

PostgreSQL9.6 主从部署详解

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

PostgreSQL9.6 主从部署详解

使用pg_controldata

PostgreSQL9.6 主从部署详解

PostgreSQL9.6 主从部署详解

备机状态为: 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

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计3395字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中