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 $PGDATA
/database/pgdata bash
master端執行: CREATE USER repuser replication LOGIN CONNECTION LIMIT 3 ENCRYPTED PASSWORD 'repuser'; 服務器
Step3:配置Master端的訪問文件pg_hba.conf post
增長一行: Step4:配置MASTER端配置文件
測試
max_wal_senders = 1注:max_wal_senders是Slave庫的節點數,有多少個slave庫就設多少,
wal_level = hot_standbyarchive_mode = on
archive_command = 'cd ./'
hot_standby = on
wal_keep_segments = 64 spa
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
6.1:postgresql.conf文件
hot_standby = on 日誌
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