postgresql-11主从复制(流复制)部署


  • 主从机器分配

    • IP地址 DB版本 主从关系
      192.168.63.134 11.6
      192.168.63.141 11.6
  •  

    安装postgresql

    • yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y yum install postgresql11 -y yum install postgresql11-server -y

       

  • 主库配置

    • 主库初始化

      • /usr/pgsql-11/bin/postgresql-11-setup initdb
        systemctl start postgresql-11

         

    • 创建复制用户,进行主从同步使用
      • [root@localhost data]# sudo -i -u postgres -bash-4.2$ psql psql (11.6) 输入 "help" 来获取帮助信息. postgres=# create role repl login replication encrypted password '123456'; CREATE ROLE
    • 主库上配置从库采用repl账号
      • vim /var/lib/pgsql/11/data/pg_hba.conf #只需要台添加下面两行,repl是用来做备份的用户,后面的192.168.63.0/24是该网段内的IP地址 host replication repl 192.168.63.0/24 md5 host all repl 192.168.63.0/24 trust vim /var/lib/pgsql/11/data/postgresql.conf listen_addresses = '*' # what IP address(es) to listen on; port = 5432 # (change requires restart) max_connections = 512 # (change requires restart) shared_buffers = 128MB # min 128kB dynamic_shared_memory_type = posix # the default is the first option wal_level = hot_standby # minimal, replica, or logical archive_mode = on # enables archiving; off, on, or always archive_command = 'cp %p /var/lib/pgsql/11/data/pg_archive/%f' # command to use to archive a logfile segment max_wal_senders = 6 # max number of walsender processes wal_keep_segments = 10240 # in logfile segments, 16MB each; 0 disables wal_sender_timeout = 60s # in milliseconds; 0 disables log_directory = 'log' # directory where log files are written

         

      • 修改完,要创建刚刚配置的一些目录结构:
      • mkdir /var/lib/pgsql/11/data/pg_archive/
        chown -R postgres.postgres /var/lib/pgsql/11/data

         

      • 重启主库服务
      • systemctl restart postgresql-11

         

  • 从库配置

    • 从库安装完成后,不初始化,若已经初始化,删除其data目录
    • #把主节点所有的数据文件都会拷贝过来
      [root@localhost ~]# pg_basebackup -h 192.168.63.134 -U repl -D /var/lib/pgsql/11/data/ -X stream -P 口令: 25312/25312 kB (100%), 1/1 表空间 [root@localhost ~]# ls /var/lib/pgsql/12/data/ backup_label current_logfiles log pg_commit_ts pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf base global pg_archive pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgresql.conf

       

    • 从库配置文件,根据下面的配置进行修改。
      [root@localhost ~]# vim /var/lib/pgsql/11/data/postgresql.conf listen_addresses = '*' # what IP address(es) to listen on; port = 5432 # (change requires restart) max_connections = 1000 # (change requires restart) shared_buffers = 128MB # min 128kB dynamic_shared_memory_type = posix # the default is the first option wal_level = replica # minimal, replica, or logical archive_mode = on # enables archiving; off, on, or always archive_command = 'cp %p /var/lib/pgsql/12/data/pg_archive/%f' # command to use to archive a logfile segment wal_sender_timeout = 60s # in milliseconds; 0 disables hot_standby = on # "on" allows queries during recovery max_standby_streaming_delay = 30s # max delay before canceling queries wal_receiver_status_interval = 10s # send replies at least this often hot_standby_feedback = on # send info from standby to prevent log_directory = 'log' # directory where log files are written,

       

    • 创建恢复文件recovery.conf。
      • [root@localhost 11]# cp /usr/pgsql-11/share/recovery.conf.sample /var/lib/pgsql/11/data/recovery.conf [root@localhost 11]# vim /var/lib/pgsql/11/data/recovery.conf # 调整参数: recovery_target_timeline = 'latest' #同步到最新数据 standby_mode = on #指明从库身份
        trigger_file = 'failover.now' primary_conninfo = 'host=192.168.63.134 port=5432 user=repl password=123456'  #连接到主库信息

         

      • 启动从库
        systemctl start postgresql-11

         

    • 验证主从配置
      • 在主库上运行以下命令
        postgres=# select client_addr,sync_state from pg_stat_replication; client_addr | sync_state ----------------+------------ 192.168.63.141 | async (1 行记录)

         

    • 可以创建一个数据库自行验证


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM