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

MariaDB MaxScale实现mysql8主从同步读写分离

一、MaxScale基本介绍

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

二、MaxScale实验环境

中间件192.168.121.51MaxScale 22.08.4
主服务器192.168.121.11mysql 8.0.30
从服务器192.168.121.12mysql 8.0.30
从服务器192.168.121.13mysql 8.0.30

三、实现数据库主从复制

(1)主库配置

[root@openEuler-1 ~]# tail -n4 /etc/my.cnf
server_id=11
gtid_mode=ON
enforce-gtid-consistency=ON
read_only=0
[root@openEuler-1 ~]# systemctl restart mysqld# 创建用户
create user 'rep'@'192.168.121.%' identified with mysql_native_password by '123456';
# 用户授权
grant replication slave on *.* to 'rep'@'192.168.121.%';

(2)从库配置()

[root@openEuler-2 ~]# tail -n4 /etc/my.cnf
server_id=12
gtid_mode=ON
enforce-gtid-consistency=ON
read_only=1
[root@openEuler-2 ~]# systemctl restart mysqld# 配置主从同步
CHANGE MASTER TO
MASTER_HOST = '192.168.121.11',
MASTER_USER = 'rep',
MASTER_PASSWORD = '123456',
MASTER_AUTO_POSITION = 1;# 启动主从同步
start slave

四、创建用户

在开始配置之前,需要在master中为 MaxScale 创建两个用户,用于监控模块和路由模块,已经实现主从复制的前提下,主库创建的用户,能同步到从库上

(1)创建监控用户

# 创建用户
mysql> create user 'maxscale_monitor'@'192.168.121.%' identified with mysql_native_password by 'Admin@123456';# 授权用户
mysql> grant replication slave, replication client on *.* to maxscale_monitor@'192.168.121.%';

(2)创建路由用户

# 创建用户
mysql> create user 'maxscale_route'@'192.168.121.%' identified with mysql_native_password by 'Admin@123456';# 授权用户
mysql> GRANT SHOW DATABASES ON *.* TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.user TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.db TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.tables_priv TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.columns_priv TO maxscale_route@'192.168.121.%';
mysql> GRANT SELECT ON mysql.proxies_priv TO maxscale_route@'192.168.121.%';

五、安装MaxScale

在 Download MariaDB Products & Tools | MariaDB 选择对应系统合适的版本下载安装,可供参考。

# 配置存储库源
[root@localhost ~]# curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash# 安装maxscale
[root@localhost ~]# dnf install maxscale -y

六、配置MaxScale

在/etc/maxscale.cnf.d新建一个配置my.cnf或者直接修改/etc/maxscale.cnf文件

[maxscale]
# 开启线程个数,默认为1.设置为auto会同cpu核数相同
threads=auto# 定义三台主机
[dbserv1]
type=server
address=192.168.121.11
port=3306
protocol=MariaDBBackend[dbserv2]
type=server
address=192.168.121.12
port=3306
protocol=MariaDBBackend[dbserv3]
type=server
address=192.168.121.13
port=3306
protocol=MariaDBBackend# 监视进程
[MySQL-Monitor]
type=monitor
module=mariadbmon
servers=dbserv1, dbserv2, dbserv3
user=maxscale_monitor
password=Admin@123456
monitor_interval=2s# 读写分离
[Read-Write-Service]
type=service
router=readwritesplit
servers=dbserv1,dbserv2,dbserv3
user=maxscale_route
password=Admin@123456
enable_root_user=true# 监听读写服务端口
[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MariaDBClient
address=0.0.0.0
port=3306

七、重启MaxScale服务

# 启动服务
[root@localhost ~]# maxscale -f /etc/maxscale.cnf.d/my.cnf -U maxscale# 查看运行状态
[root@localhost ~]# maxctrl list servers
┌─────────┬────────────────┬──────┬─────────────┬─────────────────┬──────┬───────────────┐
│ Server  │ Address        │ Port │ Connections │ State           │ GTID │ Monitor       │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ dbserv1 │ 192.168.121.11 │ 3306 │ 0           │ Master, Running │      │ MySQL-Monitor │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ dbserv2 │ 192.168.121.12 │ 3306 │ 0           │ Slave, Running  │      │ MySQL-Monitor │
├─────────┼────────────────┼──────┼─────────────┼─────────────────┼──────┼───────────────┤
│ dbserv3 │ 192.168.121.13 │ 3306 │ 0           │ Slave, Running  │      │ MySQL-Monitor │
└─────────┴────────────────┴──────┴─────────────┴─────────────────┴──────┴───────────────┘# 查看注册服务
[root@localhost ~]# maxctrl list services
┌────────────────────┬────────────────┬─────────────┬───────────────────┬───────────────────────────┐
│ Service            │ Router         │ Connections │ Total Connections │ Targets                   │
├────────────────────┼────────────────┼─────────────┼───────────────────┼───────────────────────────┤
│ Read-Write-Service │ readwritesplit │ 0           │ 0                 │ dbserv1, dbserv2, dbserv3 │
└────────────────────┴────────────────┴─────────────┴───────────────────┴───────────────────────────┘# 查看服务监听状态信息
[root@localhost ~]# maxctrl list listeners Read-Write-Service
┌─────────────────────┬──────┬─────────┬─────────┬────────────────────┐
│ Name                │ Port │ Host    │ State   │ Service            │
├─────────────────────┼──────┼─────────┼─────────┼────────────────────┤
│ Read-Write-Listener │ 3306 │ 0.0.0.0 │ Running │ Read-Write-Service │
└─────────────────────┴──────┴─────────┴─────────┴────────────────────┘

八、测试连接

(1)在master中创建一个访问用户,已经实现主从复制的前提下,主库创建的用户,能同步到从库上

mysql> create user 'admin_user'@'%' identified with mysql_native_password by 'Admin@123456';
mysql> grant all privileges on *.* to 'admin_user'@'%' with grant option;

(2)测试读写分离

[root@openEuler-2 ~]# mysql -uadmin_user -p'Admin@123456' -h 192.168.121.51 -e "select @@server_id"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          13 |
+-------------+
[root@openEuler-2 ~]# mysql -uadmin_user -p'Admin@123456' -h 192.168.121.51 -e "select @@server_id"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          12 |
+-------------+
[root@openEuler-2 ~]# mysql -uadmin_user -p'Admin@123456' -h 192.168.121.51 -e "begin;select @@server_id commit;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------+
| commit |
+--------+
|     11 |
+--------+

(3)也可以在从库中 slave(192.168.121.12) 新增一条数据,登录主库 master(192.168.121.11) 进行查询如果查询不到,在中间件连接库中 maxscale(192.168.121.51) 可以查询到则成功 ,但是不建议这样测试,会破坏主从同步。

九、配置脚本启动maxscale

[root@localhost ~]# ps -ef | grep maxscale
maxscale   10918       1  0 11:59 ?        00:00:09 maxscale -f /etc/maxscale.cnf.d/my.cnf -U maxscale
root       10964    1681  0 12:42 pts/0    00:00:00 grep --color=auto maxscale# 关闭进程,停止服务
[root@localhost ~]# killall -9 maxscale# 拷贝配置文件为主配置文件
[root@localhost ~]# cp /etc/maxscale.cnf.d/my.cnf /etc/maxscale.cnf
# 原来的重命名,防止出现报错
[root@localhost ~]# mv /etc/maxscale.cnf.d/my.cnf{,.bak}# 启动服务
[root@localhost ~]# systemctl enable --now maxscale

十、开启maxscale GUI

(1)在maxscale配置文件中[maxscale]下添加两行配置

[root@localhost ~]# vim /etc/maxscale.cnf
[root@localhost ~]# head -n4 /etc/maxscale.cnf
[maxscale]
threads=auto
admin_host=0.0.0.0
admin_secure_gui=false
[root@localhost ~]# systemctl restart maxscale

(2)访问 http://192.168.121.51:8989(默认账户密码如下)

http://www.lryc.cn/news/533766.html

相关文章:

  • 【python】简单的flask做页面。一组字母组成的所有单词。这里的输入是一组字母,而输出是所有可能得字母组成的单词列表
  • 单片机之基本元器件的工作原理
  • 吴恩达深度学习——卷积神经网络的特殊应用
  • 安宝特方案 | AR助力制造业安全巡检智能化革命!
  • Unity-Mirror网络框架-从入门到精通之Discovery示例
  • 项目的虚拟环境的搭建与pytorch依赖的下载
  • 现代前端工程化实践:高效构建的秘密
  • ARM Linux Qt使用JSON-RPC实现前后台分离
  • 【C++篇】C++11新特性总结1
  • 【Nginx + Keepalived 实现高可用的负载均衡架构】
  • 使用外骨骼灵活远程控制协作机器人案例
  • Centos Stream 10 根目录下的文件夹结构
  • python连点器
  • STM32G474--Whetstone程序移植(单精度)笔记
  • Spring Boot 3.4 中 MockMvcTester 的新特性解析
  • java 读取sq3所有表数据到objectNode
  • 网络计算机的五个组成部分
  • jakarta EE学习笔记-个人笔记
  • vue3-响应式 toRefs
  • 学习threejs,使用Lensflare模拟镜头眩光
  • redis高级数据结构布隆过滤器
  • mysql 5.7安装
  • Golang:精通sync/atomic 包的Atomic 操作
  • 微信小程序如何使用decimal计算金额
  • 最新Modular公司之MAX和Mojo作者 克里斯·拉特纳简介
  • Redis数据库篇 -- Pipeline
  • 爬虫自动化(DrissionPage)
  • 常见string库中的函数(C语言超详细)
  • 单例模式几种实现
  • android中关于CheckBox自定义选中图片选中无效问题