MaxScale新功能实测:一份数据同时写入MongoDB和MySQL

指尖上的架构 2024-09-19 03:02:09

作者介绍

贺春旸,dbaplus社群金牌专家,凡普金科和爱钱进DBA团队负责人,《MySQL管理之道:性能调优、高可用与监控》第一&二版、《MySQL运维进阶指南》作者,曾任职于中国移动飞信、安卓机锋网。五次荣获dbaplus年度MVP,致力于MariaDB、MongoDB等开源技术的研究,主要负责数据库性能调优、监控和架构设计。

一、介绍

MariaDB MaxScale的NoSQL协议模块是一个强大而独特的功能,它允许使用MongoDB协议与MariaDB数据库进行交互。

以下是该模块的主要特点:

1、协议转换

将MongoDB的查询语言和操作转换为SQL语句。使得应用程序可以使用MongoDB的驱动程序与MariaDB/MySQL通信。

2、无缝集成

允许现有的MongoDB应用程序直接连接到MariaDB/MySQL,无需修改应用程序代码。简化了从MongoDB向MariaDB/MySQL的迁移过程。

MaxScale to MongoDB架构示意图

二、应用场景

需求:希望实现一份数据同时写入MongoDB和MySQL(用于运营分析),建立双写机制。以往在没有MariaDB MaxScale的情况下,这通常需要大量的代码重构。而如今,通过MariaDB MaxScale的NoSQL协议模块,能够实现将MongoDB中的数据无缝迁移至MySQL,无需修改任何现有代码。

三、安装与部署

1、下载与安装MaxScale

shell> wget https://dlm.mariadb.com/3843223/MaxScale/24.02.2/yum/centos/7/x86_64/maxscale-24.02.2-1.rhel.7.x86_64.rpmshell> yum install maxscale-24.02.2-1.rhel.7.x86_64.rpm -y

2、配置MaxScale支持MongoDB协议

环境:192.168.176.204 - MaxScale 192.168.198.239 - MariaDB

1)创建MaxScale配置文件

shell> vim /etc/maxscale.cnf[maxscale]threads=autoadmin_secure_gui=falseadmin_host=0.0.0.0[server1]type=serveraddress=192.168.198.239port=4309#protocol=MySQLBackendprotocol=MariaDBBackend[MariaDB-Monitor]type=monitormodule=mariadbmonservers=server1user=adminpassword=123456monitor_interval=2s[Read-Write-Service]type=servicerouter=readwritesplitservers=server1user=adminpassword=123456enable_root_user=1[Read-Write-Listener]type=listenerservice=Read-Write-Serviceprotocol=mariadbprotocolport=4006[MongoDB-Listener]type=listenerservice=Read-Write-Serviceprotocol=nosqlprotocolnosqlprotocol.user=adminnosqlprotocol.password=123456############################################### 第一次要设置false,然后创建账号## db.createUser({user:"admin",pwd:"123456",roles:[{role:"dbOwner",db:"test"}]})## 然后再修改为true,并重启maxscalenosqlprotocol.authentication_required=falsenosqlprotocol.authorization_enabled=false#############################################port=17017

注:首次启动MaxScale服务时,需要将以下两个参数设置为false:

nosqlprotocol.authentication_required=falsenosqlprotocol.authorization_enabled=false

此操作的目的是先通过非认证模式登录17017端口,然后再创建应用所需的访问账号。请确保后端MariaDB/MySQL服务启动正常,且已存在admin账号,权限为ALL。

2)启动MaxScale

shell> systemctl start maxscaleshell> systemctl status maxscale● maxscale.service - MariaDB MaxScale Database Proxy Loaded: loaded (/usr/lib/systemd/system/maxscale.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2024-09-06 14:51:06 CST; 8h ago Process: 130360 ExecStart=/usr/bin/maxscale (code=exited, status=0/SUCCESS) Main PID: 130361 (maxscale) CGroup: /system.slice/maxscale.service └─130361 /usr/bin/maxscale

3)创建Mongo连接账号,提供给应用程序访问使用

shell> mongosh --port 17017

在test数据库中创建账号admin,密码为123456,并赋予dbOwner权限:

use testdb.createUser({user:"admin",pwd:"123456",roles:[{role:"dbOwner",db:"test"}]})

此命令将为test数据库创建一个具有dbOwner权限的管理员账号。

4)以认证模式登录17017端口

编译maxscale.cnf配置文件,将下面的参数值修改为true:

nosqlprotocol.authentication_required=truenosqlprotocol.authorization_enabled=true

注:需重启MaxScale服务生效。

shell> systemctl restart maxscale

5)连接MongoDB协议17017端口

shell> mongosh mongodb://admin:123456@127.0.0.1:17017/test --authenticationDatabase test

a. 插入数据

db.t2.insert({"age": 45, "name": "中年大叔", "hobby": ["美食", "摩旅", "游泳", "电影", "实现50岁退休"], "address": "北京"})

此时,你回到后端MariaDB/MySQL数据库里,已经发现t2表已经创建,表结构如下:

mysql> show create table t2\G*************************** 1. row *************************** Table: t2Create Table: CREATE TABLE `t2` ( `id` varchar(35) GENERATED ALWAYS AS (json_compact(json_extract(`doc`,'$._id'))) VIRTUAL, `doc` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`doc`)), UNIQUE KEY `id` (`id`), CONSTRAINT `id_not_null` CHECK (`id` is not null)) ENGINE=InnoDB DEFAULT CHARSET=latin1

b. 查询数据

mysql> select * from t2\G*************************** 1. row *************************** id: {"$oid":"66dab9b9fc7f8c8fff4a7bcf"}doc: { "_id" : { "$oid" : "66dab9b9fc7f8c8fff4a7bcf" }, "age" : 45.0, "name" : "中年大叔", "hobby" : [ "美食", "摩旅", "游泳", "电影", "实现50岁退休" ], "address" : "北京" }1 row in set (0.00 sec)

用MySQL的JSON函数查询数据:

mysql> SELECT -> JSON_UNQUOTE(JSON_EXTRACT(doc, '$.name')) AS name, -> JSON_EXTRACT(doc, '$.age') AS age, -> JSON_EXTRACT(doc, '$.hobby') AS hobby -> FROM t2;+--------------+------+---------------------------------------------------------------+| name | age | hobby |+--------------+------+---------------------------------------------------------------+| 中年大叔 | 45.0 | ["美食", "摩旅", "游泳", "电影", "实现50岁退休"] |+--------------+------+---------------------------------------------------------------+1 row in set (0.00 sec)

至此大功告成。

四、总结

MariaDB MaxScale NoSQL协议模块的核心优势在于其卓越的适应性和效率。它巧妙地架起了NoSQL和SQL世界之间的桥梁,使得组织能够在不改变现有MongoDB应用代码的情况下,实现向MySQL的无缝过渡和双写操作。这一创新极大地简化了数据库基础架构,显著降低了维护成本和复杂性。

更为重要的是,这个模块释放了开发团队的生产力。通过消除处理复杂数据层转换的需求,开发人员可以将更多精力集中在核心业务逻辑的开发上。这不仅加快了项目交付速度,还提高了代码质量和创新能力。

0 阅读:36

指尖上的架构

简介:感谢大家的关注