瀚高数据库
目录
环境
文档用途
详细信息
环境
系统平台:Linux x86-64 Red Hat Enterprise Linux 7
版本:14
文档用途
本文主要用于介绍pglogical的安装配置
详细信息
一、简介
pglogical 2插件(后边简称pglogical)使用发布/订阅的模式为PostgreSQL提供了逻辑流复制的实现方式。pglogicla是基于BDR项目的一部分技术发展而来。
我们一般使用以下名称来描述节点间的数据流,复用了之前的Slony技术解析:
• Nodes(节点) - Postgresql数据库实例
• Providers and Subscribers(发布者和订阅者) - Nodes使用的角色
• Replication Set(复制集) - 表的集合
pglogical使用pg最新的内核特性,因为使用上有一下版本限制:
• 发布者和订阅者节点必要运行在PostgreSQL 9.4版本及以上
• 复制源的筛选和冲突检测只在PostgreSQL9.5版本及以上
• 订阅者端可以是Postgres-XL 9.5版本及以上
支持的使用场景包括
• 可用于大版本升级。
• 完整数据库复制。
• 使用复制集选择性地复制表集。
• 在发布者服务器端或订阅者服务器端选择性复制表行(row_filter)。
• 在发布者端选择性复制表列
• 从多个上游服务器收集/合并数据
架构细节:
• pglogical工作在单库级别而不是像流复制一样的实例级别
• 一个订阅者端可以在不引起额外的磁盘写负载的情况下同时为多个订阅端传输数据
• 一个发布者端可以合并来自多个源的更改并自动检测与处理冲突。(多主机需求的某些方面)
二、要求
为了使用pglogical插件,发布者端和订阅者端必须跑在Postgresql9.4及以上的版本中。
pglogical插件必须在发布者端跟订阅者端同时安装,必须在两端执行CREATE EXTENSION pglogical
需要同步的表在发布者端和订阅者端必须有相同的模式名称及表名称
需要同步的表在发布者和订阅者端必须有相同的列,每列的数据类型必须相同。检查约束、非空约束等也要相同,或者订阅者端的限制比发布者端要弱一些。
表必须有相同的主键。不建议额外添加除主键之外的其他唯一约束。
三、安装示例
pglogical提供rpm包安装及源码安装两种方式,rpm包安装较为简答,本文主要介绍源码包的安装方法。
pglogical源码包的安装同其他任何PostgreSQL的扩展安装一样使用PGXS。
确保包含PostgreSQL发行版中pg_config的目录配置在PATH的环境变量中。如果你的环境中没有安装pg_config这个命令,你需要用你的包管理器来安装对应版本的开发包。
下面以pg14.6为数据库版本为大家演示pglogical插件安装及单向数据同步搭建过程。
OS:redhat7.7
CPU: X86_64
database:pg14.6
extension version:2.4.2
Providers:192.168.164.51
Subscribers:192.168.164.52
1、安装扩展
发布端、订阅端解压缩安装pglogical,下面为发布端的日志
1
2
3
4
5
6
7
8[root@host1 opt]# tar -zxvf pglogical-REL2_4_2.tar.gz [root@host1 opt]# chown -R postgres:postgres pglogical-REL2_4_2 [root@host1 opt]# su - postgres [postgres@host1 ~]$ cd /opt/pglogical-REL2_4_2 [postgres@host1 pglogical-REL2_4_2]$ make clean [postgres@host1 pglogical-REL2_4_2]$ make [postgres@host1 pglogical-REL2_4_2]$ make install
2、修改数据库参数及pg_hba文件,发布端订阅端同时修改postgresql.conf文件修改内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17wal_level = 'logical' max_worker_processes = 10 # one per database needed on provider node # one per node needed on subscriber node max_logical_replication_workers = 10 max_replication_slots = 10 # one per node needed on provider node max_wal_senders = 10 # one per node needed on provider node shared_preload_libraries = 'pglogical' pg_hba.conf文件修改内容,增加本文段免密登录。 # IPv4 local connections: host all all 127.0.0.1/32 trust host all all 192.168.164.0/24 trust 修改完成后重启发布端及订阅端数据库生效 [postgres@host1 data]$ pg_ctl restart [postgres@host2 data]$ pg_ctl restart
3、创建扩展
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18发布端 [postgres@host1 data]$ psql test1 psql (14.6) Type "help" for help. test1=# create extension pglogical; CREATE EXTENSION 订阅端: [postgres@host2 data]$ psql test2 postgres psql (14.6) Type "help" for help. test2=# create extension pglogical; CREATE EXTENSION
4、创建测试表及测试数据发布端创建测试表,模拟测试数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23test1=# c test1 test1 You are now connected to database "test1" as user "test1". test1=> test1=> create table t01(id int primary key,name text); CREATE TABLE test1=> insert into t01 select n,'aaaaaa' from generate_series(1,1000) n; INSERT 0 1000 test1=> create table t02(id int primary key,name text); CREATE TABLE test1=> insert into t02 select n,'aaaaaa' from generate_series(1,1000) n; INSERT 0 1000 test1=> create table t03(id int primary key,name text); CREATE TABLE test1=> insert into t03 select n,'aaaaaa' from generate_series(1,1000) n; INSERT 0 1000 订阅端只创建测试表 [postgres@host2 ~]$ psql test2 test2 test2=> create table t01(id int primary key,name text); CREATE TABLE test2=> create table t02(id int primary key,name text); CREATE TABLE test2=> create table t03(id int primary key,name text); CREATE TABLE
5、发布端创建节点,创建复制集,将要同步的表t01加入到复制集中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27##使用超级用户创建发布节点 test1=> c test1 postgres You are now connected to database "test1" as user "postgres". test1=# select pglogical.create_node(node_name :='provider1',dsn :='host=192.168.164.51 port=5432 dbname=test1'); create_node ------------- 2976894835 (1 row) ##创建复制集 test1=# select pglogical.create_replication_set('defalut'); create_replication_set ------------------------ 692406752 (1 row) ##增加表 test1=# select pglogical.replication_set_add_table('default','public.t01'); replication_set_add_table --------------------------- t (1 row) ##确定复制集表信息 test1=# select * from pglogical.replication_set_table; set_id | set_reloid | set_att_list | set_row_filter -----------+------------+--------------+---------------- 290045701 | t01 | | (1 row)
6、订阅端创建节点 ##使用超级用户创建订阅节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25test2=# c test2 postgres You are now connected to database "test2" as user "postgres". test2=# test2=# select pglogical.create_node(node_name :='subscriber1',dsn :='host=192.168.164.52 port=5432 dbname=test2'); create_node ------------- 330520249 (1 row) test2=# 2022-12-09 16:53:06.188 CST [12715] LOG: manager worker [12715] at slot 0 generation 4 detaching cleanly 2022-12-09 16:53:06.191 CST [12716] LOG: starting pglogical database manager for database test2 2022-12-09 16:53:07.195 CST [12717] LOG: manager worker [12717] at slot 1 generation 1 detaching cleanly ##创建订阅 test2=# SELECT pglogical.create_subscription(subscription_name := 'subscription1',provider_dsn := 'host=192.168.164.51 port=5432 dbname=test1'); create_subscription --------------------- 1763399739 (1 row) test2=# 2022-12-09 16:56:18.755 CST [12744] LOG: manager worker [12744] at slot 2 generation 1 detaching cleanly 2022-12-09 16:56:18.756 CST [12745] LOG: starting apply for subscription subscription1 2022-12-09 16:56:18.759 CST [12746] LOG: manager worker [12746] at slot 2 generation 2 detaching cleanly
7、核对订阅端数据 ##查询三张表信息,可以看到只有加入到复制集的表的数据同步了过来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40test2=# select count(*) from t01; count ------- 1000 (1 row) test2=# select count(*) from t02; count ------- 0 (1 row) test2=# select count(*) from t03; count ------- 0 (1 row) ##发布端再插入1000条数据,查阅订阅端数据 test1=# c test1 test1 You are now connected to database "test1" as user "test1". test1=> insert into t01 select n,'aaaaaa' from generate_series(1001,2000) n; INSERT 0 1000 test1=> select count(*) from t01; count ------- 2000 (1 row) test2=# select count(*) from t01; count ------- 2000 (1 row) 单向逻辑复制同步完成,增量数据及基量数据均成功。
最后
以上就是故意小鸽子最近收集整理的关于pglogical的安装配置的全部内容,更多相关pglogical内容请搜索靠谱客的其他文章。
发表评论 取消回复