pg12.1异步流复制

环境:centos7 pg12.1
192.168.149.133 (master)
192.168.149.134 (standby)

异步流复制:
1、主数据库配置(192.168.149.133):
找到/data/pgdata/pg12/data/pg_hba.conf文件并在末尾添加:
host replication all 0/0 md5

表示任何用户都可以连接到本数据库进行流复制,演示用的是postgres超级用户,也可以创建专门的复制用户
修改配置文件/data/pgdata/pg12/data/postgresql.conf设置如下必要参数:

listen_addresses = '*'
max_wal_senders = 10
wal_level = replica

2、在Standby上做备份(192.168.149.134)

注意:a、先把从库服务stop
b、把数据data目录重命名:mv data data_bak

然后在从库上远程备份,命令如下:
pg_basebackup -h 192.168.149.133 -U postgres -F p -P -X stream -R -D $PGDATA -l backup20210311

备份完成后就会在data目录下生成standby.signal文件,同时会在postgresql.auto.conf文件中生成如下内容:
primary_conninfo = 'user=postgres password=postgres host=192.168.149.133 port=1921 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any'

3、启动数据库(192.168.149.134)
如果搭建成功则在主库上(192.168.149.133)查询复制状态会出现如下结果:

postgres=# select client_addr,state,sync_state from pg_stat_replication;
client_addr | state | sync_state
-----------------+-----------+------------
192.168.149.134 | streaming | async
(1 row)

4、验证
主库(192.168.149.133)上创建数据库、表、插入数据:

postgres=# create database tt;
CREATE DATABASE
postgres=# \c tt
You are now connected to database "tt" as user "postgres".
tt=# create table t(id int,name varchar(20));
CREATE TABLE
tt=# insert into t select 1,'aa';
INSERT 0 1
tt=# select * from t;
id | name
----+------
1 | aa
(1 row)

tt=# insert into t select 2,'bb';
INSERT 0 1

tt=# alter table t add column addr varchar(20);
ALTER TABLE
tt=# \d t
Table "public.t"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
id | integer | | |
name | character varying(20) | | |
addr | character varying(20) | | |

从库验证:
postgres=# \c tt
You are now connected to database "tt" as user "postgres".
tt=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | t | table | postgres
(1 row)

tt=# select * from t;
id | name | addr
----+------+------
1 | aa |
2 | bb |
(2 rows)

tt=# \d t
Table "public.t"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
id | integer | | |
name | character varying(20) | | |
addr | character varying(20) | | |

发现DML和DDL都同步成功。

配置安装请参考:https://www.jianshu.com/p/a4d684725a8f

你可能感兴趣的:(pg12.1异步流复制)