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

ops/2024/9/24 12:15:12/

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/ops/115278.html

相关文章

AUTOSAR_EXP_ARAComAPI的5章笔记(7)

☞ 返回总目录 5.3.6 Methods 对于远程服务提供的每种方法,Proxy Class都包含一个特定于该方法的包装类的成员。 在我们的示例中,有三种方法,相应的成员分别名为 Calibrate(类型为 methods::Calibrate)、Adjust&…

【企业微信】群机器人自动消息配置

0、群聊机器人 内部企微群聊可以添加一个机器人,这个机器人其实是个消息接口,可以外部脚本来自动定时发送消息到群里,打工人最有用的提醒就是每周提醒发周报了。 1、创建机器人 一般公司都没有人使用,我们可以手动创建一个。 …

基于 BERT 的自定义中文命名实体识别实现

基于 BERT 的自定义中文命名实体识别实现 在自然语言处理中,命名实体识别(Named Entity Recognition,NER)是一项重要的任务,旨在识别文本中的特定实体,如人名、地名、组织机构名等。本文将介绍如何使用 BERT 模型实现自定义中文命名实体识别,并提供详细的代码分析和解读…

LabVIEW界面输入值设为默认值

在LabVIEW中,将前面板上所有控件的当前输入值设为默认值,可以通过以下步骤实现: 使用控件属性节点:你可以创建一个属性节点来获取所有控件的引用。 右键点击控件,选择“创建” > “属性节点”。 设置属性节点为“D…

[游戏技术]L4D服务器报错解决

服务器报错: CreateBoundSocket: :bind to port 0 returned error no name available 公网端口未开放,STEAM服务器无法访问

基于二自由度汽车模型的汽车质心侧偏角估计

一、质心侧偏角介绍 在车辆坐标系中,质心侧偏角通常定义为质心速度方向与车辆前进方向的夹角。如下图所示,u为车辆前进方向,v为质心速度方向,u和v之间的夹角便是质心侧偏角。 质心侧偏角的作用有如下三点: 1、稳定性…

【Verilog学习日常】—牛客网刷题—Verilog快速入门—VL59

根据RTL图编写Verilog程序 描述 根据以下RTL图,使用 Verilog HDL语言编写代码,实现相同的功能,并编写testbench验证功能。 输入描述: clk:系统时钟信号 rst_n:复位信号,低电平有效 data_in…

第6天:趋势轮动策略开发(年化18.8%,大小盘轮动加择时)

原创内容第655篇,专注量化投资、个人成长与财富自由。 轮动策略是一种投资策略,它涉及在不同的资产类别、行业或市场之间进行切换,以捕捉市场机会并优化投资组合的表现。 这种策略的核心在于识别并利用不同资产或市场的相对强弱&#xff0c…