当前位置: 首页 > news >正文

postgresql主从+repmgr+keepalive安装

  1.           Postgresql16数据库相关介质,统一上传到/tmp/pgsoft/(创建此目录)下:以下操作,所有节点都需要执行。
    1. 安装介质准备
    2. postgresql16-16.4-1PGDG.rhel8.x86_64.rpm

      postgresql16-contrib-16.4-1PGDG.rhel8.x86_64.rpm

      postgresql16-libs-16.4-1PGDG.rhel8.x86_64.rpm

      postgresql16-server-16.4-1PGDG.rhel8.x86_64.rpm

    3. 主机目录建议:

      /usr/pgsql-16/

      postgresql安装目录

      /data/pgdata

      postgresql数据库目录

      /data/pglogs

      Postgresql数据库运行日志目录

      /data/archivelog

      Postgresql数据库归档日志目录

      /data/pgtbs

      Postgresql数据库用户数据表空间目录

      /data/pgwal

      Postgresql数据库WAL日志存放路径

    4. 存储空间规划
    5. 磁盘名

      冗余

      容量

      备注

      /data

      根据存储确定

      500G

      存放potgresql数据库数据及日志

      /

      根据存储确定

      100G

      用于安装postgresql数据库软件

    6. 完装前配置准备
    7. 操作系统准备条件检查
    8. 检查主机名

      #hostname

      修改主机名

      #hostnamectl set-hostname pbgpamsdb01

      检查系统内存

      # free -g

      检查虚拟内存

      # swapon -s

      检查磁盘空间

      #df -h

      检查时间和时区

      # date -R

      # timedatectl |grep "Time zone"

      Time zone: Asia/Shanghai (CST, +0800)

      注: 保证所有节点时间和时区一致。

      1. 配置Chrony服务
    9. 检查chrony服务:由系统管理员完成。

      #cat /etc/chrony.conf |grep -i server

      # Use public servers from the pool.ntp.org project.

      #server 0.rhel.pool.ntp.org iburst

      #server 1.rhel.pool.ntp.org iburst

      #server 2.rhel.pool.ntp.org iburst

      #server 3.rhel.pool.ntp.org iburst

      server 192.168.10.1 iburst

      server 192.168.10.2 iburst

      #systemctl status chronyd

      #systemctl restart chronyd

      #cat /etc/chrony.conf

      #chkconfig --list

      # chkconfig chronyd on

      查看时间同步源:

      # chronyc sources -v

      立即手工同步

      # chronyc -a makestep

      查看时间同步源状态:

      # chronyc sourcestats -v

      ##未配置NTP时间同步,需要修改ntp.conf文件,让CTSSD服务处于ACTIVE状态:

      #   mv /etc/ntp.conf /etc/ntp.conf.bak

      1. 调整系统内核参数
    10. 编辑/etc/sysctl.conf文件
    11. cat  << EOF  >>  /etc/sysctl.conf 

      net.core.rmem_default = 262144

      net.core.rmem_max = 4194304

      net.core.wmem_default = 262144

      net.core.wmem_max = 1048576

      fs.aio-max-nr = 1048576

      fs.file-max = 6815744

      fs.aio-max-nr = 1048576

      kernel.panic_on_oops=1

      net.core.somaxconn = 8192

      net.ipv4.tcp_keepalive_time = 600 #默认7200

      net.ipv4.ip_local_port_range = 10000 65000 #默认32768到61000

      net.ipv4.tcp_max_syn_backlog = 8192 #默认1024

      net.ipv4.tcp_max_tw_buckets = 5000 #默认65535

      #vm.nr_hugepages = 81920 #share_pool /2MB *1.2

      kernel.sem = 4096 128000 64 512

      vm.swappiness=10

      EOF

       (2)加载内核参数:

      # sysctl -p

      1. 配置limit.conf
    12. #vi /etc/security/limits.conf

      #POSTGRESQL SETTING

      postgres soft nofile 1048576

      postgres hard nofile 1048576

      postgres soft nproc 131072

      postgres hard nproc 131072

      postgres soft stack 10240

      postgres hard stack 32768

      postgres soft core 6291456

      postgres hard core 6291456

      postgres soft memlock -1

      postgres hard memlock -1

      cat  << EOF  >>  /etc/security/limits.conf

      #POSTGRESQL SETTING

      postgres soft nofile 1048576

      postgres hard nofile 1048576

      postgres soft nproc 131072

      postgres hard nproc 131072

      postgres soft stack 10240

      postgres hard stack 32768

      postgres soft core 6291456

      postgres hard core 6291456

      postgres soft memlock -1

      postgres hard memlock -1

      EOF

      备注:修改前需要运行如下命令:sed -i  's/4096/unlimited/g' /etc/security/limits.d/20-nproc.conf

       

      1. 关闭TRANSPARENT HUGEPAGES
    13. 关闭前查看

      # cat /sys/kernel/mm/transparent_hugepage/enabled

      [always] madvise never

      关闭TRANSPARENT HUGEPAGES 实施

      #vi  /etc/default/grub

      在GRUB_CMDLINE_LINUX选项后追加参数transparent_hugepage=never:

      GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet transparent_hugepage=never"

      #grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

      说明:非/boot/efi模式,使用#grub2-mkconfig -o /boot/grub2/grub.cfg

      #重启系统后查看:

      cat /sys/kernel/mm/transparent_hugepage/enabled

      always madvise [never]

    14. 创建postgres用户及目录 
    • groupadd postgres

      useradd -g postgres  postgres

      # passwd postgres

      echo "Prod_postgres2025"|passwd --stdin postgres

      mkdir -p /data/pgdata

      mkdir -p /data/archivelog

      mkdir -p /data/pglogs

      mkdir -p /data/pgtbs

      mkdir -p /data/repmgr

      mkdir -p /data/pgwal

      chown -R postgres.postgres /data

    以root用户身份执行,在两节点上做如下配置。
      1. 设置环境变量
    1. 编辑/etc/profile文件,在文件末尾添加下述内容

      cat  << EOF  >>  /etc/profile 

      #POSTGRESQL SETTING

      PATH=\$PATH:/usr/pgsql-16/bin

      export PATH

      EOF

      编辑/home/postgres/.bash_profile

      cat  << EOF  >> /home/postgres/.bash_profile 

      #POSTGRESQL SETTING

      export PGPORT=5432

      export LANG=en_US.UTF8

      export PGDATA=/data/pgdata

      export PGHOME=/usr/pgsql-16/

      export PATH=\$PATH:\$PGHOME/bin

      EOF

    2. 安装postgresql
      1. 上传并解压介质
    3. 将所有安装介质上传到/home/postgres下:

      postgresql16-16.4-1PGDG.rhel8.x86_64.rpm

      postgresql16-contrib-16.4-1PGDG.rhel8.x86_64.rpm

      postgresql16-libs-16.4-1PGDG.rhel8.x86_64.rpm

      postgresql16-server-16.4-1PGDG.rhel8.x86_64.rpm

      1. 安装postgres
    4. yum install postgresql16-16.4-1PGDG.rhel8.x86_64.rpm  postgresql16-contrib-16.4-1PGDG.rhel8.x86_64.rpm  postgresql16-libs-16.4-1PGDG.rhel8.x86_64.rpm  postgresql16-server-16.4-1PGDG.rhel8.x86_64.rpm

      1. 初始化数据库
    5. su - postgres

      initdb -D /data/pgdata  --waldir=/data/pgwal  --wal-segsize=64

      编辑pg_hba.conf文件加入

      local   replication   repmgr                                    trust

      host    replication   repmgr            127.0.0.1/32            trust

      host    replication   repmgr            192.168.10.33/32         trust

      host    replication   repmgr            192.168.10.34/32         trust

      local   repmgr        repmgr                                    trust

      host    repmgr        repmgr            127.0.0.1/32            trust

      host    repmgr        repmgr            192.168.10.33/32         trust

      host    repmgr        repmgr            192.168.10.34/32         trust

      host     all            all             0.0.0.0/0                md5

      1. 编辑参数文件
    6. Vi /data/pgdata/postgresql.conf,在文件末尾加入如下内容

      ##general

      listen_addresses = '*'

      unix_socket_directories = '/run/postgresql, /tmp'

      max_connections = 300

      ##内存设置

      #取总内存一半

      shared_buffers=8GB

      #取总内存的0.75

      effective_cache_size =12GB

      work_mem=16MB

      temp_buffers=16MB

      #maintenance_work_mem一版按照该1GB分配50MB,如果小于1G,侧设置为1GB,对于autovacuum_work_mem默认值是-1,则使用maintenance_work_mem的设置值

      maintenance_work_mem=1GB

      ##设置log日志

      log_statement=ddl

      log_truncate_on_rotation = on

      log_checkpoints = on

      log_connections = on

      log_disconnections = on

      log_min_duration_statement = 10000

      log_directory='/data/pg_logs/'

      log_line_prefix = '%m %p %u %d %r %a '

      log_rotation_age = 91d

      log_rotation_size = 20MB

      log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

      log_file_mode = 0604

      ##指定用户数据保存表空间

      default_tablespace = pgtbs

      ##设置wal日志

      wal_level =replica

      wal_log_hints = on

      max_wal_size = 8GB
      min_wal_size = 1GB

      wal_keep_size=1GB

      max_standby_archive_delay = '300s'
      max_standby_streaming_delay = '300s'

      bgwriter_delay = '50ms'
      bgwriter_lru_maxpages = '500'
      bgwriter_lru_multiplier = '3.0'

      ##设置归档

      archive_mode = on

      #for pg14

      #archive_command = 'cp  %p  /data/archivelog/%f'

      #for pg16

      archive_library = 'basic_archive'

      basic_archive.archive_directory = '/data/archivelog/'

      archive_timeout = 900s

      #

      shared_preload_libraries='repmgr,pg_stat_statements,pg_buffercache,pg_prewarm,file_fdw ,pg_visibility ,pgcrypto ,pgrowlocks,uuid-ossp'

      1. 启动数据库
    7. pg_ctl   -l /data/pglogs/pg_log.log start

      1. 创建表空间及加载插件
    8. create tablespace pgtbs location '/data/pgtbs/';

      create extension pg_buffercache;

      create extension pg_stat_statements;

      CREATE EXTENSION pg_prewarm;

      CREATE EXTENSION file_fdw;

      CREATE EXTENSION pg_visibility;

      create extension pgcrypto;

      create extension pgrowlocks;

      create extension "uuid-ossp";

      1. 创建维护用户
    9. 为方便维护创建my_cf维护用户,用户平时查看,监控,备份

      CREATE USER mon_pg WITH PASSWORD 'password' INHERIT;

      GRANT pg_monitor TO mon_pg;

      创建repmgr同步用户

      create user repmgr with password ' password ' superuser replication;

      create database repmgr owner repmgr;

      alter user repmgr set search_path to repmgr, "$user", public;

    10. 配置集群
      1. 配置个节点ssh互信
    11. ssh-keygen -t rsa

      ssh-copy-id postgres@192.168.10.33

      ssh-copy-id postgres@192.168.10.34

      1. 安装同步插件repmgr
    12. yum install repmgr_16-5.4.1-1PGDG.rhel8.x86_64.rpm

      复制配置文件至指定目录

      cp /etc/repmgr/16/repmgr.conf  /data/repmgr/repmgr.conf

      1. 配置repmgr配置文件
    13. 在主从节点/data/repmgr/repmgr.conf尾部加入

      节点一:

      # leader节点

      node_id=1

      node_name='pwdrzhdb01'

      conninfo='host=192.168.10.33 port=5432 user=repmgr dbname=repmgr connect_timeout=2'

      data_directory='/data/pgdata'

      failover='automatic'

      promote_command='/usr/pgsql-16/bin/repmgr standby promote --config-file=/data/repmgr/repmgr.conf --log-to-file'

      follow_command='/usr/pgsql-16/bin/repmgr standby follow --config-file=/data/repmgr/repmgr.conf --log-to-file --upstream-node-id=%n'

      monitoring_history=yes

      connection_check_type=ping

      reconnect_attempts=6

      reconnect_interval=5

      standby_disconnect_on_failover=true

      repmgrd_pid_file='/data/repmgr/repmgrd.pid'

      repmgrd_service_start_command='/usr/pgsql-16/bin/repmgrd -f /data/repmgr/repmgr.conf start'

      repmgrd_service_stop_command='kill -9 `cat /data/repmgr/repmgrd.pid`'

      log_level=INFO

      log_file='/data/repmgr/repmgrd.log'

      log_status_interval=10

      节点二:

      # standby节点

      node_id=2

      node_name='pwdrzhdb02'

      conninfo='host=192.168.10.34 port=5432 user=repmgr dbname=repmgr connect_timeout=2'

      data_directory='/data/pgdata'

      failover='automatic'

      promote_command='/usr/pgsql-16/bin/repmgr standby promote --config-file=/data/repmgr/repmgr.conf --log-to-file'

      follow_command='/usr/pgsql-16/bin/repmgr standby follow --config-file=/data/repmgr/repmgr.conf --log-to-file --upstream-node-id=%n'

      monitoring_history=yes

      connection_check_type=ping

      reconnect_attempts=6

      reconnect_interval=5

      standby_disconnect_on_failover=true

      repmgrd_pid_file='/data/repmgr/repmgrd.pid'

      repmgrd_service_start_command='/usr/pgsql-16/bin/repmgrd -f /data/repmgr/repmgr.conf start'

      repmgrd_service_stop_command='kill -9 `cat /data/repmgr/repmgrd.pid`'

      log_level=INFO

      log_file='/data/repmgr/repmgrd.log'

      log_status_interval=10

      1. 注册主节点
    14. 在主节点执行

      repmgr -f /data/repmgr/repmgr.conf primary register

      1. 克隆备库
    15. 在备节点执行

      测试备库是否可以克隆

      repmgr -h 192.168.10.33 -U repmgr -d repmgr -f  /data/repmgr/repmgr.conf  standby clone --dry-run

      无报错即可开始克隆

      repmgr -h 192.168.10.33 -U repmgr -d repmgr -f  /data/repmgr/repmgr.conf  standby clone

      1. 启动备库
    16. pg_ctl   -l /data/pglogs/pg_log.log start

      1. 注册备库
    17. repmgr -f /data/repmgr/repmgr.conf standby register

      1. 启动自动切换监控进程
    18. 主备节点都运行

      repmgr -f /data/repmgr/repmgr.conf  daemon start

      1. 常用命令
    19. 检测集群状态

      [postgres@pwbrzhdb01:/data/repmgr]$repmgr -f /data/repmgr/repmgr.conf  cluster show

       ID | Name       | Role    | Status    | Upstream   | Location | Priority | Timeline | Connection string                                                                          

      ----+------------+---------+-----------+------------+----------+----------+----------+---------------------------------------------------------------------------------------------

       1  | pwdrzhdb01 | primary | * running |            | default  | 100      | 3        | host=192.168.10.33 port=5432 user=repmgr password=repmgr_123 dbname=repmgr connect_timeout=2

       2  | pwdrzhdb02 | standby |   running | pwdrzhdb01 | default  | 100      | 3        | host=192.168.10.34 port=5432 user=repmgr  dbname=repmgr connect_timeout=2

      主从节点切换

      [postgres@pwbrzhdb02:/data]$repmgr -f /data/repmgr/repmgr.conf  standby switchover

      NOTICE: executing switchover on node "pwdrzhdb02" (ID: 2)

      NOTICE: attempting to pause repmgrd on 2 nodes

      NOTICE: local node "pwdrzhdb02" (ID: 2) will be promoted to primary; current primary "pwdrzhdb01" (ID: 1) will be demoted to standby

      NOTICE: stopping current primary node "pwdrzhdb01" (ID: 1)

      NOTICE: issuing CHECKPOINT on node "pwdrzhdb01" (ID: 1)

      DETAIL: executing server command "/usr/pgsql-14/bin/pg_ctl  -D '/data/pgdata' -W -m fast stop"

      INFO: checking for primary shutdown; 1 of 60 attempts ("shutdown_check_timeout")

      INFO: checking for primary shutdown; 2 of 60 attempts ("shutdown_check_timeout")

      NOTICE: current primary has been cleanly shut down at location 0/1C000028

      NOTICE: promoting standby to primary

      DETAIL: promoting server "pwdrzhdb02" (ID: 2) using pg_promote()

      NOTICE: waiting up to 60 seconds (parameter "promote_check_timeout") for promotion to complete

      NOTICE: STANDBY PROMOTE successful

      DETAIL: server "pwdrzhdb02" (ID: 2) was successfully promoted to primary

      NOTICE: node "pwdrzhdb02" (ID: 2) promoted to primary, node "pwdrzhdb01" (ID: 1) demoted to standby

      NOTICE: switchover was successful

      DETAIL: node "pwdrzhdb02" is now primary and node "pwdrzhdb01" is attached as standby

      NOTICE: STANDBY SWITCHOVER has completed successfully

      [postgres@pwbrzhdb02:/data]$

      检测node状态:

      [postgres@pwbrzhdb02:/data]$repmgr -f /data/repmgr/repmgr.conf  node status

      Node "pwdrzhdb02":

         PostgreSQL version: 14.11

         Total data size: 34 MB

         Conninfo: host=192.168.10.34 port=5432 user=repmgr  dbname=repmgr connect_timeout=2

         Role: primary

         WAL archiving: enabled

         Archive command: cp %p /data/archivelog/%f

         WALs pending archiving: 0 pending files

         Replication connections: 1 (of maximal 10)

         Replication slots: 0 physical (of maximal 10; 0 missing)

         Replication lag: n/a

      检测node复制状态

      [postgres@pwbrzhdb01:/data/repmgr]$repmgr -f /data/repmgr/repmgr.conf  node check

      Node "pwdrzhdb01":

         Server role: OK (node is standby)

         Replication lag: OK (0 seconds)

         WAL archiving: OK (0 pending archive ready files)

         Upstream connection: OK (node "pwdrzhdb01" (ID: 1) is attached to expected upstream node "pwdrzhdb02" (ID: 2))

         Downstream servers: OK (this node has no downstream nodes)

         Replication slots: OK (node has no physical replication slots)

         Missing physical replication slots: OK (node has no missing physical replication slots)

         Configured data directory: OK (configured "data_directory" is "/data/pgdata")

相关文章:

  • 如何在 IntelliJ IDEA 中配置并调用虚拟机 HDFS
  • uniapp微信小程序一键授权登录
  • AI数字人融合VR全景:开启未来营销与交互新篇章
  • 无人机动力系统全解析:核心组件、工作原理与实用指南
  • shell脚本练习(6):备份MySQL数据库表
  • MH22D3开发高级UI应用,适配arm2d驱动
  • 高效管理多后端服务:Nginx 配置与实践指南
  • 兼顾长、短视频任务的无人机具身理解!AirVista-II:面向动态场景语义理解的无人机具身智能体系统
  • ssh快速连接服务器终端配置
  • pyenv简单的Python版本管理器(macOS版)
  • HarmonyOs开发之———UIAbility进阶
  • #跟着若城学鸿蒙# web篇-初探
  • 关于NLP自然语言处理的简单总结
  • AgenticSeek开源的完全本地的 Manus AI。无需 API,享受一个自主代理,它可以思考、浏览 Web 和编码,只需支付电费。
  • PyTorch中.item()函数:提取单元素张量值
  • 自用Vscode 配置c++ debug环境
  • OpenCV图像旋转原理及示例
  • Ubuntu Linux bash的相关默认配置文件内容 .profile .bashrc, /etc/profile, /etc/bash.bashrc等
  • pytorch中各种乘法操作
  • OpenCV CUDA模块中逐元素操作------数学函数
  • 受贿1.29亿余元,黑龙江省原副省长王一新被判无期
  • 习近平会见智利总统博里奇
  • 京东CEO许冉:外卖日单量接近2000万单,看到外卖对平台拉动和转化效应
  • 福州千余公共道路泊车位装“智能地锁”续:运营公司被责令改正并罚款
  • 商务部召开外贸企业圆桌会:全力为外贸企业纾困解难,提供更多支持
  • 93岁南开退休教授陈生玺逝世,代表作《明清易代史独见》多次再版