Replication Postgres Docker
Contents
用法
全自动配置主从replication模式postgres
docker-compose up -d
|
|
配置主从postgres
配置pg_hba.conf
samenet 在同一网段允许访问
host replication replic samenet md5
配置postgres.conf
|
|
配置recovery.conf
|
|
验证
查看主从sender进程
主,
wal sender
1 2 3
[postgres@pgmaster ~/9.6/data]$ ps -ef | grep post postgres 127044 55 0 14:35 ? 00:00:00 postgres: wal sender process replic 172.26.0.3(46174) authentication postgres 127046 55 0 14:35 ? 00:00:00 postgres: wal sender process replic 172.26.0.3(46178) authentication
从
1 2
[postgres@pgslave ~]$ ps -ef | grep post postgres 57 45 0 15:07 ? 00:00:00 postgres: wal receiver process streaming 0/3000060
查看数据库
主,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
postgres=# \x Expanded display is on. postgres=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 110 usesysid | 16384 usename | replic application_name | walreceiver client_addr | 172.26.0.3 client_hostname | client_port | 54464 backend_start | 2018-11-05 01:50:18.789676+00 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
pg_controldata
主库
in production
1 2 3 4 5 6
[postgres@pgmaster ~]$ pg_controldata pg_control version number: 960 Catalog version number: 201608131 Database system identifier: 6620022152420352029 Database cluster state: in production pg_control last modified: Mon 05 Nov 2018 07:11:52 AM UTC
从库
in archive recovery
1 2 3 4 5 6
[postgres@pgslave ~]$ pg_controldata pg_control version number: 960 Catalog version number: 201608131 Database system identifier: 6620022152420352029 Database cluster state: in archive recovery pg_control last modified: Mon 05 Nov 2018 07:11:52 AM UTC
测试
create table in master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[postgres@pgmaster ~]$ psql psql (9.6.10) Type "help" for help. postgres=# create table test( postgres(# id int, postgres(# name char(50) postgres(# ); CREATE TABLE postgres=# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | test | table | postgres (1 row)
check table replic to slave
1 2 3 4 5 6 7 8 9 10
[postgres@pgslave ~]$ psql psql (9.6.10) Type "help" for help. postgres=# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | test | table | postgres (1 row)
create one data in master
1 2 3 4 5 6 7 8 9
postgres=# insert into test (id,name) values (1,'bingo'); INSERT 0 1 postgres=# select * from test; id | name ----+---------------------------------------------------- 1 | bingo (1 row) postgres=#
check data in slave.
1 2 3 4 5
postgres=# select * from test; id | name ----+---------------------------------------------------- 1 | bingo (1 row)
如果在slave上做写操作将会失败。
1 2
postgres=# insert into test (id,name) values (1,'bingo'); ERROR: cannot execute INSERT in a read-only transaction
Author wxdlong
LastMod 2019-11-16