Skip to content

MaxScale 实现读写分离和负载

概述

多台数据库已经配置好主从复制

安装

shell
    yum install -y https://downloads.mariadb.com/MaxScale/2.5.6/centos/7/x86_64/maxscale-2.5.6-1.rhel.7.x86_64.rpm

配置

在master库创建两个用户(监控用户和路由用户)

sql
    #监控用户
    grant replication slave, replication client on *.* to scalemonx@'%' identified by "xxx";
    #路由用户
    grant select  on mysql.* to maxscalex@'%' identified by 'xxx';
    #刷新数据库配置
    flush privileges;

配置maxscale.cnf

shell
    #默认配置文件在/etc 目录下
    vim /etc/maxscale.cnf
    [server1]
    type=server
    address=127.0.0.1
    port=3306
    protocol=MariaDBBackend
    [server2]
    type=server
    address=127.0.0.1
    port=3306
    protocol=MariaDBBackend
   [Splitter-Service]
    type=service
    router=readwritesplit
    servers=server1,server2
    user=''
    password=''
    [Splitter-Listener]
    type=listener
    service=Splitter-Service
    protocol=MariaDBClient
    port=4006
    #注释块
    #[Read-Only Service]
    #...
    #注释块
    #[Read-Only-Listener]
    #...

启动maxscale

shell
 maxscale -f /etc/maxscale.cnf -U maxscale
 ps -ef | grep maxscale #查看进程
 netstat -ntelp #查看端口 6603

命令行查看运行状态

shell
 maxctrl list servers

┌─────────┬────────────────┬──────┬─────────────┬───────────────────────────────────┬──────┐ │ Server │ Address │ Port │ Connections │ State │ GTID │ ├─────────┼────────────────┼──────┼─────────────┼───────────────────────────────────┼──────┤ │ server1 │ 127.0.0.1 │ 3306 │ 0 │ Master, Running │ │ ├─────────┼────────────────┼──────┼─────────────┼───────────────────────────────────┼──────┤ │ server2 │ xxxx.xxxx.xxxx │ 3306 │ 0 │ Slave of External Server, Running │ │ └─────────┴────────────────┴──────┴─────────────┴───────────────────────────────────┴──────┘

查看服务监听状态信息

shell
 maxctrl list listeners Read-Write-Service

┌─────────────────────┬──────┬──────┬─────────┬────────────────────┐ │ Name │ Port │ Host │ State │ Service │ ├─────────────────────┼──────┼──────┼─────────┼────────────────────┤ │ Read-Write-Listener │ 4006 │ :: │ Running │ Read-Write-Service │ └─────────────────────┴──────┴──────┴─────────┴────────────────────┘

GUI WEB 页面访问 http://xx.xx.xx.xx:8989 user admin pass mariadb