注1:本次测试所使用OPENGAUSS版本为5.1.0
注2:暂时实时同步对DDL语句不支持
注3:目前针对opengauss 5.0及mogdb 5.0以下版本,不支持DDL,故第5章节的DDL配置步骤无需要操作

1 数据库配置(必选项)

1.1 日志格式配置(如果已经修改过,可忽略)

  • 修改postgresql.conf文件
#逻辑级别日志
wal_level = logical
#如果需要远程读日志,需要设置,至少为1,就接受1个读日志线程
max_wal_senders = 10   #建议大于等于10 
#设置日志保留个数,设置太少了,日志量太大了日志自动删除后,容易读不到日志
wal_keep_segments = 10   #建议大于等于10

# wal_keep_size:要为日志复制进程保留多少日志量。旧版本使用的是wal_keep_segments参数(要为日志复制进程保留多少个日志文件),在pg 13中,该参数已被移除。wal_keep_size = wal_keep_segments * wal_segment_size
# 修改完成后需要重启数据库:gs_ctl -D /var/lib/postgresql/data -l logfile restart
# wal_level=logical 需要重启数据库
  • 验证是否成功
postgres=# select name,setting,category from pg_settings where name in ('wal_level','wal_keep_segments','max_wal_senders','wal_keep_size');
       name        | setting |           category
-------------------+---------+-------------------------------
 max_wal_senders   | 10      | Replication / Sending Servers
 wal_keep_segments | 10      | Replication / Sending Servers
 wal_level         | logical | Write-Ahead Log / Settings
(3 rows)

2 添加槽位号说明(可选操作)

(如发布数据库未配置SLOT_NAME参数,则此步骤添加槽位号跳过,目前软件仅支持添加物理复制槽)

2.1 创建槽位号(SQL/系统命令两种可选)

  • 创建槽位号
  select pg_create_physical_replication_slot('zcbus_slot_name');
  • 删除槽位号
  select pg_drop_replication_slot('zcbus_slot_name');

2.1.1 查看创建槽位号信息

postgres=#  select * from pg_replication_slots;
    slot_name    | plugin | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn 
-----------------+--------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------
 zcbus_slot_name |        | physical  |        |          | f         | t      |       2495 |      |              | 0/136FA6E0  | 
(1 row)

2.1.2 切换日志测试

切换opengauss日志操作:

  select pg_switch_xlog();
  ZCBUS发布节点配置增加SLOT_NAME参数,自动匹配指定槽位号进行日志读取。

删除日志参考:
检查可以删除wal的日志文件


83e44cfdd569:~\/data$ pg_controldata /var/lib/opengauss/data | grep "Latest checkpoint location"
Latest checkpoint location:           0/4000528

omm=# select pg_xlogfile_name('0/4000528');
     pg_xlogfile_name
 000000010000000000000004

删除日志文件操作

pg_archivecleanup /var/lib/postgresql/data/pg_wal/ 000000010000000100000004

也可手动rm 来进行删除

3 配置解析日志用户(必选项)

  • 如果远程读日志,需要设置读日志的权限,修改pg_hba.conf
host    replication     all             0.0.0.0/32            ident/md5 ###可选

上段用户加密方式可以通过下述SQL查询加密方式,进行配置

  • 查询数据库加密方式
SHOW password_encryption_type;

* 当参数password_encryption_type设置为0时,表示采用md5方式对密码加密。md5为不安全的加密算法,不建议使用。
* 当参数password_encryption_type设置为1时,表示采用sha256和md5方式对密码加密。其中包含md5为不安全的加密算法,不建议使用。
* 当参数password_encryption_type设置为2时,表示采用sha256方式对密码加密,为默认配置。
  • 执行如下命令将其设置为安全的加密算法
示例:gs_guc reload -N all -I all -c "password_encryption_type=0"
  • 验证是否成功
postgres=# select * from pg_hba_file_rules;
 line_number | type  |   database    | user_name |  address  |                 netmask                 | auth_method | options | error 
-------------+-------+---------------+-----------+-----------+-----------------------------------------+-------------+---------+-------
          95 | local | {replication} | {all}     |           |                                         | trust       |         | 

4 数据表配置(必选项)

4.1 需要同步的表打开补充日志

ALTER TABLE schemaname.tabname REPLICA IDENTITY FULL;

---批量生成打开表补充日志sql语句参考
SELECT 'ALTER TABLE '||nspname||'.'||relname||' REPLICA IDENTITY FULL;' FROM pg_class c,pg_namespace n where relnamespace = n.oid and c.relkind='r' and nspname='public';

---注1:表所属用户或者超级用户才有开启REPLICA IDENTITY权限,否则报错:must be owner of table

4.1.1 分区表检查SQL以及注意事项

源表为分区表开启补充日志
SELECT
   parent.relname AS partitioned_table_name,
   parent.relkind AS partitioned_table_type,
   parent.relpartbound AS partition_bound,
   child.relname,
   child.relkind,
   child.relreplident,
   'ALTER TABLE ' || nspname || '.' || child.relname || ' REPLICA IDENTITY FULL' rif
FROM
 pg_class parent
LEFT JOIN pg_inherits ON pg_inherits.inhparent = parent.oid
LEFT JOIN pg_class child ON pg_inherits.inhrelid = child.oid
LEFT JOIN pg_namespace pns ON child.relnamespace = pns.oid
WHERE
 parent.relkind = 'p'
AND parent.relname = 'tabname';

---该语句能查询出分区表中的分区是否开启补充日志,若没有开启,可通过生成的批量SQL开启表补充日志

备注:

  opengauss只有owner或usersuper权限才能修改表结构或执行drop操作,请在执行操作前确定所同步表的Owner或用Superuser权限用户执行ALTER USER zcbus WITH SUPERUSER;

5 DDL兼容配置(可选项)

  • 添加此功能后,在增量复制过程中,若有表结构修改等操作,软件将自动同步。若未添加此功能,在修改结构时若遇到结构不一致的情况,可能需要人工参与。

5.1 创建ddl_event_tbl表(用于存储DDL语句)

  • 创建表
create schema zcbus;

create table zcbus.ddl_event_tbl (
event text,
tag text,
objid oid,
object_type text,
schema_name text,
object_identity text,
query text,
op_time timestamp default now()
);
  • 检查ddl_event_tbl表是否创建
select * from information_schema.tables where table_type='BASE TABLE' and table_name='ddl_event_tbl';

5.2 创建函数zcbus_func_ddl_command

create or replace function zcbus.zcbus_func_ddl_command() returns event_trigger as $$  
declare  
  v1 text;
  r record;
begin
  select query into v1 from pg_stat_activity where pid=pg_backend_pid();
  -- RAISE NOTICE 'ddl event:%, command:%', tg_event, tg_tag; 
  if TG_EVENT='ddl_command_end' then  
    SELECT * into r FROM pg_event_trigger_ddl_commands();
    if r.classid > 0 then
      insert into zcbus.ddl_event_tbl(event, tag, objid, object_type, schema_name, object_identity, query)  
        values(TG_EVENT, TG_TAG, r.objid, r.object_type, r.schema_name, r.object_identity, v1);
    end if;
  end if;
  if TG_EVENT='sql_drop' then
    if TG_TAG != 'ALTER TABLE' then
      SELECT * into r FROM pg_event_trigger_dropped_objects();
      insert into zcbus.ddl_event_tbl(event, tag, object_type, schema_name, object_identity, query)  
        values(TG_EVENT, TG_TAG, r.object_type, r.schema_name, r.object_identity, v1);
    end if;
  end if;
end;  
$$ language plpgsql strict;
  • 检查function是否创建:
select a.* FROM pg_catalog.pg_proc a JOIN pg_catalog.pg_namespace b ON a.pronamespace = b.oid
WHERE a.proname = 'zcbus_func_ddl_command' ;

5.3 创建事件触发器

CREATE EVENT TRIGGER zcbus_et_ddl_command on ddl_command_end EXECUTE PROCEDURE zcbus.zcbus_func_ddl_command();
CREATE EVENT TRIGGER zcbus_et_ddl_drop on sql_drop EXECUTE PROCEDURE zcbus.zcbus_func_ddl_command();
  • 检查EVENT TRIGGER是否创建:
select * from  pg_event_trigger  where evtname in ('zcbus_et_ddl_drop','zcbus_et_ddl_command');

6 用户授权(必选项)

6.1 创建用户及赋权

切换到同步库下,执行如下(数据库命令行执行):

create user zcbus with password  '*****';
grant connect on database *** to zcbus;  # ***为需要同步的数据库名称
create schema zcbus authorization zcbus;
grant select,trigger on table zcbus.ddl_event_tbl  to zcbus;
grant execute ON FUNCTION zcbus.zcbus_func_ddl_command to zcbus;
alter user zcbus REPLICATION;
grant select on all tables in schema *** to zcbus;
alter table zcbus.ddl_event_tbl owner to zcbus;

以上过程中,web页面不要点击注册按键,所有配置手工完成!

7 补充说明

7.1 添加新的同步表

添加新表,根据schemaname.tablename,确认postgres账号开启表的补充日志模式状态开启成功

select relreplident from pg_class c , pg_tables tab where tab.schemaname = 'schemaname' and c.relname='tablename';

查询结果说明

d = 默认(主键)
n = 无
f = 所有列
i = 索引的indisreplident被设置或者默认
查询结果为f,则此表满足同步条件,开启表的补充日志模式成功

注:每次添加完新的同步表,需要再次对同步用户执行如下权限

grant select on all tables in schema *** to zcbus;

7.2 数据类型支持列表

  • OPENGAUSS FOR PG
OPENGAUSS FOR PG数据类型 是否支持 描述
samllint
integer(p,s)
bigint
decimal(p,s)
numeric(p,s)
real
double precision
smallserial
serial
bigserial
money
binary_integer
largeserial
float4
float(p)
binary_double
dec(p,s)
tinyint
character varying(p)
varchar(p)
character(p)
char
text
nchar(p)
nvarchar(p)
timestamp
timestamp with time zone
date
time
time with time zone
interval
smalldatetime
reltime
abstime
bit
bit varying
boolean
blob
raw
bytea
  • OPENGAUSS FOR MYSQL
OPENGAUSS FOR MYSQL数据类型 是否支持 描述
tinyint
int
smallint
mediumint
bigint
float
decimal(p,s)
double
char
varchar
tinytext
text
mediumtext
longtext
date
time
datetime
timestamp
year
blob
tinyblob
mediumblob
longblob
binary
varbinary
enum 否,因其为自定义数据类型
set 否,因其为自定义数据类型
文档更新时间: 2024-07-27 20:56   作者:操李红