基于MaxScale搭建MariaDB读写分离集群的方法【2024年最新版】

server/2024/9/23 22:25:24/

1、什么是MaxScale

MaxScale是MariaDB数据库的一个中间件,为MariaDB提供代理服务,主要可以实现读写分离和一定的负载均衡功能,其中读写分离可将读操作和写操作分离到不同的数据库服务器上,以提高系统的整体性能和扩展性,而正是读写分离功能,使得MaxScale具备已经的负载均衡特性。

2、搭建环境

在这里插入图片描述

3、搭建步骤

3.1、分别在两台OpenEuler上安装MariaDB数据库并分别完成初始化(两台服务器步骤相同)

(1)执行安装命令“yum install mariadb-server -y”;
在这里插入图片描述
(2)执行启动MariaDB数据库命令“systemctl start mariadb”;
在这里插入图片描述
(3)执行MariaDB初始化命令“mysql_secure_installation”,并按自身需求完成MariaDB初始化,初始化案例如下:

[root@localhost ~]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.Enter current password for root (enter for none):   #输入事先设置的数据库root账户密码
OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.You already have a root password set, so you can safely answer 'n'.Change the root password? [Y/n] n   #是否改变root用户密码?... skipping.By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.Remove anonymous users? [Y/n] n   #是否删除匿名用户?... skipping.Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n   #是否不允许root用户直接登录?... skipping.By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.Remove test database and access to it? [Y/n] n   #是否删除测试数据库?... skipping.Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.Reload privilege tables now? [Y/n] y   #是否重新加载权限信息?... Success!Cleaning up...All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!

(4)输入以下代码分别在两套MariaDB上创建监控用户“monitor”和路由用户“maxscale”,并授予monitor用户replication slave权限和super权限,授予maxscale所有权限:

# 路由账号
create user 'maxscale'@'%' identified by 'ymh123';
grant all on *.* to maxscale@'%';# 监控账号
create user 'monitor'@'%' identified by 'ymh123';
grant replication slave,super on *.* to 'monitor'@'%';

3.2、将两套MariaDB数据库设置为主从关系

3.2.1、配置MariaDB-Master

(1)在192.168.174.136服务器上输入命令“vim /etc/my.cnf”,并在[mysqld]字段下添加如下配置,并保存退出:

server-id=1
log-bin=mysql-bin

在这里插入图片描述
(2)输入命令“systemctl restart mariadb”重启MariaDB数据库
在这里插入图片描述
(3)输入命令“mysql -u root”进入数据库,并输入命令“show master status;”记录好“File”字段下的binlog文件的文件名和“Position”字段下的数值,如下:
在这里插入图片描述

3.2.2、配置MariaDB-Slave

(1)在192.168.174.136服务器上输入命令“vim /etc/my.cnf”,并在[mysqld]字段下添加如下配置,并保存退出(server-id要与Master不同,且不写入log-bin字段):

server-id=2

(2)输入命令“systemctl restart mariadb”重启MariaDB数据库
在这里插入图片描述
(3)输入命令“mysql -u root”进入数据库,并输入命令“change master to master_host = '192.168.174.136', master_user = 'monitor', master_password = 'ymh123', master_log_file = 'mysql-bin.000006', master_log_pos = 594;”最后输入“exit”退出MariaDB数据库
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
(4)输入命令“systemctl restart mariadb”重启MariaDB数据库后,输入“mysql -u root”进入数据库
在这里插入图片描述
(5)输入命令“show slave status\G;”如果以下两个字段都是yes,则主从搭建成功(当然,可在Master进行写操作,如果同步到slave,则主从没问题):
在这里插入图片描述

4、安装并配置MaxScale

(1)进入MariaDB官方网站,找到对应版本的MaxScale安装包,并下载;
在这里插入图片描述
在这里插入图片描述
(2)通过dpkg命令对安装包进行本地安装,需要注意的是,安装过程中可能会缺乏必要的依赖,如果出现缺依赖的情况,此时通过“apt install -f”命令自动补充依赖;
在这里插入图片描述
在这里插入图片描述
(3)输入“vim /etc/maxscale.cnf”命令,对MaxScale进行如下配置:

[maxscale]
threads=auto[server1]
type=server
address=192.168.174.136
port=3306[server2]
type=server
address=192.168.174.130
port=3306[MariaDB-Monitor]
type=monitor
module=mariadbmon
servers=server1,server2   #监控模块写入所有MariaDB节点
user=monitor
password=ymh123
monitor_interval=2s#本实验只涉及读写分离,不涉及只读,因此只读模块注释掉不配置
#[Read-Only-Service]
#type=service
#router=readconnroute
#servers=server1
#user=service_user
#password=service_pw
#router_options=slave[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2
user=maxscale
password=ymh123#本实验只涉及读写分离,不涉及只读,因此只读模块注释掉不配置
#[Read-Only-Listener]
#type=listener
#service=Read-Only-Service
#protocol=mariadbprotocol
#port=4008[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=mariadbprotocol
port=4006

(4)输入命令“systemctl restart maxscale”重启MaxScale;
在这里插入图片描述
(5)输入命令“maxctrl list servers”,若出现下图结果,则MaxScale读写分离搭建成功;
在这里插入图片描述
(6)查看MaxScale读写分离模块的监听端口,为默认的4006端口
在这里插入图片描述

5、读写分离验证

在读写分离验证之前,我们需要明确MariaDB主从架构的一个特点,即数据的同步只能是Master节点向Slave节点单方向进行同步。因此如果在Slave节点进行写操作,则写入的数据不会同步到Master节点中。
在这里插入图片描述
基于上述特性,如果通过MaxScale转发出来的写操作,写入的数据在Master和 Slave两个节点同时存在,则该写操作是由MaxScale分配到Master节点,然后由Master节点主从同步到Slave,步骤如下:
(1)打开数据库客户端,连接到MaxScale节点的4006端口;
在这里插入图片描述
(2)进行写操作,并验证Master和Slave节点是否同时存在写入的数据;
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
(3)通过SQL语句“select @@hostname”查看读操作访问的节点(需提前修改数据库服务器的主机名)
在这里插入图片描述
综上,读写分离配置成功。

6、注意事项

(1)创建用户时,监控用户monitor除赋予replication slave权限外,还要赋予super权限,否则MaxScale与MariaDB通过monitor用户建立监控进程时,会出现认证错误(Auth Error),这一点无论是在国内其他文章还是在官方英文文档中,都没有指明,如果出现认证错误,此时最佳的排查方式是查看路径为/var/log/maxscale/maxscale.log的日志内容;
在这里插入图片描述
在这里插入图片描述
(2)进行写操作后,不要单纯使用“select @@hostname”来判断写操作的节点,因为“select @@hostname”本身是读操作语句,只能判断自己,结果也一定是slave节点;
(3)OpenEuler系统本身暂未适配MaxScale,如果要在OpenEuler上安装MaxScale,最好的方式是使用容器版本的MaxScale;
(4)MaxScale不具备高可用功能,需要配合MariaDB的其他高可用方案使用。


http://www.ppmy.cn/server/121013.html

相关文章

Authentication plugin ‘caching _sha2_password’ cannot be loaded:

使用navicat连接mysql8.0以及以上版本的时候,出现这样的错误:“Authentication plugin ‘caching _sha2_password’ cannot be loaded:” 出现这个原因是MySQL8之前的版本中加密规则是mysql_native_password,而在MySQL8之后,加密规…

巴黎嫩事件对数据信息安全的影响及必要措施

2024年9月17日,黎巴嫩首都贝鲁特发生了多起小型无线电通信设备爆炸事件,导致伊朗驻黎巴嫩大使受轻伤。这一事件不仅引发了对安全的广泛关注,也对数据信息安全提出了新的挑战。 王工 18913263502 对数据信息安全的影响: 数据泄露风…

[leetcode刷题]面试经典150题之2移除元素(简单)

题目 移除元素 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素。元素的顺序可能发生改变。然后返回 nums 中与 val 不同的元素的数量。 假设 nums 中不等于 val 的元素数量为 k,要通过此题,您需要执行以下操作…

SpringBoot开发——集成Tess4j实现OCR图像文字识别

文章目录 1、准备工作2、配置Tess4j3、编写OCR服务4、创建控制器5、测试集成6、处理多语言与自定义字体7、总结 随着数字化转型的推进,光学字符识别( OCR, Optical Character Recognition)技术在各种应用场景中变得愈发重要。 OCR技术可以…

Redis——C++库redisplusplus在Linux环境下的安装

目录 第一步,安装hiredis第二步,下载redis源码第三步,编译/安装 redis-plus-plus使用redis-plus-plus(以Centos为例)Ubuntu的Makefile 第一步,安装hiredis redis-plus-plus 是基于 hiredis 实现的,而hiredis 是⼀个 C…

【编程基础知识】Mysql的各个索引数据结构及其适用场景

一、引言 在数据库的世界中,索引是提升查询速度的超级英雄。就像图书馔的目录帮助我们快速找到书籍一样,MySQL中的索引加速了数据检索的过程。本文将带你深入了解MySQL索引的多种数据结构、它们的适用场景以及如何巧妙地使用它们来优化性能。 二、索引…

《SmartX ELF 虚拟化核心功能集》发布,详解 80+ 功能特性和 6 例金融实践

《SmartX ELF 虚拟化核心功能集》电子书现已发布!本书详细介绍了 SmartX ELF 虚拟化及云平台核心功能,包含虚机服务、容器服务、网络服务、存储服务、运维管理、工具服务、数据保护等各个方面。 即刻下载电子书,了解如何利用基于 SmartX ELF …

如何确保Redis双删的准确性

为了保证Redis双删一致性的准确性,可以采取以下几种策略: 延时双删策略:在更新数据时,先删除缓存,再更新数据库,然后等待一段时间(例如几百毫秒到几秒),再次删除缓存。这…