Halo DB 14 小白零基础系列(10)HaloDB中MySQL模式下的流复制实现

2024年 3月 12日 95.0k 0

前言:

        最近这段时间,更新的有些不及时,在这里为大家道歉。在之前的文章中带各位朋友了解了HaloDB中的conf文件以及hba文件,那么在HaloDB中我们的王牌技术,实现多数据库兼容模式的相关内容就可以为大家介绍。本篇为大家介绍,如何在HaloDB实现MySQL模式下的流复制相关内容。

        来,日常打广环节到了!

        我们的HaloDB 是基于原生PG打造的新一代高性能安全自主可控全场景通用型统一数据库。业内首次创造性的提出插件式内核架构设计,通过配置的方式,适配不同的应用场景,打造全场景覆盖的能力,满足企业大部分数据存储处理需求,从而消除数据孤岛,降低系统复杂度,保护企业既有投资,降低企业成本。同时支持x86、arm等异构平台之间的混合部署。

        如果有对我们的产品感兴趣的朋友可以通过主页的联系方式与我取得联系,获取license来安装体验,当然您如果有好的建议也可以提给我们,下面正式开始今天的内容。

一、HaloDB中的MySQL模式开启:

      在我们的HaloDB中,针对目前市场所普遍使用的MySQL数据库,我司在这方面推出了HaloDB for MySQL模式,为了降低各位开发人员以及企业的学习和使用成本,我司深入研究和开发系统内核,针对MySQL完成了通信协议层的兼容和适配。具体的使用方式如下,话不多说,发车了。

1、部署规划

2、开启归档模式

创建归档目录:

[root@halodb1 ~]# mkdir -p /data/halo/archivedir
[root@halodb1 ~]# chown -R halo:halo /data/halo/archivedir

开启归档模式:

# 启用归档模式
archive_mode = on
# 将WAL文件归档到指定的归档目录。在这个例子中,%p是WAL文件的路径,%f是WAL文件的文件名。归档命令的含义是,如果归档目录中不存在相同文件名的归档文件,则将WAL文件复制到归档目录中。
archive_command = 'test ! -f /data/halo/archivedir/%f && cp %p /data/halo/archivedir/%f'
# 指定了还原命令,用于在需要恢复数据库时从归档目录中复制WAL文件到WAL日志目录。
restore_command = 'cp /data/halo/archivedir/%f %p'

3、HaloDB for MySQL模式开启:

主库修改HaloDB中$PGDATA/postgresql.conf配置文件, 修改内容以及相关参数解释如下,请参考

#database_compat_mode = 'postgresql' 修改为 database_compat_mode = 'mysql'             
#second_listener_on = false 修改为 second_listener_on = 1                       
#mysql.halo_mysql_version = '5.7.32-log' 修改为 mysql.halo_mysql_version = '8.0.21-log'
#mysql.ci_collation = true 修改为 mysql.ci_collation = true

4、主库重启数据库并创建扩展

在halo用户下操作

[halo@halodb1 ~]$ pg_ctl restart
pg_ctl: PID file "/data/halo/postmaster.pid" does not exist
Is server running?
trying to start server anyway
waiting for server to start....2024-03-12 17:15:41.001 CST [2022] LOG: starting 羲和(Halo) 1.0.14.10 (231130) on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2024-03-12 17:15:41.001 CST [2022] LOG: listening on IPv4 address "0.0.0.0", port 1921
2024-03-12 17:15:41.001 CST [2022] LOG: listening on IPv6 address "::", port 1921
2024-03-12 17:15:41.003 CST [2022] LOG: listening on Unix socket "/var/run/halo/.s.PGSQL.1921"
2024-03-12 17:15:41.005 CST [2022] LOG: listening on IPv4 address "0.0.0.0", port 3306
2024-03-12 17:15:41.005 CST [2022] LOG: listening on IPv6 address "::", port 3306
2024-03-12 17:15:41.007 CST [2022] LOG: listening on Unix socket "/var/run/halo/.s.PGSQL.3306"
2024-03-12 17:15:41.017 CST [2023] LOG: database system was shut down at 2024-03-12 17:00:31 CST
2024-03-12 17:15:41.023 CST [2022] LOG: database system is ready to accept connections
done
server started
[halo@halodb1 ~]$ psql -c "create extension aux_mysql; "
CREATE EXTENSION

5、设置mysql应用的用户 

halo0root=# set password_encryption='mysql_native_password';
SET
halo0root=# CREATE USER mysqltest SUPERUSER PASSWORD '123456';
CREATE ROLE

这里需要注意,在HaloDB中使用mysql模式情况下,mysql的数据库即schema,只需要在halo0root库下创建schema就可以。

6、主库重新加载设置

[halo@halodb1 halo]$ pg_ctl reload
server signaled
2024-03-12 18:07:29.009 CST [2022] LOG: received SIGHUP, reloading configuration files

7、验证MySQL模式开启成功

halo0root=# d List of relations
Schema | Name | Type | Owner
--------+-----------+-------+-------
mysql | dual | view | halo
mysql | proc | view | halo
mysql | proc_type | table | halo
mysql | user | table | halo
(4 rows)

二、HaloDB for MySQL的流复制

        在上文中,我们已经了解了在HaloDB for MySQL下的开启,那采用流复制进行主备搭建也十分简单和便捷,同学们继续往下看。

1、主库创建复制用户

halo0root=# CREATE USER replica PASSWORD '123456' REPLICATION;

2、主库配置$PGDATA/pg_hba.conf

# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
## 添加下面内容
host replication replica 0.0.0.0/0 md5

3、主库重新加载配置

[halo@halodb1 ~]$ pg_ctl reload
server signaled
2024-03-12 18:46:51.340 CST [3120] LOG: received SIGHUP, reloading configuration files

4、创建备库:

        创建备库时,需要将两个节点的数据库关闭并清空数据目录。由于我是使用虚拟机,所以在虚拟机关闭状态下,修改IP即可。清空备库的数据文件,生产环境请谨慎操作!!!删错了的话,哥们你可就有机会过上1~3年起的规律生活了~

[halo@halodb2 halo]$ ll
total 60
drwx------ 6 halo halo 54 Jan 8 13:38 base
drwx------ 2 halo halo 4096 Mar 12 18:49 global
drwx------ 2 halo halo 6 Dec 20 13:33 pg_commit_ts
drwx------ 2 halo halo 6 Dec 20 13:33 pg_dynshmem
-rw------- 1 halo halo 4840 Feb 29 17:37 pg_hba.conf
-rw------- 1 halo halo 1636 Dec 20 13:33 pg_ident.conf
drwx------ 4 halo halo 68 Mar 12 18:49 pg_logical
drwx------ 4 halo halo 36 Dec 20 13:33 pg_multixact
drwx------ 2 halo halo 6 Dec 20 13:33 pg_notify
drwx------ 2 halo halo 6 Dec 20 13:33 pg_replslot
drwx------ 2 halo halo 6 Dec 20 13:33 pg_serial
drwx------ 2 halo halo 6 Dec 20 13:33 pg_snapshots
drwx------ 2 halo halo 63 Mar 12 18:49 pg_stat
drwx------ 2 halo halo 6 Mar 12 18:49 pg_stat_tmp
drwx------ 2 halo halo 18 Dec 20 13:33 pg_subtrans
drwx------ 2 halo halo 19 Jan 5 11:46 pg_tblspc
drwx------ 2 halo halo 6 Dec 20 13:33 pg_twophase
-rw------- 1 halo halo 3 Dec 20 13:33 PG_VERSION
drwx------ 3 halo halo 92 Jan 2 11:21 pg_wal
drwx------ 2 halo halo 18 Dec 20 13:33 pg_xact
-rw------- 1 halo halo 113 Jan 8 13:03 postgresql.auto.conf
-rw------- 1 halo halo 30536 Jan 3 07:42 postgresql.conf
-rw------- 1 halo halo 43 Mar 12 18:49 postmaster.opts
[halo@halodb2 halo]$ rm -rf *
[halo@halodb2 halo]$ ll
total 0

5、备库开启复制槽复制

[halo@halodb2 data]$ pg_basebackup -F p -X stream -v -P -h 192.168.1.77 -p 1921 -U replica -D /data/halo -R -C --slot halo33
Password:
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/9000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created replication slot "halo33"
165129/165129 kB (100%), 2/2 tablespaces
pg_basebackup: write-ahead log end point: 0/9000100
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed

这里有个小坑需要注意下,创建完MySQL用户,不要直接创建replica用户,否则会出现下面的问题,具体产生原因 我下一期说~(我要是忘了大家记得提醒我,要不然我管挖不管埋了)

6、启动备库

[halo@halodb2 data]$ pg_ctl start
waiting for server to start....2024-03-12 19:40:36.694 CST [3658] LOG: starting 羲和(Halo) 1.0.14.10 (231130) on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2024-03-12 19:40:36.694 CST [3658] LOG: listening on IPv4 address "0.0.0.0", port 1921
2024-03-12 19:40:36.694 CST [3658] LOG: listening on IPv6 address "::", port 1921
2024-03-12 19:40:36.696 CST [3658] LOG: listening on Unix socket "/var/run/halo/.s.PGSQL.1921"
2024-03-12 19:40:36.698 CST [3658] LOG: listening on IPv4 address "0.0.0.0", port 3306
2024-03-12 19:40:36.698 CST [3658] LOG: listening on IPv6 address "::", port 3306
2024-03-12 19:40:36.700 CST [3658] LOG: listening on Unix socket "/var/run/halo/.s.PGSQL.3306"
2024-03-12 19:40:36.706 CST [3659] LOG: database system was interrupted; last known up at 2024-03-12 19:40:18 CST
2024-03-12 19:40:38.111 CST [3659] LOG: entering standby mode
2024-03-12 19:40:38.118 CST [3659] LOG: redo starts at 0/9000028
2024-03-12 19:40:38.120 CST [3659] LOG: consistent recovery state reached at 0/9000100
2024-03-12 19:40:38.120 CST [3658] LOG: database system is ready to accept read-only connections
2024-03-12 19:40:38.139 CST [3666] LOG: started streaming WAL from primary at 0/A000000 on timeline 1
done
server started

7、验证流复制是否成功

主库建表并且随机插入数据,有图有真相。

备库查询:

建表语句在这里,方便大家复制粘贴玩耍~

halo0root=# CREATE TABLE halodbzz (
halo0root(# id INT PRIMARY KEY,
halo0root(# username VARCHAR(50) NOT NULL,
halo0root(# email VARCHAR(100) UNIQUE,
halo0root(# age INT,
halo0root(# created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
halo0root(# );
CREATE TABLE
halo0root=# INSERT INTO halodbzz (id, username, email, age) VALUES
halo0root-# (1, 'zhangchenxi', 'zhangchenxi@example.com', 18),
halo0root-# (2, 'sky', 'sky@example.com', 18),
halo0root-# (3, 'Salvatorezz', 'charlie@example.com', 18);
INSERT 0 3
halo0root=#

最后:

             虽然是慢工,但不是啥细活。

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论