环境:
CentOS7.2
maxscale2.0.4
mariadb10.1.21
192.168.8.254 maxscale
192.168.8.101 master
192.168.8.102 slave
192.168.8.103 slave
一.mariadb主辅环境
请参看MariaDB Replication
二.maxscale
1.安装maxscale
https://downloads.mariadb.com/MaxScale/
https://github.com/mariadb-corporation/MaxScale
https://mariadb.com/kb/en/mariadb-enterprise/5929/
rpm -ivh
https://downloads.mariadb.com/MaxScale/2.0.4/centos/7/x86_64/maxscale-2.0.4-1.centos.7.x86_64.rpm
2.master数据库节点中创建监控和路由用户
监控用户
CREATE USER scalemon@'%' IDENTIFIED BY
'maxscale';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO
scalemon@'%';
路由用户
CREATE USER maxscale@'%' IDENTIFIED BY
'maxscale';
GRANT SELECT ON mysql.* TO
maxscale@'%';
3.配置maxscale
https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-configuration-usage-scenarios/
https://mariadb.com/kb/en/mariadb-enterprise/readwrite-splitting-with-mysql-replication/
更多详细解释请参看官方帮助
ii.生成密文密码
maxkeys
maxpasswd /var/lib/maxscale/.secrets maxscale
chown maxscale: /var/lib/maxscale/.secrets
ii.主配置
cat >/etc/maxscale.cnf
<<HERE
[maxscale]
threads=auto
[server1]
type=server
address=192.168.8.101
port=3306
protocol=MySQLBackend
[server2]
type=server
address=192.168.8.102
port=3306
protocol=MySQLBackend
serv_weight=6
[server3]
type=server
address=192.168.8.103
port=3306
protocol=MySQLBackend
serv_weight=4
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3
user=scalemon
passwd=E0BA10ADBCDE2E76F160DCE864AC90EC
monitor_interval=10000
[Read-Write Service]
type=service
router=readwritesplit
servers=server1
user=maxscale
passwd=E0BA10ADBCDE2E76F160DCE864AC90EC
max_slave_connections=100%
[Read-Only Service]
type=service
router=readconnroute
servers=server2,server3
user=maxscale
passwd=E0BA10ADBCDE2E76F160DCE864AC90EC
router_options=slave
weightby=serv_weight
[MaxAdmin Service]
type=service
router=cli
[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
HERE
https://mariadb.com/kb/en/mariadb-enterprise/5943/
MaxAdmin默认监听Unix domain
socket,仅在maxscale主机才能查看管理信息,更安全。
监听tcp端口只需加上port=xxx,如果没address=x.x.x.x则监听在0.0.0.0,比较危险,请慎用。
[MaxAdmin
Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
port=6603
4.启动maxscale
systemctl start maxscale
maxadmin -uadmin -pmariadb list servers
提示:启用tcp监听后需要认证,默认用户名和密码admin/mariadb
5.logrotate
cat >/etc/logrotate.d/maxscale <<HERE
/var/log/maxscale/*.log {
monthly
rotate 5
missingok
nocompress
sharedscripts
postrotate
# run if maxscale is running
if test -n "`ps acx|grep maxscale`"; then
/usr/bin/maxadmin flush logs
fi
endscript
}
HERE
6.测试智能读写分离
先在master节点创建test用户
GRANT ALL PRIVILEGES ON *.* TO test@'%' IDENTIFIED BY 'test';
再通过maxscale连接测试
mysql -h 192.168.8.254 -P 4006 -utest -ptest
select @@hostname;
start transaction;
select @@hostname;
普通查询在node2.example.com(slave)上,开启事务后就自动路由到了node1.example.com(master)。智能路由不是吹的
小问题
https://jira.mariadb.org/browse/MXS-716?jql=project
= MXS AND text ~ "ERROR 1045 (28000):"
实测,通过maxscale访问,如果加上库名则不管权限给多大都会报权限拒绝,但不加库则可直接登录
三.maxscale高可用
https://mariadb.com/kb/en/mariadb-enterprise/how-to-make-mariadb-maxscale-high-available/
https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-ha-with-lsyncd/