postgresql-11主從復制(流復制)部署


  • 主從介紹

    • PostgreSQL流復制默認是異步的。如果主服務器崩潰,則某些已被提交的事務可能還沒有被復制到后備服務器,這會導致數據丟失。數據的丟失量與故障轉移時的復制延遲成比例。
    • 同步復制能夠保證一個事務的所有修改都能被傳送到一台或者多台同步后備服務器。這擴大了由一次事務提交所提供的標准持久化級別。在計算機科學理論中這種保護級別被稱為 2-safe 復制。而當synchronous_commit被設置為remote_write時,則是 group-1-safe (group-safe 和 1-safe)。
    • 在請求同步復制時,一個寫事務的每次提交將一直等待,直到收到一個確認表明該提交在主服務器和后備服務器上都已經被寫入到磁盤上的預寫式日志中。數據會被丟失的唯一可能性是主服務器和后備服務器在同一時間都崩潰。這可以提供更高級別的持久性,盡管只有系統管理員要關系兩台服務器的放置和管理。等待確認提高了用戶對於修改不會丟失的信心,但是同時也不必要地增加了對請求事務的響應時間。最小等待時間是在主服務器和后備服務器之間的來回時間。
    • 只讀事務和事務回滾不需要等待后備服務器的回復。子事務提交也不需要等待后備服務器的響應,只有頂層提交才需要等待。長時間運行的動作(如數據載入或索引構建)不會等待最后的提交消息。所有兩階段提交動作要求提交等待,包括預備和提交。
    • 同步后備可以是物理復制后備或者是邏輯復制訂閱者。它還可以是任何其他物理或者邏輯WAL復制流的消費者,它懂得如何發送恰當的反饋消息。除內建的物理和邏輯復制系統之外,還包括pg_receivewalpg_recvlogical之類的特殊程序,以及一些第三方復制系統和定制程序。同步復制支持的細節請查看相應的文檔。
  • 主從機器分配

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

         

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

 

主從切換詳情請查閱 https://www.cnblogs.com/miclis/p/10480979.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM