CentOS 7のPostgreSQLプライマリ・ストリーム・レプリケーションの導入

23592 ワード

CentOS 7のPostgreSQLプライマリ・ストリーム・レプリケーションの導入
  • 一、準備環境
  • 二、ホストデータベース配置
  • 三、マスタデータベース構成
  • 3.1メインライブラリ構成
  • 3.2ライブラリ構成
  • から
  • 四、主従状態検査
  • 一、環境の準備
    オペレーティングシステム:CentOS Linux release 7.4データベースバージョン:PostgreSQL 9.6.12マスターライブラリ:192.168.189.152スレーブライブラリ:192.168.189.153
    二、ホストデータベースの配置
    次の手順は、2つのノードで行います.プライマリ・ライブラリにはデータベース・ソフトウェアのインストールとデータベースの初期化が必要です.ライブラリからデータベース・ソフトウェアのインストールだけで初期化は必要ありません.1、RPMソースの追加
    [root@localhost opt]# yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
    

    2、yumを使用してpostgresqlを直接インストールする
    [root@localhost opt]# yum -y install postgresql96-server postgresql96-contrib postgresql96-devel
    

    3、postgresqlファイルディレクトリおよびユーザーグループの作成
            
    [root@localhost opt]# mkdir -p /data/pgdata/
    
      pg   
    [root@localhost opt]# groupadd pg
    
    [root@localhost opt]# useradd -g pg pg
    
               
    [root@localhost opt]# chown -R pg:pg /data/
    [root@localhost opt]# chown -R pg:pg /var/run/postgresql
    

    4、データベースの初期化 : ,
    [pg@localhost ~]$ /usr/pgsql-9.6/bin/initdb -D /data/pgdata/
    

    初期化プロセス:
    The files belonging to this database system will be owned by user "pg".
    This user must also own the server process.
    
    The database cluster will be initialized with locale "en_US.UTF-8".
    The default database encoding has accordingly been set to "UTF8".
    The default text search configuration will be set to "english".
    
    Data page checksums are disabled.
    
    fixing permissions on existing directory /data/pgdata ... ok
    creating subdirectories ... ok
    selecting default max_connections ... 100
    selecting default shared_buffers ... 128MB
    selecting dynamic shared memory implementation ... posix
    creating configuration files ... ok
    running bootstrap script ... ok
    performing post-bootstrap initialization ... ok
    syncing data to disk ... ok
    
    WARNING: enabling "trust" authentication for local connections
    You can change this by editing pg_hba.conf or using the option -A, or
    --auth-local and --auth-host, the next time you run initdb.
    
    Success. You can now start the database server using:
    

    初期化完了後にデータベースを起動
    -->/usr/pgsql-9.6/bin/pg_ctl -D /data/pgdata/ -l logfile start
    
    [postgres@localhost pgsql]$ pg_ctl stop -D /mydata/pgdata/

    5、環境変数の追加
    [pg@localhost ~]$ vi ~/.bash_profile    
    PGDATA=/data/pgdata
    PGHOST=127.0.0.1
    PGDATABASE=postgres
    PGUSER=pg
    PGPORT=5432
    PATH=/usr/pgsql-9.6/bin:$PATH
    export PATH
    export PGDATA PGHOST PGDATABASE PGUSER PGPORT
    export TMOUT=1000
    export LD_LIBRARY_PATH=/usr/pgsql-9.6/lib
    

    手順6、7メインライブラリでのみ操作
    6.リスニング構成の変更
    次のリスニングパラメータを変更します.リスニングは、任意のアドレスの使用を許可します.
    [pg@localhost ~]$ vi /data/pgdata/postgresql.conf
    listen_addresses = '*'
    

    7.データベースへのアクセスを許可するセグメントを追加する
     127.0.0.1       
     [pg@localhost ~]$ vi /data/pgdata/pg_hba.conf 
     # IPv4 local connections:
    host    all             all             127.0.0.1/32        trust
        —->host    all             all             0.0.0.0/0            trust
    

    三、マスターデータベースの構成
    3.1マスターライブラリ構成
    (1)主従同期に特化したレプリケーションユーザの作成
    [root@localhost ~]# su - pg
    [pg@localhost ~]$ psql
    postgres=# create user rpl superuser password '11111';
    

    (2)マスターライブラリ上の構成ライブラリで許可されているセグメントからライブラリセグメントへの追加
    [pg@localhost ~]$  vi /data/pgdata/pg_hba.conf
    host    replication     rpl            192.168.0.0/0         md5
    

    (3)マスターライブラリパラメータファイルの修正
    [pg@localhost ~]$ vi /data/pgdata/postgresql.conf
    wal_level = hot_standby   -->     
    max_wal_senders=2	  -->         
    wal_keep_segments =64-->xlog          wal    ,           。
    max_connections = 1000 -->       
    

    (4)マスターライブラリサービスの再起動
    [pg@localhost ~]$  pg_ctl  -D /data/pgdata/ -l logfile restart
    

    3.2ライブラリからの構成
    (1)ベース・バックアップは、ライブラリからプライマリ・ライブラリのデータ・ファイルをローカルにコピーする必要がなく、pg_を使用するbasebackupは、プライマリ・ライブラリのデータ・ファイルをネットワークを介してローカルにコピーし、ディレクトリ構造を含むことができます. : ,
    [pg@localhost ~]$ pg_basebackup -h 192.168.212.152 -p 5432 -U rpl -F p -x -P -R -D /data/pgdata
    Password: 
    46694/46694 kB (100%), 1/1 tablespace
        
    

    (2)ライブラリプロファイルからの構成
    [pg@localhost ~]$ vi /data/pgdata/postgresql.conf
           wal_level,max_wal_senders,wal_keep_segments
    
      
    hot_standby = on ———>         
    max_standby_streaming_delay = 30s -->              
    wal_receiver_status_interval = 10s -->              
    hot_standby_feedback = off -->              
    max_connections = 1500 -->        
    

    3)リカバリファイルrecovery.conf recovery.confをマスターライブラリに作成し、ライブラリから切り替えたときのパラメータ構成.ライブラリからrecovery.confファイルコピー可能なテンプレートがない場合
    [pg@localhost pgdata]$ cp /usr/pgsql-9.6/share/recovery.conf.sample /data/pgdata/recovery.conf
    
    [pg@localhost pgdata]$ vi /data/pgdata/recovery.conf
        :
    recovery_target_timeline = 'latest'  moren
    standby_mode = on
    primary_conninfo = 'host=192.168.212.152 port=5432 user=rpl password=11111'
    

    (4)スレーブサービスの開始
    [pg@localhost pgdata]$ pg_ctl -D /data/pgdata/ -l logfile start
    

    四、主従状態検査
        sender    
    [root@localhost ~]# ps -ef|grep "wal sender process"
    pg        7031  7015  0 17:04 ?        00:00:00 postgres: wal sender process rpl 192.168.212.153(49562) streaming 0/50001E8
    
        receiver    
    [root@localhost ~]# ps -ef|grep "wal receiver"
    pg        2360  2354  0 17:04 ?        00:00:00 postgres: wal receiver process   streaming 0/50002C8
    
    
    psql        
    postgres=# select usename,application_name,client_addr,state,sync_state from  pg_stat_replication; 
     usename | application_name |   client_addr   |   state   | sync_state 
    ---------+------------------+-----------------+-----------+------------
     rpl    | walreceiver      | 192.168.212.153 | streaming | async-->  
    (1 row)
    
          
    [pg@localhost ~]$ pg_controldata /data/pgdata/|grep "Database cluster state"
    Database cluster state:               in production
    
          
    [pg@localhost ~]$ pg_controldata /data/pgdata/|grep "Database cluster state"
    Database cluster state:               in archive recovery
    

    マスタスレーブデータ同期検証マスタライブラリ構築テーブル、スレーブライブラリクエリーテスト
      :
    pg=# insert into test values(1);
    
    pg=# select * from test;        
     id 
    ----
      1
    (1 row)
    
      :
    pg=# insert into test values(2);
    ERROR:  cannot execute INSERT in a read-only transaction
    pg=# select * from test;        
     id 
    ----
      1
    (1 row)