linuxea:zabbix数据库使用mysql-proxy读写分离

最早使用程序实现的读写不利于扩展,依赖太高。
mysql-proxy通过lua脚本来实现读写的分离,当访问数据库的过程中有一个中间件可以将增删改查的语句进行转发到后端的服务器上,如,写操作到主服务器,读操作到从服务器(暂且不提主从实现),那么读多写少的环境中,多台从库分摊到读操作,在这个lua脚本中明确的写明当什么样的条件下将语句发送到什么样的服务器上!
我们借鉴上一篇的MariaDB Galera Cluster配置使用集群做读写分离,在之前是内网的zabbix,现在扩展集群后并加入mysql-proxy中间件测试。如下图

download:https://downloads.mysql.com/archives/proxy/

[root@LinuxEA ~]# wget https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
[root@LinuxEA ~]# useradd -r mysql-proxy -s /sbin/nologin 
[root@LinuxEA ~]# tar xf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local/
[root@LinuxEA ~]# cd /usr/local/
[root@LinuxEA /usr/local]# ln -s mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit mysql-proxy

配置启动可以直接在mysql-proxy后面加参数启动和指定配置文件启动

--help-all :获取全部帮助信息;
-proxy-address-=host:port :代理服务监听的地址和端口;
--admin-address=host:port :管理模块监听的地址和端口;
--proxy-backend-addresses=host:port :后端mysql服务器的地址和端口;
--proxy-read-only-backend-addresses=host:port :后端只读mysql服务器的地址和端口;
--proxy-lua-script=file_name :完成mysql代理功能的Lua脚本;
--daemon :以守护进程模式启动mysql-proxy;
--keepalive :在mysql-proxy崩溃时尝试重启之;
--log-file=/path/to/log_file_name :日志文件名称;
--log-level=level :日志级别;
--log-use-syslog :基于syslog记录日志;
--plugins=plugin:在mysql-proxy启动时加载的插件;
--user=user_name :运行mysql-proxy进程的用户;
--defaults-file=/path/to/conf_file_name : 默认使用的配置文件路径;其配置段使用[mysql-proxy]标识;
--proxy-skip-profiling : 禁用profile;
启动参数:
 --plugins=proxy        #指定proxy插件,该配置写入配置文件无法启动
 --plugins=admin       #指定admin插件
 --defaults-file=/etc/mysql-proxy.conf           #指定配置文件

创建配置文件:/etc/mysql-proxy.conf

[mysql-proxy]
#以后台守护进程方式启动
daemon=true      
#当进程故障后自动重启
keepalive=true   
#设置日志级别为debug,可以在调试完成后改成info                
log-level=debug
#设置日志文件路径       
log-file=/var/log/mysql-proxy.log
#设置mysql-proxy的家目录
basedir=/usr/local/mysql-proxy
#指定mysql-proxy的监听地址                  
proxy-address=10.10.0.98:4040
#设置后台master服务器             
proxy-backend-addresses=10.0.1.49:3306
#设置后台从服务器                       
proxy-read-only-backend-addresses=10.0.1.61:3306,10.10.240.113:3306
#设置读写分离脚本路径                  
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua    
#设置mysql-proxy管理地址,需要admin插件 
admin-address=10.10.0.98:4041
#设置登录管理地址用户和密码
admin-username=admin                                  
admin-password=admin
#设置管理后台lua脚本路径,改脚本默认没有要自动定义
admin-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua

启动

[root@LinuxEA ~]# chmod 660 /etc/mysql-proxy.conf 
[root@LinuxEA ~]# chown -R  mysql-proxy /usr/local/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit/
[root@LinuxEA ~]# /usr/local/mysql-proxy/bin/mysql-proxy --plugins=proxy --plugins=admin --defaults-file=/etc/mysql-proxy.conf 
[root@LinuxEA ~]# ss -tlnp|grep mysql-proxy
LISTEN     0      128    10.10.0.98:4040                     *:*                   users:(("mysql-proxy",pid=15648,fd=10))
LISTEN     0      128    10.10.0.98:4041                     *:*                   users:(("mysql-proxy",pid=15648,fd=11))
[root@LinuxEA ~]# 

安装客户端测试:

[root@LinuxEA ~]# cat > /etc/yum.repos.d/MariaDB.repo <<EOF
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
[root@LinuxEA ~]# yum install MariaDB-client -y

授权zabbix-server用户

MariaDB [(none)]> GRANT ALL  ON zabbix.* To 'zabbix'@'10.10.0.98' IDENTIFIED BY 'password';
Query OK, 0 rows affected (1.34 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.46 sec)

测试登陆:

[root@LinuxEA ~]# mysql -uzabbix -ppassword -h10.10.0.98 -P4040
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 398742
Server version: 10.0.30-MariaDB-wsrep MariaDB Server, wsrep_25.19.rc3fc46e

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| zabbix             |
+--------------------+
2 rows in set (0.01 sec)

MariaDB [(none)]> 

修改zabbix配置,并且重启,进行测试

[root@LinuxEA ~]# egrep -v "^$|^#" /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
DBHost=10.10.0.98
DBName=zabbix
DBUser=zabbix
DBPassword=password
DBPort=4040
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000

在查看,up有两个
mysql-proxy会检测客户端连接,当连接没有超过min_idle_connections预设值时, 不会进行读写分离, 即查询操作会发生到Master上。
我们可以修改,原来大概是这样的:

[root@Linuxea ~]# vim /usr/local/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit/share/doc/mysql-proxy/rw-splitting.lua
37 -- connection pool
 38 if not proxy.global.config.rwsplit then
 39 |   proxy.global.config.rwsplit = {
 40 |   |   min_idle_connections = 4, #超过4个才会进行读写分离
 41 |   |   max_idle_connections = 8, #默认8 ,修改最大连接为2,当终端超过2个,就会开启读写分离
 42 
 43 |   |   is_debug = false
 44 |   }
 45 end
[root@LinuxEA ~]# mysql -uadmin -padmin -h10.10.0.98 -P4041
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> select * from help;
+------------------------+------------------------------------+
| command                | description                        |
+------------------------+------------------------------------+
| SELECT * FROM help     | shows this help                    |
| SELECT * FROM backends | lists the backends and their state |
+------------------------+------------------------------------+
2 rows in set (0.00 sec)

MySQL [(none)]> select * from backends;
+-------------+--------------------+-------+------+------+-------------------+
| backend_ndx | address            | state | type | uuid | connected_clients |
+-------------+--------------------+-------+------+------+-------------------+
|           1 | 10.0.1.49:3306     | up    | rw   | NULL |                 0 |
|           2 | 10.0.1.61:3306     | up    | ro   | NULL |                 0 |
|           3 | 10.10.240.113:3306 | up    | ro   | NULL |                 0 |
+-------------+--------------------+-------+------+------+-------------------+
3 rows in set (0.00 sec)

zabbix-server链接:

[root@DS-VM-Linuxea ~]# egrep -v "^$|^#" /etc/zabbix/zabbix_server.conf 
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
DBHost=10.10.0.98
DBName=zabbix
DBUser=zabbix
DBPassword=password
DBPort=4040
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
2 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2017-07-13分类: Mariadb

标签: mariadb, 读写分离

发表评论