Stream Replication
master防火墙配置
该搭建整套环境如下:
PostgreSQL 9.6 SUSE 环境搭建(一)
PostgreSQL 主从异步流复制配置(二)
PostgreSQL 主从异步、同步流复制配置(三)
服务器地址如下:
master :10.10.56.16
slave :10.10.56.17
slave :10.10.56.18
slave :10.10.56.19
在 master 上搭建PostgeSQL 环境基于配置(一),具体请参考 搭建PostgreSQL 安装环境(一)
安装成功后,在master的客户端认证文件pg_hba.conf 后新增如下:
postgres@clw-db1:~> vim /pgdata/9.6/poc/data/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
host replication repl 10.10.56.17/32 md5
host replication repl 10.10.56.18/32 md5
host replication repl 10.10.56.19/32 md5
上述表示:允许地址为17、18、19的用户repl 通过MD5密码验证 从主机进行复制。
修改配置文件后,若PostgeSQL 服务在运行 则reload,没有运行则 start 使之生效:
postgres@clw-db1:~> /opt/pgsql-9.6/bin/pg_ctl -D /pgdata/9.6/poc/data/ reload
server signaled
postgres@clw-db1:~>
master配置成功后,slave 安装基本环境同 master ,区别在于 slave 从库不需要进行 initdb 初始化数据库
slave复制数据
postgres@clw-db2:/pgdata/9.6/poc> /opt/pgsql-9.6/bin/pg_basebackup -h 10.10.56.16 -U repl -W -Fp -Pv -Xs -R -D /pgdata/9.6/poc/data/
Password:
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
transaction log start point: 0/2000028 on timeline 1
pg_basebackup: starting background WAL receiver
29956/29956 kB (100%), 1/1 tablespace
transaction log end point: 0/20000F8
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
-h 表示主机地址 -W 表示需要密码 -Fp 表示普通文件格式输出
-Xs 表示通过流复制抓取备份日志 -R 表示在输出目录默认创建一个recovery.conf文件
-u 表示用户 -D 指定数据库存放的目录
上述表示把数据从主库master同步到slave 上
查看slave的recovery配置文件
postgres@clw-db2:/pgdata/9.6/poc/data> cat recovery.conf
standby_mode = 'on'
primary_conninfo = 'user=repl password=123456 host=10.10.56.16 port=5432 sslmode=disable sslcompression=1'
启动slave
postgres@clw-db2:/home/postgres> /opt/pgsql-9.6/bin/pg_ctl -D /pgdata/9.6/poc/data/ start
server starting
postgres@clw-db2:/home/postgres> FATAL: data directory "/pgdata/9.6/poc/data" has group or world access
DETAIL: Permissions should be u=rwx (0700).
解决方法:
postgres@clw-db2:/pgdata/9.6/poc> chmod 0700 data
启动PG server
postgres@clw-db2:/pgdata/9.6/poc> /opt/pgsql-9.6/bin/pg_ctl -D /pgdata/9.6/poc/data/ start
server starting
postgres@clw-db2:/pgdata/9.6/poc>
看到日志输出以下内容说明启动成功:
postgres@clw-db2:/pgdata/9.6/poc> tail -f /pgdata/9.6/poc/data/pg_log/postgresql-2018-05-03_172139.csv
2018-05-03 17:21:39.838 CST,,,30454,,5aead4a2.76f6,2,,2018-05-03 17:21:38 CST,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,""
查看主从是否已连接
postgres@clw-db1:~> /opt/pgsql-9.6/bin/psql -p 5432 -U postgres pocdb
psql (9.6.8)
Type "help" for help.
pocdb=# \x
Expanded display is on.
pocdb=#
pocdb=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 21072
usesysid | 16385
usename | repl
application_name | walreceiver
client_addr | 10.10.56.17
client_hostname |
client_port | 43200
backend_start | 2018-05-03 17:21:39.842751+08
backend_xmin |
state | streaming
sent_location | 0/3000220
write_location | 0/3000220
flush_location | 0/3000220
replay_location | 0/3000220
sync_priority | 0
sync_state | async