PostgreSQL 9.6 keepalived主从部署

## 环境:

PostgreSQL版:9.6

 

角色                     OS                    IP
master                 CentOS7   10.100.12.73

slave                    CentOS7          10.100.12.74

vIP                                                10.100.12.63

 

 ## 主从安装postgresql

postgresql官网安装文档:https://www.postgresql.org/download/linux/redhat/

* Install the repository RPM:

 

yum -y install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm

 

* Install the client packages:

yum -y install postgresql96

* Optionally install the server packages:

yum -y install postgresql96-server postgresql96-devel

* Optionally initialize the database and enable automatic start:

/usr/pgsql-9.6/bin/postgresql96-setup initdb
mv /usr/lib/systemd/system/postgresql-9.6.service /usr/lib/systemd/system/postgresql.service
systemctl enable postgresql
暂时先不启动服务

 

把/usr/pgsql-9.6/bin 加入系统环境变量

tail /etc/profile
## PATH export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/pgsql-9.6/bin

重载环境变量

. /etc/profile

 

 

## master服务器配置

启动postgresql服务

systemctl start postgresql

 

创建同步用户repluser

su - postgres
psql
create role repluser login replication encrypted password 'yHJ7TXda9q9zzIsv';
postgres=# \du # 查看用户
\q


 

编辑 /var/lib/pgsql/9.6/data/pg_hba.conf  新增下面两行

host    replication     repluser        10.100.12.74/32         md5
host all all 0.0.0.0/0 md5

 

 

 

mkdir -p /data/pgsql/archivedir; chown -R postgres:postgres /data/pgsql/archivedir

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

listen_addresses = '*'		# what IP address(es) to listen on;port = 5432				# (change requires restart)
max_connections = 512			# (change requires restart) #从库的 max_connections要大于主库shared_buffers = 128MB			# min 128kBdynamic_shared_memory_type = posix	# the default is the first optionwal_level = hot_standby			# minimal, replica, or logical  #热备模式

archive_mode = on               # enables archiving; off, on, or always #允许归档

archive_command = 'test ! -f /data/pgsql/archivedir/%f && cp %p /data/pgsql/archivedir/%f' # command to use to archive a logfile segmentmax_wal_senders = 8 # max number of walsender processes #可以设置最多几个流复制链接,差不多有几个从,就设置多少wal_keep_segments = 1024 # in logfile segments, 16MB each; 0 disableslog_destination = 'stderr' # Valid values are combinations oflogging_collector = on # Enable capturing of stderr and csvloglog_directory = 'pg_log' # directory where log files are written,log_filename = 'postgresql-%a.log' # log file name pattern,log_truncate_on_rotation = on # If on, an existing log file with thelog_rotation_age = 1d # Automatic rotation of logfiles willlog_rotation_size = 0 # Automatic rotation of logfiles willlog_line_prefix = '< %m > ' # special values:log_timezone = 'PRC'datestyle = 'iso, mdy' timezone = 'PRC'lc_messages = 'en_US.UTF-8' # locale for system error messagelc_monetary = 'en_US.UTF-8' # locale for monetary formatting lc_numeric = 'en_US.UTF-8' # locale for number formatting lc_time = 'en_US.UTF-8' # locale for time formattingdefault_text_search_config = 'pg_catalog.english'

重启postgresql服务

systemctl restart postgresql

 

## slave服务器配置

mkdir -p /data/pgsql/archivedir; chown -R postgres:postgres /data/pgsql/archivedir

su - postgres
rm -rf /var/lib/pgsql/9.6/data/*  #开始没有启动从库服务,这一步可以省略
pg_basebackup -h 10.100.12.73 -U repluser -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

 

修改配置文件 /var/lib/pgsql/9.6/data/recovery.conf

grep -v "^#" /var/lib/pgsql/9.6/data/recovery.confrecovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=10.100.12.73 port=5432 user=repluser password=yHJ7TXda9q9zzIsv'		# e.g. 'host=localhost port=5432'
trigger_file = '/var/lib/pgsql/9.6/data/trigger.kenyon'     #主从切换时后的触发文件,即 touch /var/lib/pgsql/9.6/data/trigger.kenyon 就可切换主从,也可以使用命令 /usr/pgsql-9.6/bin/pg_ctl promote

 

配置postgresql.conf文件

listen_addresses = '*'		# what IP address(es) to listen on;port = 5432				# (change requires restart)
max_connections = 1024			# (change requires restart) 一般从的最大链接要大于主的shared_buffers = 128MB			# min 128kBdynamic_shared_memory_type = posix	# the default is the first optionwal_level = hot_standby			# minimal, replica, or logicalarchive_mode = on               # enables archiving; off, on, or alwaysarchive_command = 'test ! -f /data/pgsql/archivedir/%f && cp %p /data/pgsql/archivedir/%f'		# command to use to archive a logfile segmentmax_wal_senders = 8		# max number of walsender processeswal_keep_segments = 1024		# in logfile segments, 16MB each; 0 disableshot_standby = on			# "on" allows queries during recovery  #说明这台机器不仅仅用于数据归档,也用于查询max_standby_streaming_delay = 30s	# max delay before canceling querieswal_receiver_status_interval = 10s	# send replies at least this often #多久向主报告一次从的状态hot_standby_feedback = on		# send info from standby to prevent #如果有错误的数据复制,是否向主进行反馈log_destination = 'stderr'		# Valid values are combinations oflogging_collector = on			# Enable capturing of stderr and csvloglog_directory = 'pg_log'		# directory where log files are written,log_filename = 'postgresql-%a.log'	# log file name pattern,log_truncate_on_rotation = on		# If on, an existing log file with thelog_rotation_age = 1d			# Automatic rotation of logfiles willlog_rotation_size = 0			# Automatic rotation of logfiles willlog_line_prefix = '< %m > '			# special values:log_timezone = 'PRC'datestyle = 'iso, mdy'
timezone = 'PRC'lc_messages = 'en_US.UTF-8'			# locale for system error messagelc_monetary = 'en_US.UTF-8'			# locale for monetary formatting
lc_numeric = 'en_US.UTF-8'			# locale for number formatting
lc_time = 'en_US.UTF-8'				# locale for time formattingdefault_text_search_config = 'pg_catalog.english'

 

启动 postgresql服务

 

## 查看postgresql主从状态

在 master上执行
su - postgrespsqlselect client_addr,sync_state from pg_stat_replication;select * from pg_stat_replication;

 

pg_controldata /var/lib/pgsql/9.6/data    # 这种方法对于直接kill进程的情况下是不适用的,查看结果不准确,Database cluster state:信息

主库状态为:in production

备机状态为: in archive recovery

 

## keepalived配置

主从安装keepalived

yum -y install keepalived

 

master keepalived 配置

cat /etc/keepalived/keepalived.conf

global_defs {notification_email {admin@xx.com}notification_email_from keepalived@xx.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id pg_ha
}vrrp_script chk_postgresql {script "/etc/keepalived/script/script/check_postgresql.sh |grep 'postgresql_success' "interval 2weight -10
}vrrp_instance VI_1 {state BACKUP        ############ 辅机为 BACKUPinterface eth0virtual_router_id 62mcast_src_ip 10.100.12.73priority 100                  ########### 权值要比 back 高advert_int 2nopreemptauthentication {auth_type PASSauth_pass SNKQusp4kFpUKz}track_script { chk_postgresql ### 执行监控的服务 }virtual_ipaddress {10.100.12.63}notify_master "/bin/python /etc/keepalived/script/keepalived_notify.py 'PostgreSQL-1 [10.100.12.73] change to master, vip:10.100.12.63' "notify_backup "/bin/python /etc/keepalived/script/keepalived_notify.py 'PostgreSQL-1 [10.100.12.73] postgresql check faild, change to slave, vip:10.100.12.63' "}

 

sh脚本:

cd /etc/keepalived/script    #sh脚本赋予可执行权限

cat check_postgresql.sh

#!/bin/bash
# songyanlinpguser="postgres"
BIN="/usr/pgsql-9.6/bin"
datef=`date +%Y-%M-%d" "%H:%m`
data_dir="/var/lib/pgsql/9.6/data"
log_dir="/var/log/postgresql.log"
service_name="postgresql"
pid="postmaster"
status="postgresql_failed"
status_success="postgresql_success"function CheckService(){local ret=`$BIN/pg_controldata $data_dir |grep -E "in production|in archive recovery" |wc -l`echo $ret
}function CheckPs(){local ret=`pidof $pid |wc -l`echo $ret
}if [ $(CheckService) == 0 -o $(CheckPs) == 0 ]; thenecho "$datef postgresql master status is erro!" >> $log_dirservice $service_name restartif [ $(CheckService) != 0 -a $(CheckPs) != 0 ]; thenstatus=$status_successfi
elsestatus=$status_success
fiecho $status

 

cat keepalived_notify.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-import smtplib
from email.mime.text import MIMEText
from email.header import Header
import sys, time, subprocess, random# 第三方 SMTP 服务
mail_host="smtp.exmail.qq.com"  #设置服务器
userinfo_list = [{'user':'rp1@qq.com','pass':'pwd'}, {'user':'rp2@qq.com','pass':'pwd'}, {'user':'rp3@tuandai.com','pass':'pwd'}]user_inst = userinfo_list[random.randint(0, len(userinfo_list)-1)]
mail_user=user_inst['user'] #用户名
mail_pass=user_inst['pass'] #口令sender = mail_user # 邮件发送者
receivers = ['mymail@163.com', 'gogo@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱p = subprocess.Popen('hostname', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
hostname = p.stdout.readline().split('\n')[0]message_to = ''
for i in receivers:message_to += i + ';'def print_help():note = '''python script.py message'''print(note)exit(1)time_stamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))message_content = ''
if len(sys.argv) == 2:message_content = '%s [%s] %s' %(time_stamp, hostname ,sys.argv[1])subject = '%s [%s] postgresql status is error' %(time_stamp, hostname)
else:print_help()message = MIMEText(message_content, 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] =  Header(message_to, 'utf-8')message['Subject'] = Header(subject, 'utf-8')try:smtpObj = smtplib.SMTP()smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号smtpObj.login(mail_user,mail_pass)smtpObj.sendmail(sender, receivers, message.as_string())print("邮件发送成功")
except smtplib.SMTPException as e:print("Error: 无法发送邮件")print(e)

 

slave keepalived配置

cat /etc/keepalived/keepalived.conf

global_defs {notification_email {admin@xx.com}notification_email_from keepalived@xx.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id pg_ha
}vrrp_script chk_postgresql {script "/etc/keepalived/script/check_postgresql.sh |grep 'postgresql_success' "interval 2weight -10
}vrrp_instance VI_1 {state BACKUP        ############ 辅机为 BACKUPinterface eth0virtual_router_id 62mcast_src_ip 10.100.12.74priority 99                  ########### 权值要比 back 高advert_int 2#nopreemptauthentication {auth_type PASSauth_pass SNKQusp4kFpUKz}track_script { chk_postgresql ### 执行监控的服务 }virtual_ipaddress {10.100.12.63}notify_master "/etc/keepalived/script/postgresql_slave_to_master.sh"}

 

sh脚本:

check_postgresql.sh  keepalived_notify.py与master相同

 

cat postgresql_slave_to_master.sh

#!/bin/bash
#pguser="postgres"
BIN="/usr/pgsql-9.6/bin"
datef=`date +%Y-%M-%d" "%H:%m`
data_dir="/var/lib/pgsql/9.6/data"
log_dir="/var/log/postgresql.log"
service_name="postgresql"
pid="postmaster"
status="postgresql_failed"
status_success="postgresql_success"function CheckService(){local ret=`$BIN/pg_controldata $data_dir |grep "in production" |wc -l`echo $tet
}function CheckStatus(){local ret=`$BIN/pg_controldata $data_dir |grep "shut down in recovery" |wc -l`echo $ret
}function CheckStatus2(){local ret=`$BIN/pg_controldata $data_dir |grep "in archive recovery" |wc -l`echo $ret
}if [ $(CheckStatus) != 0 ];thenservice $service_name restart
fiif [ $(CheckStatus2) != 0 ]; thensu - $pguser -c "$BIN/pg_ctl promote"
fi/bin/python /etc/keepalived/script/keepalived_notify.py "PostgreSQL[10.100.12.74] change to master, vip:10.100.12.63"

 

 

附:

若主从已经切换后,把原来的master设置为从,可按上面从机设置方法设置

 

 

 

postgresql扩展组件

报错:

Running handlers:
There was an error running gitlab-ctl reconfigure:bash[migrate gitlab-rails database] (gitlab::database_migrations line 51) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of "bash"  "/tmp/chef-script20180125-31534-ul2ug1" ----
STDOUT: rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedFile: ERROR:  could not open extension control file "/usr/pgsql-9.6/share/extension/pg_trgm.control": No such file or directory
: CREATE EXTENSION IF NOT EXISTS "pg_trgm"
/opt/gitlab/embedded/service/gitlab-rails/db/schema.rb:18:in `block in <top (required)>'
/opt/gitlab/embedded/service/gitlab-rails/db/schema.rb:14:in `<top (required)>'
/opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:52:in `block (3 levels) in <top (required)>'
/opt/gitlab/embedded/bin/bundle:23:in `load'
/opt/gitlab/embedded/bin/bundle:23:in `<main>'Caused by:
PG::UndefinedFile: ERROR:  could not open extension control file "/usr/pgsql-9.6/share/extension/pg_trgm.control": No such file or directory
/opt/gitlab/embedded/service/gitlab-rails/db/schema.rb:18:in `block in <top (required)>'
/opt/gitlab/embedded/service/gitlab-rails/db/schema.rb:14:in `<top (required)>'
/opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:52:in `block (3 levels) in <top (required)>'
/opt/gitlab/embedded/bin/bundle:23:in `load'
/opt/gitlab/embedded/bin/bundle:23:in `<main>'
Tasks: TOP => db:schema:load
(See full trace by running task with --trace)
-- enable_extension("plpgsql")-> 0.0224s
-- enable_extension("pg_trgm")
STDERR: 
---- End output of "bash"  "/tmp/chef-script20180125-31534-ul2ug1" ----
Ran "bash"  "/tmp/chef-script20180125-31534-ul2ug1" returned 1

 

 

yum -y install postgresql96-contrib-9.6.6    # 默认的 yum -y install postgresql-contrib

su - postgres-bash-4.2$ psql gitlabhq_production psql (9.6.6) Type "help" for help.postgres=# CREATE EXTENSION pg_trgm;

 

转载于:https://www.cnblogs.com/linkenpark/p/8339936.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369735.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CSS——清除浮动的六种解决方案

内容的高度撑起父元素容器的高度&#xff0c;效果图如下HTML和CSS代码如下给&#xff50;标签加上浮动以后&#xff0c;&#xff50;&#xff5b;float&#xff1a;left&#xff1b;&#xff5d;&#xff0c;此时DIV塌陷&#xff0c;两段内容同行显示&#xff0c;效果如下&…

40个Java Collections面试问答

Java Collections Framework是Java编程语言的基本方面。 这是Java面试问题的重要主题之一。 在这里&#xff0c;我列出了Java集合框架的一些重要问题和解答。 什么是Java Collections Framework&#xff1f; 列出Collections框架的一些好处&#xff1f; 集合框架中泛型的好处…

vs mysql iss_MySQL5.7与8.0的连接问题(vs2015\2017)

1.MySQL8.0 root密码忘记重置与5.7不同&#xff0c;绝大多数经验帖不适用8.0https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html8.0 重置密码的方式2.MySQL连接vs2015时报错提示&#xff1a;Authentication method ‘caching_sha2_password‘ not supported …

191. Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1 bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11 has binary representation 00000000000000000000000000001011, so the function should return 3. …

AtCoder Beginner Contest 084(AB)

A - New Year 题目链接&#xff1a;https://abc084.contest.atcoder.jp/tasks/abc084_a Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement How many hours do we have until New Year at M oclock (24-hour notation) on 30th, December? Cons…

打开就随机生长的树

今天接触了一个新东西&#xff0c;感觉很酷炫的样子。不是我写的&#xff0c;拿给大家看一看&#xff0c;喜欢的可以直接拿走不谢。树的形状和树枝多少都是随机的&#xff0c;每刷新一次就有一次的惊喜哦&#xff0c;无聊的亲们可以多刷几次&#xff0c;当动画来看哦。2017年又…

等待正确的时刻–集成测试

当您必须测试多线程程序时&#xff0c;总是需要等到系统达到特定状态后&#xff0c;测试才能验证是否达到了正确的状态。 这样做的通常方法是在系统中插入一个“探针”&#xff0c;该探针将向同步原语发出信号 &#xff08;例如Semaphore &#xff09;&#xff0c;并且测试将一…

网络编程---黏包

基于UDP协议的socket udp的server 不需要进行监听也不需要建立连接&#xff0c;在启动服务之后只能被动的等待客户端发送消息过来。 客户端发送消息的同时还会 自带地址信息&#xff0c;消息回复的时候 不仅需要发送消息 还需把对方的地址填上。 udp的client 不需要connect 因为…

CSS布局(二) 盒子模型属性

盒子模型的属性 宽高width/height 在CSS中&#xff0c;可以对任何块级元素设置显式高度。 如果指定高度大于显示内容所需高度&#xff0c;多余的高度会产生一个视觉效果&#xff0c;就好像有额外的内边距一样&#xff1b; 如果指定高度小于显示内容所需高度&#xff0c;取决于…

Extjs 下拉框

刚刚熟练了easyui控件的使用&#xff0c;又開始了如今的这个项目。这个项目是个半成品。前端使用的是Extjs控件&#xff0c;jsp中没有代码。就引用了非常多的js。。。于是乎有种不知所措了呀。。。 说实话特别的不想去看那些代码&#xff0c;第一是不熟悉&#xff0c;第二是太乱…

Java中的贷款模式(又名贷方承租人模式)

这篇文章是关于在Java中实现贷款模式的。 用例 在保存资源的代码与访问资源的代码之间实现分离&#xff0c;从而使访问代码无需管理资源。 当我们编写用于读取/写入文件或查询SQL / NOSQL数据库的代码时&#xff0c;上述用例适用。 在AOP的帮助下&#xff0c;肯定有API处理了此…

python亲密度_859. 亲密字符串(Python)

题目难度&#xff1a;★★☆☆☆类型&#xff1a;字符串给定两个由小写字母构成的字符串 A 和 B &#xff0c;只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果&#xff0c;就返回true &#xff1b;否则返回 false 。提示0 < A.length < 200000 < B.length &l…

开发电子商城1

1 从阿里云的镜像 下载centos 6.8 64位 https://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-bin-DVD1.iso 2&#xff1a;用vm安装 选定basic service版本 3&#xff1a;将Centos的yum源更换为国内的阿里云源 http://www.centoscn.com/image-text/config/20…

静态工厂方法与传统构造方法

之前&#xff0c;我已经讨论过一些关于Builder模式的信息 &#xff0c; Builder Pattern是一种有用的模式&#xff0c;用于实例化具有多个&#xff08;可能是可选的&#xff09;属性的类&#xff0c;这些属性可以使读取&#xff0c;编写和维护客户端代码更加容易&#xff0c;还…

python输入代码界面通常_vscode写python时的代码错误提醒和自动格式化的方法

python的代码错误检查通常用pep8、pylint和flake8&#xff0c;自动格式化代码通常用autopep8、yapf、black。这些工具均可以利用pip进行安装&#xff0c;这里介绍传统的利用pip.exe安装和在VScode中安装两种方式。【温馨提醒】要使用flake8或要想flake8等工具起作用&#xff0c…

HTML/CSS 知识点

本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 整个前端开发的工作流程 产品经理提出项目需求UI出设计稿前端人员负责开发静态页面(跟前端同步的后台人员在准备数据)前后台的交互测试产品上线(后期项目维护) 互联网原…

枚举枚举和修改“最终静态”字段的方法

在本新闻通讯中&#xff0c;该新闻通讯最初发表在Java专家的新闻通讯第161期中&#xff0c;我们研究了如何使用sun.reflect包中的反射类在Sun JDK中创建枚举实例。 显然&#xff0c;这仅适用于Sun的JDK。 如果需要在另一个JVM上执行此操作&#xff0c;则您可以自己完成。 这一…

java编译找不到符号_关于久违的Javac,编译出现“找不到符号”

参考文档&#xff1a;http://blog.csdn.net/qq369201191/article/details/49946609工作以来习惯了maven编译&#xff0c;已经忘记了javac这个东东&#xff0c;以至于遇到javac问题时困惑了&#xff0c;下面总结一下&#xff0c;以便后者参考。一、使用javac进行项目java文件编译…

CSS--居中方式总结

一、水平居中方法 1.行内元素、字体的水平居中 1.对于行内元素&#xff08;display值为inline或inline-block都可以&#xff09;或者字体&#xff1a;父元素添加css规则&#xff1a;text-align&#xff1a;center; <style> p{/*关键*/text-align:center; }ul{/*关键*/…

009-MailUtils工具类模板

版本一&#xff1a;JavaMail的一个工具类 package ${enclosing_package};import java.security.GeneralSecurityException; import java.util.Properties;import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.ma…