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

MaxScale:MySQL读写分离实战指南

一、maxscale概述

MaxScale 数据库代理工具简介

MaxScale 是 MariaDB 公司开发的智能数据库代理和负载均衡工具,专门为 MySQL/MariaDB 数据库设计。

核心功能

  1. 负载均衡

    • 在多个数据库服务器间分配查询负载

    • 支持读写分离(主从架构)

  2. 高可用性

    • 自动故障检测和故障转移

    • 支持主从切换和自动重连

  3. 查询路由

    • 基于SQL语句内容的路由决策

    • 可将特定查询定向到特定服务器

  4. 安全功能

    • 数据库防火墙

    • 查询过滤和重写

    • 连接加密

主要特点

  • 完全兼容 MySQL 协议

  • 支持多种路由模块(读/写分离、分片等)

  • 可插拔架构,支持自定义模块开发

  • 提供REST API进行监控和管理

  • 支持二进制日志服务器功能

典型使用场景

  1. 作为MySQL/MariaDB集群的入口点

  2. 实现透明的读写分离

  3. 数据库连接池管理

  4. 在不修改应用代码的情况下扩展数据库架构

  5. 提供数据库访问的审计和监控层

MaxScale是maridb开发的一个mysql数据中间件,其配置简单,能够实现读写分离,并且可以根据主从状态实现写库的自动切换。

二、读写分离

1、环境说明

数据库角色IP应用与系统版本
master192.168.166.25rocky linux9.4 mysql-8.0.36
slave192.168.166.26rocky linux9.4 mysql-8.0.36
slave2192.168.166.27rocky linux9.4 mysql-8.0.36
maxscale192.168.166.9rocky linux9.4 maxscale24.02.3-GA

2、mysql主从复制配置

分别在主从三台服务器上安装mysql8,并配置主从复制。

#yum install -y mariadb mariadb-server
#systemctl start mariadb
yum install -y mysql mysql-server
##由于认证插件问题,需要在master服务器使用下述命令进行更改密码的验证插件。
ALTER USER'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;###修改认证插件
##my.cnf
default-authentication-plugin=mysql_native_password

3、maxscale安装

#下载yum源
[root@maxscale ~]# curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash# [info] Checking for script prerequisites.# [info] MariaDB Server version 11.2 is valid# [info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo# [info] Adding trusted package signing keys.../etc/pki/rpm-gpg ~
~# [info] Successfully added trusted package signing keys# [info] Cleaning package cache...25 files removed
[root@maxscale ~]# ls /etc/yum.repos.d/
mariadb.repo  rocky-addons.repo  rocky-devel.repo  rocky-extras.repo  rocky.repo
#安装maxscale
[root@maxscale ~]# yum -y install maxscale

4、配置maxscale

登录到主库

[root@master ~]# mysql -uroot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.36 MySQL Community Server - GPLCopyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#创建maxscale用户密码是maxscale
mysql> CREATE USER 'maxscale'@'%' IDENTIFIED BY 'maxscale';
Query OK, 0 rows affected (0.00 sec)
#授权maxscale可以查询所有数据库
mysql> GRANT SELECT ON mysql.* TO 'maxscale'@'%';
Query OK, 0 rows affected (0.01 sec)
#授权可以看所有数据库
mysql> GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';
Query OK, 0 rows affected (0.00 sec)
#创建admin用户可以在maxscale上登录
mysql> CREATE USER 'admin'@'192.168.166.%' IDENTIFIED BY 'admin';
Query OK, 0 rows affected (0.01 sec)

在maxscale上安装mysql

[root@maxscale ~]# yum -y install mysql
[root@maxscale ~]# which mysql
/usr/bin/mysql
#登录到master
[root@maxscale ~]# mysql -uadmin -padmin -h192.168.166.25
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.36 MySQL Community Server - GPLCopyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
####因为没有权限所以只能看到两个数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
+--------------------+
2 rows in set (0.00 sec)mysql> exit
Bye

回到master主库设置增删改查权限

mysql> GRANT CREATE, SELECT, INSERT, UPDATE, DELETE ON *.* TO 'admin'@'192.168.166.%';
Query OK, 0 rows affected (0.00 sec)

在maxscale上修改配置文件

[root@maxscale ~]# vim /etc/maxscale.cnf
#先看有无这个
[maxscale]
threads=auto#修改后端服务器地址
[server1]
type=server
address=192.168.166.25
port=3306
protocol=MySQLBackend[server2]
type=server
address=192.168.166.26
port=3306
protocol=MySQLBackend[server3]
type=server
address=192.168.166.27
port=3306
protocol=MySQLBackend#配置监控
[MySQL-Monitor]
type=monitor
module=mariadbmon
servers=server1,server2,server3
user=monitor
password=monitor
monitor_interval=2s#注释掉只读配置
#[Read-Only-Service]
#type=service
#router=readconnroute
#servers=server2
#user=maxscale
#password=maxscale
#router_options=slave#修改读写分离服务
[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
password=maxscale
version_string = 8.0   # mariadb的版本  version_string = 10.5
#配置listener
#注释掉只读
#[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=3306 #(伪装成数据库端口3306)

MariaDB数据库配置

[maxscale]
threads=auto
# 增加 MariaDB 兼容性配置
mariadb_compatible=1  # 启用 MariaDB 兼容模式
log_info=1  # 记录详细日志便于调试# 后端 MariaDB 服务器配置
[server1]
type=server
address=192.168.166.25
port=3306
protocol=MariaDBBackend  # 明确指定 MariaDB 后端协议(原 MySQLBackend 兼容但建议显式修改)
priority=1  # 可设置优先级,数字越小优先级越高(主库建议设为1)[server2]
type=server
address=192.168.166.26
port=3306
protocol=MariaDBBackend
priority=2  # 从库优先级略高[server3]
type=server
address=192.168.166.27
port=3306
protocol=MariaDBBackend
priority=3  # 从库优先级# MariaDB 监控配置(适配 Galera 集群或主从架构)
[MariaDB-Monitor]
type=monitor
module=mariadbmon  # MariaDB 专用监控模块(原配置正确,补充细节)
servers=server1,server2,server3
user=monitor  # 需在所有 MariaDB 节点创建该用户并授权
password=monitor
monitor_interval=2s
# 增加监控特性(根据实际架构选择)
# 若为 Galera 集群,启用以下配置:
# galera_monitor=1
# 若为主从架构,启用以下配置:
# detect_stale_master=1  # 检测失效主库
# failover_timeout=60s  # 故障转移超时时间# 读写分离服务(适配 MariaDB 读写逻辑)
[Read-Write-Service]
type=service
router=readwritesplit  # 读写分离路由(MariaDB 推荐使用)
servers=server1,server2,server3
user=maxscale  # 需在所有 MariaDB 节点创建该用户并授权(用于路由验证)
password=maxscale
version_string=10.6  # 匹配实际 MariaDB 版本(如 10.6、10.11 等,避免协议兼容问题)
# 增加 MariaDB 特有优化
master_failure_mode=failover  # 主库故障时自动故障转移
slave_selection_criteria=LEAST_CURRENT_OPERATIONS  # 选择负载最低的从库
strict_multi_master=0  # 禁用多主严格模式(适合主从架构)
disable_sescmd_history=1  # 禁用会话命令历史(减少内存占用)# 读写分离监听器
[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MariaDBProtocol  # 明确使用 MariaDB 协议(原 mariadbprotocol 兼容,建议标准化)
port=3306  # 伪装数据库端口,客户端可直接用 3306 连接
address=0.0.0.0  # 允许所有地址访问(生产环境建议指定具体IP)

切换到主库创建monitor用户

mysql> CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor';
Query OK, 0 rows affected (0.01 sec)
#再添加授权
mysql> GRANT REPLICATION CLIENT on *.* to 'monitor'@'%';
Query OK, 0 rows affected (0.00 sec)mysql> GRANT REPLICATION SLAVE on *.* to 'monitor'@'%';
Query OK, 0 rows affected (0.01 sec)mysql> GRANT SUPER,RELOAD on *.* to 'monitor'@'%';
Query OK, 0 rows affected, 1 warning (0.01 sec)

启动服务

[root@maxscale ~]# systemctl start maxscale

查看端口

[root@maxscale ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process    
LISTEN    0         4096             127.0.0.1:8989            0.0.0.0:*                 
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                 
LISTEN    0         4096                     *:3306                  *:*                 
LISTEN    0         128                   [::]:22                 [::]:*                 
[root@maxscale ~]# ss -antlp
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process                                                                                  
LISTEN     0          4096                127.0.0.1:8989                0.0.0.0:*         users:(("maxscale",pid=15450,fd=19))                                                    
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*         users:(("sshd",pid=792,fd=3))                                                           
LISTEN     0          4096                        *:3306                      *:*         users:(("maxscale",pid=15450,fd=27))                                                    
LISTEN     0          128                      [::]:22                     [::]:*         users:(("sshd",pid=792,fd=4))                                                           

查看有哪些服务

[root@maxscale ~]# maxctrl list services
┌────────────────────┬────────────────┬─────────────┬───────────────────┬───────────────────────────┐
│ Service            │ Router         │ Connections │ Total Connections │ Targets                   │
├────────────────────┼────────────────┼─────────────┼───────────────────┼───────────────────────────┤
│ Read-Write-Service │ readwritesplit │ 0           │ 0                 │ server1, server2, server3 │
└────────────────────┴────────────────┴─────────────┴───────────────────┴───────────────────────────┘

查看后台服务器有哪些

[root@maxscale ~]# maxctrl list servers
┌─────────┬─────────────────┬──────┬─────────────┬─────────────────┬──────┬───────────────┐
│ Server  │ Address         │ Port │ Connections │ State           │ GTID │ Monitor       │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ server1 │ 192.168.166.25 │ 3306 │ 0           │ Master, Running │      │ MySQL-Monitor │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ server2 │ 192.168.166.27 │ 3306 │ 0           │ Slave, Running  │      │ MySQL-Monitor │
├─────────┼─────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ server3 │ 192.168.166.26 │ 3306 │ 0           │ Slave, Running  │      │ MySQL-Monitor │
└─────────┴─────────────────┴──────┴─────────────┴─────────────────┴──────┴───────────────┘

测试

用客户机连接maxscale

username:admin
password:admin

会发现进行读操作时,是在slave的从数据库上执行;在进行写操作时,是在master主数据库上执

http://www.dtcms.com/a/485872.html

相关文章:

  • 基于Vue的体检中心管理系统的开发bk1825s9(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • RabbitMQ的概述
  • 使用PyTorch实现图像分类任务的全流程详解
  • JAVA代泊车接机送机服务代客泊车系统源码支持小程序+APP+H5
  • 吃谷机主题商城小程序的界面功能设计
  • 创建网站超市网络免费推广平台
  • 【征文计划】码上分享:基于 Rokid CXR-M SDK 构建「AI远程协作助手」实战全记录
  • PortSwigger靶场之CSRF where token is tied to non-session cookie通关秘籍
  • laya报错:GET http://xxx/bin/%22%22 404(Not Found)
  • 兴义市住房和城乡建设局网站莲花网站
  • 标题:Linux 系统中的“保险库管理员”:深入浅出理解 /etc/shadow 文件
  • CSS3》》 transform、transition、translate、animation 区别
  • HTML实现流星雨
  • JavaWeb-html、css-网页正文制作
  • GaussDB 分布式下, 报错concurrent update under Stream mode is not yet support
  • 服务器连接百度网盘并下载文件
  • 云计算实验3——CentOS中storm的安装
  • 一次被“动画关闭”启发的思考:Animate.css 与 prefers-reduced-motion 的无障碍设计
  • 《突破同质化:太空殖民地NPC行为差异化的底层架构》
  • 做网站ppt常见c2c网站有哪些
  • 专业手机网站建设价格明细表wordpress xiu 5.6
  • CSS 组合选择符详解
  • css:`target-before and :target-after 和 scroll-target-group`
  • 项目中执行SQL报错oracle.jdbc.OracleDatabaseException: ORA-00942: 表或视图不存在
  • 上门养老小程序源码 uniapp PHP MySQL
  • 供应链数据分析:Excel+Power BI双引擎打造智能供应链
  • 从零开始部署 GitLab CE 18.4.2:Docker Compose 新手教程
  • 高并发内存池日志
  • 使用PyTorch实现自定义损失函数以FocalLoss为例的详细教程
  • 《彻底理解C语言指针全攻略(4)--数组与指针的关系专题(下)》