postgresql 9.4 流複製簡單配置

selectshen發表於2015-12-18
#環境
postgresql:9.4.5

host:ct6606 192.108.56.122 master
     ct6607    192.108.56.123 slave

#安裝postgresql
    #在ct6606,ct6607分別執行以下
    [root@ct6606 ~]# ll postgresql-9.4.5-1-linux-x64.run
    -rwxr--r--. 1 root root 37802126 Nov  5  2015 postgresql-9.4.5-1-linux-x64.run

    [root@ct6606 ~]# ./postgresql-9.4.5-1-linux-x64.run
    ----------------------------------------------------------------------------
    Welcome to the PostgreSQL Setup Wizard.
    ----------------------------------------------------------------------------
    Please specify the directory where PostgreSQL will be installed.
    Installation Directory [/opt/PostgreSQL/9.4]:
    ----------------------------------------------------------------------------
    Please select a directory under which to store your data.
    Data Directory [/opt/PostgreSQL/9.4/data]:
    ----------------------------------------------------------------------------
    Please provide a password for the database superuser (postgres). A locked Unix
    user account (postgres) will be created if not present.
    Password :
    Retype password :
    ----------------------------------------------------------------------------
    Please select the port number the server should listen on.
    Port [5432]:
    ----------------------------------------------------------------------------
    Advanced Options
    Select the locale to be used by the new database cluster.
    Locale
    [1] [Default locale]
    ...
    [707] zh_CN
    [708] zh_CN.gb2312
    [709] zh_CN.utf8
    ...
    Please choose an option [1] : 709
    ----------------------------------------------------------------------------
    Setup is now ready to begin installing PostgreSQL on your computer.
    Do you want to continue? [Y/n]: y
    ----------------------------------------------------------------------------
    Please wait while Setup installs PostgreSQL on your computer.
     Installing
     0% ______________ 50% ______________ 100%
     #########################################
    ----------------------------------------------------------------------------
    Setup has finished installing PostgreSQL on your computer.

    [root@ct6606 ~]# id postgres
    uid=500(postgres) gid=500(postgres) groups=500(postgres)

    [root@ct6606 ~]# ps -ef|grep postgre|grep -v grep
    postgres 25195     1  0 09:02 ?        00:00:00 /opt/PostgreSQL/9.4/bin/postgres -D /opt/PostgreSQL/9.4/data
    postgres 25197 25195  0 09:02 ?        00:00:00 postgres: logger process        
    postgres 25199 25195  0 09:02 ?        00:00:00 postgres: checkpointer process  
    postgres 25200 25195  0 09:02 ?        00:00:00 postgres: writer process        
    postgres 25201 25195  0 09:02 ?        00:00:00 postgres: wal writer process    
    postgres 25202 25195  0 09:02 ?        00:00:00 postgres: autovacuum launcher process
    postgres 25203 25195  0 09:02 ?        00:00:00 postgres: stats collector process

    [root@ct6606 ~]# echo "export PATH=\$PATH:/opt/PostgreSQL/9.4/bin" >>/root/.bash_profile
    [root@ct6606 ~]# . /root/.bash_profile
    [root@ct6606 ~]# mv /etc/init.d/postgresql-9.4 /etc/init.d/postgresql


#配置postgresql
    #在ct6606執行以下
    #修改引數
        [root@ct6606 ~]# cd /opt/PostgreSQL/9.4/data/
        [root@ct6606 data]# vi postgresql.conf
        加入:
        wal_level=hot_standby
        max_wal_senders=1
        hot_standby=on
        [root@ct6606 data]# vi pg_hba.conf
        修改:
        host    all             all             127.0.0.1/32            md5為:
        host    all             all             0.0.0.0/0            md5
        加入:
        host    replication     repuser        0.0.0.0/0            md5

        [root@ct6606 data]# service postgresql restart

        #建replication使用者,備份,傳輸
        [root@ct6606 data]# psql -U postgres
        postgres=# create user repuser replication login connection limit 5 encrypted password 'system';
        postgres=# select pg_start_backup('replication');
        postgres=# \q

        [root@ct6606 data]# cd /opt/PostgreSQL/9.4/
        [root@ct6606 9.4]# tar -czvf data.tar.gz data --exclude=data/pg_xlog/*

        [root@ct6606 data]# psql -U postgres
        postgres=# select pg_stop_backup(), current_timestamp;
        postgres=# select pg_switch_xlog();
        postgres=# \q


        [root@ct6606 9.4]# scp data.tar.gz 192.108.56.123:/opt/PostgreSQL/9.4/


    #在ct6607執行以下
    #解壓,修改引數,啟動
        [root@ct6607 ~]# service postgresql stop
        [root@ct6607 ~]# cd /opt/PostgreSQL/9.4/
        [root@ct6607 9.4]# rm -rf data
        [root@ct6607 9.4]# tar -xzvf data.tar.gz
        [root@ct6607 9.4]# cd data
        [root@ct6607 data]# cp ../share/postgresql/recovery.conf.sample recovery.conf
        [root@ct6607 data]# vi recovery.conf
        加入:
        standby_mode=on
        trigger_file='/opt/PostgreSQL/9.4/data/pg.trigger'
        primary_conninfo='host=192.108.56.122 port=5432 user=repuser password=system keepalives_idle=60'

        [root@ct6607 data]# vi .pgpass
        加入:
        192.108.56.122:5432:postgres:repuser:system

        [root@ct6607 data]# service postgresql start

#測試
    在master端會多出postgres: wal sender process 程式
    在slave端會多出postgres: wal receiver process 程式

    [root@ct6606 9.4]# psql -U postgres -h 192.108.56.122
    postgres=# create table tb01 as select 1 a;
    SELECT 1
    postgres-# \q

    [root@ct6606 9.4]# psql -U postgres -h 192.108.56.123
    postgres=# select * from tb01;
     a
    ---
     1
    (1 row)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28539951/viewspace-1874775/,如需轉載,請註明出處,否則將追究法律責任。

相關文章