Maxscale实现MySQL读写分离

作用:实现数据访问读写分离,减轻单台服务器的兵法访问压力
前提:实现主从同步
原理:
MySQL读写分离是指,作为后端mysql主从服务器的代理,它直接接受客户端的请求,对SQL语句进行分析,判断出是读操作还是写操作,然后分发至对应的mysql服务器上。让master处理写操作,让slave处理读操作。
非常适用于读操作量比较大的场景,可减轻master的压力。
mysql-proxy是官方提供的mysql中间件产品可以实现负载平衡,读写分离,failover等,还有一些第三方的中间价也可以实现此功能,Maxscale就是其中一种。

软件:mariadb-maxscale:由mysql兄弟公司Mariadb开发
下载软件:
浏览器搜索mariadb maxscale
https://mariadb.com/kb/en/maxscale/
主要配置文件:/etc/maxscale.cnf

一、规划

代理服务器IP:192.168.21.168
读服务IP:192.168.21.168
写服务器IP:192.168.21.169
mysql版本:5.7.27
maxscale版本:2.2.9
前提:已实现主从同步

二、安装配置Maxscale

1.安装MaxScale

[root@whx software]# rpm -ivh maxscale-2.2.9-1.centos.7.x86_64.rpm 

2.在主从库配置用户及权限(主库创建即可,从库自动同步)

#创建监控用户
(root@localhost:mysql.sock)[(none)]>grant replication slave,replication client on *.* to scalemon identified by '123456';
Query OK, 0 rows affected, 1 warning (0.06 sec)
#以下权限在Maxscale2.2.9版本中是必须的权限,否则无法启动
(root@localhost:mysql.sock)[(none)]>grant select on mysql.* to scalemon;

#创建路由用户
(root@localhost:mysql.sock)[(none)]>grant select on mysql.* to maxscale identified by '123456';
Query OK, 0 rows affected, 1 warning (0.10 sec)

#创建访问用户
(root@localhost:mysql.sock)[(none)]>grant all on *.* to student identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)

(root@localhost:mysql.sock)[(none)]>create database gamedb;

(root@localhost:mysql.sock)[(none)]>grant all on gamedb.* to student identified by '123456’;

3.修改配置文件

[root@whx software]# cp /etc/maxscale.cnf /etc/maxscale.cnf.bak

[root@whx software]# cat /etc/maxscale.cnf      #对照修改

# MaxScale documentation:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22/

# Global parameters
#
# Complete list of configuration options:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-mariadb-maxscale-configuration-usage-scenarios/

[maxscale]
threads=auto

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

[server1]
type=server
address=192.168.21.168
port=6001
protocol=MariaDBBackend

[server2]
type=server
address=192.168.21.169
port=6001
protocol=MariaDBBackend

# Monitor for the servers
#
# This will keep MaxScale aware of the state of the servers.
# MariaDB Monitor documentation:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-mariadb-monitor/

[MariaDB-Monitor]
type=monitor
module=mariadbmon
servers=server1,server2
user=scalemon
passwd=123456   #官网推荐是用加密文件
monitor_interval=2000

# Service definitions
#
# Service Definition for a read-only service and
# a read/write splitting service.
#

# ReadConnRoute documentation:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-readconnroute/

[Read-Only-Service]
type=service
router=readconnroute
servers=server1,server2
user=scalemon
passwd=123456
router_options=slave

# ReadWriteSplit documentation:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-readwritesplit/

[Read-Write-Service]
type=service
router=readwritesplit
servers=server1,server2
user=scalemon
passwd=123456

# This service enables the use of the MaxAdmin interface
# MaxScale administration guide:
# https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-maxadmin-admin-interface/

[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=MariaDBClient
port=4008

[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MariaDBClient
port=4006

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

删除配置文件中有#号的行

[root@whx software]# sed -i '/#/d' /etc/maxscale.cn

启动
Maxscale2.2.9版本不允许直接由root用户使用 maxscale -f /etc/maxscale.cnf启动服务,需要指定用户-U
Maxscale日志,数据位置,需要注意修改权限:/var/lib/maxscale、/var/log/maxscale、/var/run/maxscale

maxscale -f /etc/maxscale.cnf -U mysql
chown -R mysql.mysql /var/lib/maxscale
chown -R mysql.mysql /var/log/maxscale
chown -R mysql.mysql /var/run/maxscale

4.端口查看

[root@whx maxscale]# netstat -utnalp |grep maxscale
tcp        0      0 127.0.0.1:8989          0.0.0.0:*               LISTEN      18740/maxscale      
tcp        0      0 192.168.21.168:37008    192.168.21.169:6001     ESTABLISHED 18740/maxscale      
tcp        0      0 192.168.21.168:39848    192.168.21.168:6001     ESTABLISHED 18740/maxscale      
tcp6       0      0 :::4008                 :::*                    LISTEN      18740/maxscale      
tcp6       0      0 :::4006                 :::*                    LISTEN      18740/maxscale      

注:4006读写分离使用的端口
4008管理服务使用的端口

5.登陆

[root@whx maxscale]# maxadmin  -S /tmp/maxadmin.sock
MaxScale> help。#查看帮助
MaxScale> list clients
Client Connections
-----------------+------------------+----------------------+------------
 Client          | DCB              | Service              | Session
-----------------+------------------+----------------------+------------
 localhost       |        0x1cb1550 | MaxAdmin-Service     |  0x1cb2b30
-----------------+------------------+----------------------+------------

MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.21.168  |  6001 |           0 | Master, Running
server2            | 192.168.21.169  |  6001 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+——————————

使用代理端口,IP登陆数据库,可以进行读写操作

[root@whx maxscale]# mysql -h 192.168.21.168 -P4006 -ustudent -p

6.关闭maxscale服务

Killall -9 maxscale

你可能感兴趣的:(MySQL,mysql代理,Maxscale)