日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

PostgreSQL 9.6 keepalived主从部署

發布時間:2023/12/1 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的PostgreSQL 9.6 keepalived主从部署的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。