MySQL高可用架构之MaxScale实践
本次安装部署基于MHA已部署的情况下,只部署MaxScale,并且采用的是rpm包安装
MaxScale是mariadb公司开发的一套数据库中间件。它是一个支持高可用、读写分离、
负载均衡,并且具有良好的可扩展性,不但高性能的基于事件驱动,同时具有代理和管理功能。
1、MaxScale软件下载
下载地址:https://downloads.mariadb.com/files/MaxScale/
https://github.com/mariadb-corporation/MaxScale --源码下载
参考手册:https://downloads.mariadb.com/files/MaxScale/docs
https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale
2、解压安装包
(二进制安装包)
[root@node3 MaxScale]# tar -xzvf maxscale-1.4.3-1.rhel.6.x86_64.tar.gz
3、安装依赖包
配置本地yum源:
[root@node3 MaxScale]# mkdir /media/cdrom
[root@node3 MaxScale]# mount CentOS-6.4-x86_64-bin-DVD1.iso /media/cdrom/ -o loop
[root@node3 MaxScale]# rm -rf /etc/yum.repos.d/*.repo
[root@node3 MaxScale]# vi /etc/yum.repos.d/CentOS6.repo
[Base]
name=CentOS6 ISO Base
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0
依赖包检查安装:
yum install git gcc gcc-c++ ncurses-devel bison flex glibc-devel cmake libgcc perl make libtool openssl-devel libaio libaio-devel librabbitmq-devel libcurl-devel pcre-devel tcl tcl-devel systemtap-sdt-devel libuuid libuuid-devel
rpm -q libaio libaio-devel novacom-server libedit gcc gcc-c++ ncurses-devel bison glibc-devel cmake libgcc perl make libtool openssl-devel libaio libaio-devel librabbitmq-devel libcurl-devel pcre-devel
升级openssl
[root@node3 MaxScale]# rpm -Uvh openssl-1.0.1e-42.el6_7.1.x86_64.rpm --nodeps
[root@node3 MaxScale]# rpm -Uvh openssl-devel-1.0.1e-42.el6_7.1.x86_64.rpm
4、MaxScale安装和部署
MaxScale安装:
1)rpm包安装
[root@node3 MaxScale]# rpm -ivh maxscale-beta-2.0.0-1.centos.6.x86_64.rpm --nodeps
2)源码安装
(以下是源码编译,其中编译没通过,源码安装失败,建议在centos/rhel 7以上版本源码安装)
源码安装包要求:
CMake version 2.8 or later (Packaging requires version 2.8.12 or later)
GCC version 4.4.7 or later
libaio
OpenSSL
Bison 2.7 or later
Flex 2.5.35 or later
libuuid
rhel 5,6 :libedit-devel MariaDB-devel MariaDB-server
rhel 7 :mariadb-devel mariadb-embedded-devel libedit-devel
[root@node3 MaxScale]# cd MaxScale-2.0
[root@node3 MaxScale-2.0]# cmake ./
或者手动指定编译参数,如下
cmake ./ -DCMAKE_INSTALL_PREFIX=/usr/local/maxscale \
-D
MYSQL_DIR=/usr/local/mysql/include/ \
-DEMBEDDED_LIB=/usr/local/mysql/lib/libmysqld.a \
-DMYSQL_EMBEDDED_LIBRARIES=/usr/local/mysql/lib/ \
-DERRMSG=/usr/local/mysql/share/english/errmsg.sys
[root@node3 MaxScale-2.0]# make -j 8
[root@node3 MaxScale-2.0]# make install
3)二进制安装
[root@node3 MaxScale]# mv maxscale-1.4.3-1.rhel.6.x86_64 /usr/local/maxscale
在~/.bash_profile中添加以下环境变量
export MAXSCALE_HOME=/usr/local/maxscale
export LD_LIBRARY_PATH=/usr/local/maxscale/lib
MaxScale配置:
在 master 中为 MaxScale 创建两个用户,用于监控模块和路由模块
创建监控账户
mysql> create user maxscalemon@'%' identified by "monitor" ;
mysql> grant replication slave, replication client on *.* to maxscalemon@'%';
创建路由用户
mysql> create user maxscale@'%' identified by "maxscale";
mysql> grant select on mysql.* to maxscale@'%';
mysql> grant show databases on *.* to 'maxscale'@'%';
mysql> flush privileges;
编辑配置文件:
主要修改的文件有server1的IP地址和端口以及复制相应的server2,server3配置,修改监控和路由配置,清除只读服务配置。
[root@node3 MaxScale]# cp /etc/maxscale.cnf /etc/maxscale.cnf_20160823
[root@node3 MaxScale]# vi /etc/maxscale.cnf
[maxscale]
threads=1
[server1]
type=server
address=IP1
port=3306
protocol=MySQLBackend
[server2]
type=server
address=IP2
port=3306
protocol=MySQLBackend
[server3]
type=server
address=IP3
port=3306
protocol=MySQLBackend
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3
user=maxscalemon
passwd=monitor ##--改为加密的密码
monitor_interval=10000
[Read-Only Service]
type=service
router=readconnroute
servers=server1,server2,server3
user=maxscale
passwd=maxscale ##--改为加密的密码
router_options=slave
[Read-Write Service]
type=service
router=readwritesplit
servers=server1
user=maxscale
passwd=maxscale ##--改为加密的密码
max_slave_connections=100%
[MaxAdmin Service]
type=service
router=cli
[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=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
#socket=default
port=6603
加密密码
[root@node3 ~]# maxkeys /var/lib/maxscale
加密配置文件密码
[root@node3 ~]# maxpasswd /var/lib/maxscale/.secrets monitor
7429FE1AABA353442178F74131697531
[root@node3 ~]# maxpasswd /var/lib/maxscale/.secrets maxscale
26C20853B625AD18686C0D2AC8A11E60
将加密后的密码填写到配置文件中
启动maxscale服务:
[root@node3 MaxScale]# maxscale --config=/etc/maxscale.cnf
或者 maxscale -f /etc/maxscale.cnf
5、MaxScale使用
1) 通过service服务关闭、启动、重启、查看maxscale状态等
[root@node3
MaxScale]# service maxscale
Usage:
/etc/init.d/maxscale {start|stop|status|restart|condrestart|reload}
2) 启动maxscale
service maxscale start
maxscale -f /etc/maxscale.conf
3) 关闭maxscale
service
maxscale stop
4) 登陆maxscale管理控制台管理
[root@node3
MaxScale]# maxadmin -uadmin -pmariadb
-P6603
6、MaxScale读写分离和负载均衡测试
开启general log
mysql> show
variables like 'general_log';
mysql> set
global general_log=1;
[root@node3 MaxScale]# mysql -udbadmin -pdbadmin
-hip3 -P4008 -e "select * from dbtest.t2"
[root@node2 ~]#
tailf /usr/local/mysql/data/node2.log
166 Connect dbadmin@node3 on
166 Query select @@version_comment limit 1
166 Query select * from dbtest.t1
[root@node3 MaxScale]# mysql -udbadmin -pdbadmin
-hip3 -P4008 -e "select * from dbtest.t2"
[root@node3 ~]#
tailf /usr/local/mysql/data/node3.log
160825
14:25:29 208 Connect dbadmin@node3 on
208 Query select @@version_comment limit 1
208 Query select * from dbtest.t2
208 Quit
179 Query SELECT @@server_id
179 Query SHOW SLAVE STATUS
160825
14:25:30 209 Connect dbadmin@node3 on
209 Query select @@version_comment limit 1
209 Query select * from dbtest.t2
209 Quit
160825
14:25:31 204 Query SELECT @@server_id
204 Query SHOW SLAVE STATUS
[root@node3 MaxScale]# mysql -udbadmin -pdbadmin
-hip3 -P4006 -e "insert into dbtest.t2 values(1111)"
[root@node1 ~]#
tailf /usr/local/mysql/data/node1.log
160825
14:26:53 203 Connect dbadmin@node3 on
203 Query select @@version_comment limit 1
203 Query insert into dbtest.t2 values(1111)
203 Quit
7、MaxScale安装错误信息以及解决方案
错误信息01:
Initialized empty Git repository in /tools/MaxScale/MaxScale-2.0/build/connector-c-prefix/src/connector-c/.git/
error: Couldn't resolve host 'github.com' while accessing https://github.com/MariaDB/mariadb-connector-c.git/info/refs
fatal: HTTP request failed
Initialized empty Git repository in /tools/MaxScale/MaxScale-2.0/build/connector-c-prefix/src/connector-c/.git/
error: Couldn't resolve host 'github.com' while accessing https://github.com/MariaDB/mariadb-connector-c.git/info/refs
fatal: HTTP request failed
Initialized empty Git repository in /tools/MaxScale/MaxScale-2.0/build/connector-c-prefix/src/connector-c/.git/
error: Couldn't resolve host 'github.com' while accessing https://github.com/MariaDB/mariadb-connector-c.git/info/refs
fatal: HTTP request failed
-- Had to git clone more than once:
3 times.
CMake Error at /tools/MaxScale/MaxScale-2.0/build/connector-c-prefix/tmp/connector-c-gitclone.cmake:40 (message):
Failed to clone repository:
'https://github.com/MariaDB/mariadb-connector-c.git'
make[2]: *** [connector-c-prefix/src/connector-c-stamp/connector-c-download] Error 1
make[1]: *** [CMakeFiles/connector-c.dir/all] Error 2
make: *** [all] Error 2
解决方案:
上述错误是源码编译报错,暂无好的解决方案,如果可以连接外网可以通过源码编译安装或者使用高版本os的
服务器。
错误信息02:
2016-08-23 18:26:54 notice : Loaded module mysqlmon: V1.4.0 from /usr/lib64/maxscale/libmysqlmon.so
2016-08-23 18:26:54 notice : Encrypted password file /var/lib/maxscale/.secrets can't be accessed (No such file or directory). Password encryption is not used.
2016-08-23 18:26:54 error : 1 errors were encountered while processing the configuration file '/etc/maxscale.cnf'.
2016-08-23 18:26:54 error : Failed to open, read or process the MaxScale configuration file /etc/maxscale.cnf. Exiting.
2016-08-23 18:26:54 MaxScale is shut down.
解决方案:
创建密码文件,并将加密后的密码在配置文件中替换。
maxkeys /var/lib/maxscale
maxpasswd /var/lib/maxscale/.secrets monitor
maxpasswd /var/lib/maxscale/.secrets maxscale
错误信息03:
2016-08-24 11:48:43 notice : Loaded module readconnroute: V1.1.0 from /usr/lib64/maxscale/libreadconnroute.so
2016-08-24 11:48:43 error : The service 'Read-Only Service' is missing a definition of the servers that provide the service.
解决方案:
将配置文件中的readonly内容补全,内容如下
[Read-Only Service]
type=service
router=readconnroute
servers=
user=maxscale
passwd=26C20853B625AD18686C0D2AC8A11E60
router_options=slave
错误信息04:
rpm -ivh maxscale-1.4.3-1.rhel.6.x86_64.rpm
warning: maxscale-1.4.3-1.rhel.6.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 8167ee24: NOKEY
error: Failed dependencies:
libcrypto.so.10(libcrypto.so.10)(64bit) is needed by maxscale-1.4.3-1.x86_64
libssl.so.10(libssl.so.10)(64bit) is needed by maxscale-1.4.3-1.x86_64
解决方案:
通过升级openssl解决,rpm -Uvh openssl-1.0.1e-42.el6_7.1.x86_64.rpm --nodeps和rpm -Uvh openssl-devel-1.0.1e-42.el6_7.1.x86_64.rpm 。
8、MaxScale附录
1)rpm默认安装包相关路径
/usr/share/maxscale/ 共享配置安装路径
/var/lib/maxscale 数据文件目录
/usr/bin/maxscale 执行文件目录
/var/log/maxscale 日志目录
/usr/lib64/maxscale 库文件目录
2)配置文件详解
[maxscale]
threads=auto #开启线程个数,默认为1.设置为auto会同cpu核数相同
ms_timestamp=1 #timestamp精度
syslog=1 #将日志写入到syslog中
maxlog=1 #将日志写入到maxscale的日志文件中
log_to_shm=0 #不将日志写入到共享缓存中,开启debug模式时可打开加快速度
log_warning=1 #记录告警信息
log_notice=1 #记录notice
log_info=1 #记录info
log_debug=0 #不打开debug模式
log_augmentation=1 #日志递增
#相关目录设置 ,如果改变rpm的日志和数据文件路径,需要通过创建相应目录并改变相应属主为maxscale。
logdir=/usr/local/maxscale/log/
datadir=/usr/local/maxscale/data/
libdir=/usr/lib64/maxscale/
cachedir=/usr/local/maxscale/cache/
piddir=/usr/local/maxscale/
execdir=/usr/bin/
#相关的监控信息,监控的用户需要对后端数据库有访问replication client的权限:grant replication client
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3
user=root
passwd=7AE087FBF864EBB87D108C3AB1603D0D
monitor_interval=1000 #监控心跳为1秒
detect_replication_lag=true #监控主从复制延迟,可用后续指定router service的max_slave_replication_lag单位是秒,来控制maxscale运行的最大延迟
detect_stale_master=true #当复制slave全部断掉时,maxscale仍然可用,将所有的访问指向
#read-only的只读节点slave分离
[Read-Only Service]
type=service
router=readconnroute
servers=server1,server2,server3
user=root
passwd=7AE087FBF864EBB87D108C3AB1603D0D
router_options=slave
enable_root_user=1
#读写分离,用户需要有SELECT ON mysql.db;SELECT ON mysql.tables_priv;SHOW DATABASES ON *.*的权限
[Read-Write Service]
type=service
router=readwritesplit
servers=server1,server2,server3
user=root
passwd=7AE087FBF864EBB87D108C3AB1603D0D
use_sql_variables_in=master #sql语句中的存在变量只指向master中执行
enable_root_user=1 #允许root用户登录执行
# master_accept_reads=true #master节点也可以转发读请求
max_slave_replication_lag=5 #复制延迟最大为5秒(必须比monitor的interval大)
3)maxscale缺点
1)创建链接的时候,不支持压缩协议
2)转发路由不能动态的识别master节点的迁移
3)LONGLOB字段不支持
4)在一下情况会将语句转到master节点中(保证事务一致):
明确指定事务;
prepared的语句;
语句中包含存储过程,自定义函数
包含多条语句信息:INSERT INTO ... ; SELECT LAST_INSERT_ID();
5)一些语句默认会发送到后端的所有server中,但是可以指定use_sql_variables_in=[master|all] (default: all)
6)maxscale不支持主机名匹配的认证模式,只支持IP地址方式的host解析。所以在添加user的时候记得使用合适的范式。
7)跨库查询不支持,会显示的指定到第一个数据库中
8)通过select方式改变会话变量的行为不支持