通过Maxscale代理,实现MySQL读写分离

一、搭建MySQL主从架构

    参加我的博文:AWS上搭建MySQL主从-配置半自动异步复制策略<https://mp.csdn.net/postedit/81222925>,搭建MySQL主从架构。

二、借助MySQL代理Maxscale实现SQL层读写分离

    MySQL代理的工具比较多,有MySQL Proxy,Maxscale,ProxySQL ,360的Atlas,实际安装之中,发现Atlas和maxscale好用,不会出现莫名其妙的问题,ProySQL安装了比较坑,在AWS EC2上安装遇到了莫名其妙的问题,在网上也没有找到合适的解决方案。所以放弃,最终成功在AWS EC2上安装的有Atlas和Maxscale,这两个都比较稳定,实际压力测试,稳定性不错。由于Atlas多年没有维护了,所以决定用maxscale作为代理工具。

三、安装步骤

1、安装MaxScale

wget https://downloads.mariadb.com/MaxScale/2.2.13/centos/7/x86_64/maxscale-2.2.13-1.centos.7.x86_64.rpm
yum localinstall maxscale-2.2.13-1.centos.7.x86_64.rpm -y

2、在Master数据库创建监听用户

CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor123';
GRANT SELECT,UPDATE,INSERT,DELETE ON bosma-smart to 'monitor'@'%';
flush privileges;

3、在Master数据库上执行创建可以读写的用户,maxscale。

CREATE USER 'maxscale'@'%' IDENTIFIED BY '123456';
GRANT SELECT ON mysql.user TO 'maxscale'@'%';
GRANT SELECT ON mysql.db TO 'maxscale'@'%';
GRANT SELECT ON mysql.tables_priv TO 'maxscale'@'%';
GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';
flush privileges;

4、授权访问业务数据库,这里我以business数据库为例。

GRANT SELECT,UPDATE,INSERT,DELETE ON business to 'maxscale'@'%';
GRANT ALL PRIVILEGES ON business.* TO 'ppd'@'%';
GRANT SELECT ON mysql.user TO 'maxscale'@'%';
GRANT SELECT ON mysql.db TO 'maxscale'@'%';
GRANT SELECT ON mysql.tables_priv TO 'maxscale'@'%';
GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';

5、编辑/etc/maxscale.cnf,配置读写分离策略

# MaxScale documentation on GitHub:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Documentation-Contents.md

# Global parameters
#
# Complete list of configuration options:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Getting-Started/Configuration-Guide.md

[maxscale]
threads=8

# Server definitions
#
# Set the address of the server to the network
# address of a MySQL server.
#

[server1]
type=server
address=172.31.5.106
port=3306
protocol=MySQLBackend
serv_weight=1

[server2]
type=server
address=172.31.48.255
port=3306
protocol=MySQLBackend
serv_weight=3

[server3]
type=server
address=172.31.5.107
port=3306
protocol=MySQLBackend
serv_weight=3
# Monitor for the servers
#
# This will keep MaxScale aware of the state of the servers.
# MySQL Monitor documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Monitors/MySQL-Monitor.md

[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3
user=monitor
passwd=monitor123
monitor_interval=10000
detect_stale_slave=true
# Service definitions
#
# Service Definition for a read-only service and
# a read/write splitting service.
#

# ReadConnRoute documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadConnRoute.md

#[Read-Only Service]
#type=service
#router=readconnroute
#servers=server1
#user=myuser
#passwd=mypwd
#router_options=slave

# ReadWriteSplit documentation:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadWriteSplit.md

[Read-Write Service]
type=service
router=readwritesplit
servers=server1,server2,server3
user=maxscale
passwd=maxscale
max_slave_connections=100%
weightby=serv_weight
# This service enables the use of the MaxAdmin interface
# MaxScale administration guide:
# https://github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Reference/MaxAdmin.md

[MaxAdmin Service]
type=service
router=cli

# Listener definitions for the services
#
# These listeners represent the ports the
# services will listen on.
#

#[Read-Only Listener]
#type=listener
#service=Read-Only Service
#protocol=MySQLClient
#port=4008

[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=6033

[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default

6、代码层

代码层需要修改端口为maxscale的地址和端口就OK,例如:

jdbc:mysql://172.31.5.203:6033/business?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull  

猜你喜欢

转载自blog.csdn.net/QFYJ_TL/article/details/82800206