1. 程式人生 > 實用技巧 >postgresql-11主從複製(流複製)部署

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 行記錄)

    • 可以建立一個數據庫自行驗證