1. 安裝PG數(shù)據(jù)庫(kù)安裝過(guò)程略。注:slave端可以只裝數(shù)據(jù)庫(kù),,不初始化數(shù)據(jù)庫(kù) 2. 創(chuàng)建流復(fù)制用戶(hù)master端執(zhí)行 CREATE USER repuser replication LOGIN CONNECTION LIMIT 2 ENCRYPTED PASSWORD 'repuser'; 3. 配置Master端的訪問(wèn)文件vi pg_hba.conf 增加一行: host replication repuser 192.168.2.129/16 md5 4. 配置MASTER端配置文件listen_addresses = '*' max_wal_senders = 1 wal_level = hot_standby archive_mode = on archive_command = 'cd ./' wal_keep_segments = 64 注:max_wal_senders是Slave庫(kù)的節(jié)點(diǎn)數(shù),,有多少個(gè)slave庫(kù)就設(shè)多少, wal_level是write ahead log參數(shù)值,設(shè)置流復(fù)制務(wù)必將此值更新成hot_standby wal_keep_segments默認(rèn)值是16,是PG_XLOG下的日志文件大小 archive也可以選擇關(guān)閉,歸檔是定時(shí)恢復(fù)用的,,流復(fù)制不是必須的 5. 主庫(kù)備份(Master端)5.1. 開(kāi)啟文件備份前提是wal_level參數(shù)值必須是archive或者hot_standby 執(zhí)行命令:select pg_start_backup('Replition work'); 5.2. 拷貝數(shù)據(jù)文件拷貝$PGDATA文件,,并復(fù)制到Slave服務(wù)器上,排除pg_xlog內(nèi)容。 tar czvf pgdata.tar.gz pgdata --exclude=pgdata/pg_xlog 遠(yuǎn)程拷貝至slave端并在備機(jī)端解壓 scp pgdata.tar.gz [email protected]:/database/ tar xzvf pgdata.tar.gz 5.3. 結(jié)束master端的備份上述步驟完成后,結(jié)束master端的備份 select pg_stop_backup(), current_timestamp; 6. 修改Slave端配置信息6.1. postgresql.conf文件修改postgresql.conf文件 hot_standby = on 6.2. recovery.conf文件拷貝recovery.conf文件 $cp $PGHOME/share/recovery.conf.sample $PGDATA/recovery.conf 修改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訪問(wèn)master的密碼文件,,可以不用輸密碼 192.168.2.130:1949:postgres:repuser:repuser 6.4. 刪除pid文件刪除slave端(從master端拷過(guò)來(lái)的)的pid文件和pg_xlog $ rm -rf $PGDATA/pg_xlog $ rm -f $PGDATA/postmaster.pid $ mkdir $PGDATA/pg_xlog 7. 啟動(dòng)Slave庫(kù)正常啟動(dòng)備庫(kù)(pg_ctl -D $PGDATA -l pg.log start),有異常可以看log復(fù)制完成后,,可以通過(guò)CSV日志去查看,,本處未設(shè),直接查看進(jìn)程,。 7.1. 查看master進(jìn)程:[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
7.2. 查看slave進(jìn)程:[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
此時(shí)在slave端的pg_xlog下面也產(chǎn)生了日志文件,并且之前pg_start_backup生成的文件名也變成了old的了.
查看日志內(nèi)容: [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 |
|
來(lái)自: jas0n_liu > 《postgresql》