1. 安装PG数据库安装过程略。注:slave端可以只装数据库,不初始化数据库 2. 创建流复制用户master端执行 CREATE USER repuser replication LOGIN CONNECTION LIMIT 2 ENCRYPTED PASSWORD 'repuser'; 3. 配置Master端的访问文件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库的节点数,有多少个slave库就设多少, wal_level是write ahead log参数值,设置流复制务必将此值更新成hot_standby wal_keep_segments默认值是16,是PG_XLOG下的日志文件大小 archive也可以选择关闭,归档是定时恢复用的,流复制不是必须的 5. 主库备份(Master端)5.1. 开启文件备份前提是wal_level参数值必须是archive或者hot_standby 执行命令:select pg_start_backup('Replition work'); 5.2. 拷贝数据文件拷贝$PGDATA文件,并复制到Slave服务器上,排除pg_xlog内容。 tar czvf pgdata.tar.gz pgdata --exclude=pgdata/pg_xlog 远程拷贝至slave端并在备机端解压 scp pgdata.tar.gz root@192.168.2.129:/database/ tar xzvf pgdata.tar.gz 5.3. 结束master端的备份上述步骤完成后,结束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访问master的密码文件,可以不用输密码 192.168.2.130:1949:postgres:repuser:repuser 6.4. 删除pid文件删除slave端(从master端拷过来的)的pid文件和pg_xlog $ rm -rf $PGDATA/pg_xlog $ rm -f $PGDATA/postmaster.pid $ mkdir $PGDATA/pg_xlog 7. 启动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
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
此时在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 |
|
来自: jas0n_liu > 《postgresql》