postgresql的hot standby(replication stream)

       PG在9.*版本後熱備提供了新的一個功能,那就是Stream Replication的讀寫分離,是PG高可用性的一個典型應用,也就是咱們傳統意義上說的Hot-Standby,好比Oracle的DG,mssql的mirror以及Mysql的讀寫分離等,與其餘數據庫相比較,有相同點,也有不一樣點,這些後述。下面是PG的流複製的安裝步驟以及測試。 sql

環境:
Vmware Workstation 8.0
操做系統:CentOS 6.2
數據庫   :PostgreSQL 9.1.3

虛擬主機2臺
MASTER:  192.168.2.130
 SLAVE:    192.168.2.129 數據庫

環境參數
[postgres@localhost ~]$ echo $PGHOME  
 /home/postgres

[postgres@localhost ~]$ echo $PGDATA
/database/pgdata bash

Step1:安裝PG數據庫
略,slave端能夠只裝數據庫,不初始化數據庫

Step2:建立流複製用戶

master端執行: CREATE USER repuser replication LOGIN CONNECTION LIMIT 3 ENCRYPTED PASSWORD 'repuser';  服務器

Step3:配置Master端的訪問文件pg_hba.conf post

增長一行:
host replication repuser 192.168.2.129/16 md5

Step4:配置MASTER端配置文件
測試

max_wal_senders = 1
wal_level = hot_standby

archive_mode = on 
archive_command = 'cd ./'

hot_standby = on
wal_keep_segments = 64 spa

  注:max_wal_senders是Slave庫的節點數,有多少個slave庫就設多少,
      wal_level是write ahead log參數值,設置流複製務必將此值更新成hot_standby
     wal_keep_segments默認值是16,是PG_XLOG下的日誌文件數相關參數
    archive也能夠選擇關閉,歸檔是定時恢復用的,流複製不是必須的

Step5:主庫備份(Master端)
5.1:開啓文件備份,前提是wal_level參數值必須是archive或者hot_standby,前面已作
select pg_start_backup('Replition work');

5.2:拷貝$PGDATA文件,並複製到Slave服務器上,排除pg_xlog內容,由於在slave仍是要被清掉的,另外注意TAR打包時的權限問題,本次打包時遇到一個問題: 操作系統

tar報錯

tar (child):  Cannot open: Permission denied
tar: Error is not recoverable: exiting now,後來將/database的權限授予了chown postgres解決.

打包
tar czvf pgdata.tar.gz pgdata --exclude=pgdata/pg_xlog 

備機端若是已經安裝了postgres數據庫,數據文件夾名稱同樣的話,能夠先停掉備機數據庫,更名數據文件夾名稱
mv pgdata pgdata.old

遠程拷貝至slave端並在備機端解壓
scp pgdata.tar.gz root@192.168.2.129:/database/
tar xzvf pgdata.tar.gz .net

 5.3:上述步驟完成後,結束master端的備份
select pg_stop_backup(), current_timestamp;

Step6:修改Slave端配置信息

6.1:postgresql.conf文件
 hot_standby = on 日誌

6.2:recovery.conf文件
$ cp $PGHOME/share/recovery.conf.sample $PGDATA/recovery.conf
$ vi recovery.conf  --新增如下三行
 standby_mode = 'on'
 trigger_file = '/database/pgdata/postgresql.trigger.1949'
 primary_conninfo = 'host=192.168.2.130 port=1949 user=repuser password=repuser keepalives_idle=60'

6.3:配置.pgpass文件(slave端)
新增slave訪問master的密碼文件,能夠不用輸密碼
192.168.2.130:1949:postgres:repuser:repuser

6.4:刪除slave端(從master端拷過來的)的pid文件和pg_xlog
$ rm -rf  $PGDATA/pg_xlog
$ rm -f $PGDATA/postmaster.pid
$ mkdir  $PGDATA/pg_xlog

Step7:啓動Slave庫
正常啓動備庫(pg_ctl -D $PGDATA -l pg.log start),有異常能夠看log
複製完成後,能夠經過CSV日誌去查看,本處未設,直接查看進程

7.1 查看master進程:

[postgres@localhost ~]$ ps -ef|grep postgres 
root      2454  2438  0 20:25 pts/0    00:00:00 su - postgres
postgres  2461  2454  0 20:25 pts/0    00:00:00 -bash
postgres  2535     1  0 20:26 pts/1    00:00:00 /home/postgres/bin/postgres -D /database/pgdata
postgres  2537  2535  0 20:26 ?        00:00:00 postgres: writer process                       
postgres  2538  2535  0 20:26 ?        00:00:00 postgres: wal writer process                   
postgres  2539  2535  0 20:26 ?        00:00:00 postgres: autovacuum launcher process          
postgres  2540  2535  0 20:26 ?        00:00:00 postgres: archiver process                     
postgres  2541  2535  0 20:26 ?        00:00:00 postgres: stats collector process              
postgres  3079  2535  0 21:56 ?        00:00:00 postgres: wal sender process repuser 192.168.2.129(45446) streaming 0/C01EDB8
postgres  3116  2535  0 22:02 ?        00:00:00 postgres: postgres postgres 192.168.2.1(52648) idle
postgres  3118  2535  0 22:02 ?        00:00:00 postgres: postgres test 192.168.2.1(52649) idle
postgres  3120  2535  0 22:02 ?        00:00:00 postgres: postgres test 192.168.2.1(52654) idle
root      3126  2490  0 22:04 pts/1    00:00:00 su - postgres
postgres  3214  3128  0 22:16 pts/1    00:00:00 grep postgres
postgres  3128  3126  0 22:04 pts/1    00:00:00 -bash
postgres  3213  3128  2 22:16 pts/1    00:00:00 ps -ef

7.2 查看slave進程:

[postgres@localhost ~]$ ps -ef|grep postgres
postgres  2856     1  0 21:54 pts/2    00:00:00 /home/postgres/bin/postgres -D /database/pgdata
postgres  2858  2856  0 21:54 ?        00:00:00 postgres: startup process   recovering 000000010000000000000003
postgres  2859  2856  0 21:54 ?        00:00:00 postgres: writer process                       
postgres  2860  2856  0 21:54 ?        00:00:00 postgres: stats collector process              
postgres  2899  2856  0 21:56 ?        00:00:00 postgres: wal receiver process   streaming 0/C01ED28
postgres  3007  2856  0 22:02 ?        00:00:00 postgres: postgres postgres 192.168.2.1(52652) idle
postgres  3013  2856  0 22:03 ?        00:00:00 postgres: postgres test 192.168.2.1(52657) idle
postgres  3014  2856  0 22:03 ?        00:00:00 postgres: postgres test 192.168.2.1(52658) idle
root      3020  2756  0 22:04 pts/2    00:00:00 su - postgres
postgres  3022  3020  0 22:04 pts/2    00:00:00 -bash
postgres  3091  3022  4 22:15 pts/2    00:00:00 ps -ef
postgres  3092  3022  0 22:15 pts/2    00:00:00 grep postgres

此時在slave端的pg_xlog下面也產生了日誌文件,而且以前pg_start_backup生成的文件名也變成了old的了.

查看日誌內容:
[postgres@localhost ~]$ more pgsql.log 
LOG:  database system was shut down in recovery at 2012-04-23 18:33:25 PDT
LOG:  entering standby mode
LOG:  streaming replication successfully connected to primary
LOG:  redo starts at 0/8000020
LOG:  consistent recovery state reached at 0/C000000
LOG:  database system is ready to accept read only connections
Step8:測試
On Master:
test=# select * from kenyon;
 id |  name  
----+--------
  2 | kenyon
(1 row)

test=# insert into kenyon values (2,'kenyon testing data');
INSERT 0 1
test=# 

On Slave:
test=# select * from kenyon;
 id |        name         
----+---------------------
  2 | kenyon
  2 | kenyon testing data
(2 rows)

test=# delete from kenyon where id = 2;
ERROR:  cannot execute DELETE in a read-only transaction
新增的數據已經傳輸過去,而且slave端的會話是隻讀的。
相關文章
相關標籤/搜索