当前位置: 首页 > article >正文

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

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_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS 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.kler.cn/news/317137.html

相关文章:

  • 深度学习(一)——CMC特刊推荐
  • 统一网关--gateway(仅供自己参考)
  • 深入探究PR:那些被忽视却超实用的视频剪辑工具
  • ES解说!
  • 【重学 MySQL】三十七、聚合函数
  • 【第十二章:Sentosa_DSML社区版-机器学习之回归】
  • expressjs 和 Router 配置 POST 请求
  • 智能算法躲避拥堵,高德企业用车上线“动态选路服务”为出行提效
  • Redis常用数据类型
  • 自动化测试常用函数
  • 数据结构 ——— 算法的空间复杂度
  • 使用 HFD 加快 Hugging Face 模型和数据集的下载,解决443报错
  • 逗号运算符
  • git报错:无法读取远程分支 ‘origin‘ does not appear to be a git repository
  • MySQL—多表操作详解
  • 【CSS in Depth 2 精译_038】6.2 CSS 定位技术之:绝对定位
  • 使用SBP打AssetBundle时脚本引用丢失
  • [Linux]ubuntu安装nvidia显卡驱动登录后黑屏
  • 通过 Flink 的火焰图定位反压
  • LabVIEW提高开发效率技巧----合理使用数据流与内存管理
  • MySQL篇(管理工具)
  • CPLD 工程师面试题
  • springboot结合p6spy进行SQL监控
  • Hadoop 性能优化高频面试题及答案
  • XSS—xss-labs靶场通关
  • codeforces round974 div3 分层图 树形dp
  • uniApp实现APP内自更新
  • 【OpenCV】场景中人的识别与前端计数
  • 针对论坛系统设计测试用例
  • 分布式难题-三座大山NPC