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数据中间件,其配置简单,能够实现读写分离,并且可以根据主从状态实现写库的自动切换。
官网:https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-20/

二、读写分离

1、mysql主从复制配置

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

##由于认证插件问题,需要在master服务器使用下述命令进行更改密码的验证插件。
ALTER USER'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;

###修改认证插件
##my.cnf
default-authentication-plugin=mysql_native_password

2、maxscale安装

[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

3、配置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 - GPL

Copyright (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.158.%' 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 - GPL

Copyright (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
#配置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)

切换到主库创建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主数据库上执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值