一、Mysql-Proxy 简单介绍
MySQL-Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测、分析或改变它们的通信。它使用灵活,没有限制,常见的用途包括:负载平衡,故障、查询分析,查询过滤和修改等等。
MySQL-Proxy就是这么一个中间层代理,简单的说,MySQL-Proxy就是一个连接池,负责将前台应用的连接请求转发给后台的数据库,并且通过使用lua脚本,可以实现复杂的连接控制和过滤,从而实现读写分离和负载平衡。对于应用来说,MySQL-Proxy是完全透明的,应用则只需要连接到MySQL-Proxy的监听端口即可。当然,这样proxy机器可能成为单点失效,但完全可以使用多个proxy机器做为冗余,在应用服务器的连接池配置中配置到多个proxy的连接参数即可。
MySQL-Proxy更强大的一项功能是实现“读写分离”,基本原理是让主数据库处理事务性查询,让从库处理SELECT查询。数据库复制被用来把事务性查询导致的变更同步到集群中的从库。
二、环境介绍
1、实验拓扑
2、环境介绍
3、时间同步
[root@proxy ~]# ntpdate 202.120.2.101 [root@master ~]# ntpdate 202.120.2.101 [root@slave ~]# ntpdate 202.120.2.101
三、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 ―――― 完成mysql代理功能的Lua脚本
--daemon ―――― 以守护进程模式启动mysql-proxy
--defaults-file=/path/to/conf_file_name : 默认使用的配置文件路径
--log-file=/path/to/log_file_name: 日志文件名称
--log-level=level :日志级别
--log-use-syslog:基于syslog记录日志
--user=user_name: 运行mysql-proxy进程的用户
四、配置Mysql-Proxy主从分离
声明:在在做Mysql-Proxy主从分离时由于自己的虚拟机已经还原,故一直没有想到我的主从还没有先搭建主从同步环境,于是一直按别人的实验走下去,到最后发现别人的结果相差太远,故浪费了不少时间,这里说明在做此实验时一定要现将你的主从复制做好,在接下来做这个实验。
1、增加代理用户
[root@proxy ~]# groupadd mysql-proxy [root@proxy ~]# useradd -g mysql-proxy -s /sbin//nologin -M mysql-proxy [root@proxy ~]# id mysql-proxy uid=1002(mysql-proxy) gid=1002(mysql-proxy) 组=1002(mysql-proxy)
2、安装mysql-proxy
[root@proxy src]# wget http://mirrors.sohu.com/mysql/MySQL-Proxy/mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz [root@proxy src]# tar -xf mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local/ [root@proxy src]# cd /usr/local/ [root@proxy local]# ln -sv mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit mysql-proxy "mysql-proxy" -> "mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit" [root@proxy local]# cd mysql-proxy [root@proxy mysql-proxy]# chown -R mysql-proxy.mysql-proxy /usr/local/mysql-proxy/* [root@proxy mysql-proxy]# ll 总用量 24 drwxr-xr-x 2 mysql-proxy mysql-proxy 4096 12月 24 2013 bin drwxr-xr-x 2 mysql-proxy mysql-proxy 4096 12月 24 2013 include drwxr-xr-x 6 mysql-proxy mysql-proxy 4096 12月 24 2013 lib drwxr-xr-x 2 mysql-proxy mysql-proxy 4096 12月 24 2013 libexec drwxr-xr-x 8 mysql-proxy mysql-proxy 4096 12月 24 2013 licenses drwxr-xr-x 3 mysql-proxy mysql-proxy 4096 12月 24 2013 share
3、配置环境变量
[root@proxy ~]# vim /etc/profile.d/mysql-proxy.sh export PATH=$PATH:/usr/local/mysql-proxy/bin [root@proxy ~]# source /etc/profile.d/mysql-proxy.sh
4、配置sysV脚本
[root@proxy ~]# vim /etc/init.d/mysql-proxy #!/bin/bash # # mysql-proxy This script starts and stops the mysql-proxy daemon # # chkconfig: - 78 30 # processname: mysql-proxy # description: mysql-proxy is a proxy daemon for mysql # Source function library. . /etc/rc.d/init.d/functions prog="/usr/local/mysql-proxy/bin/mysql-proxy" # Source networking configuration. if [ -f /etc/sysconfig/network ]; then . /etc/sysconfig/network fi # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 # Set default mysql-proxy configuration. ADMIN_USER="admin" ADMIN_PASSWD="admin" ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua" PROXY_OPTIONS="--daemon" PROXY_PID=/var/run/mysql-proxy.pid PROXY_USER="mysql-proxy" # Source mysql-proxy configuration. if [ -f /etc/sysconfig/mysql-proxy ]; then . /etc/sysconfig/mysql-proxy fi RETVAL=0 start() { echo -n $"Starting $prog: " daemon $prog $PROXY_OPTIONS --pid-file=$PROXY_PID --proxy-address="$PROXY_ADDRESS" --user=$PROXY_USER --admin-username="$ADMIN_USER" --admin-lua-script="$ADMIN_LUA_SCRIPT" --admin-password="$ADMIN_PASSWORD" RETVAL=$? echo if [ $RETVAL -eq 0 ]; then touch /var/lock/subsys/mysql-proxy fi } stop() { echo -n $"Stopping $prog: " killproc -p $PROXY_PID -d 3 $prog RETVAL=$? echo if [ $RETVAL -eq 0 ]; then rm -f /var/lock/subsys/mysql-proxy rm -f $PROXY_PID fi } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop start ;; condrestart|try-restart) if status -p $PROXY_PIDFILE $prog >&/dev/null; then stop start fi ;; status) status -p $PROXY_PID $prog ;; *) echo "Usage: $0 {start|stop|restart|reload|status|condrestart|try-restart}" RETVAL=1 ;; esac exit $RETVAL [root@proxy ~]# chmod +x /etc/init.d/mysql-proxy [root@proxy ~]# chkconfig --add mysql-proxy
5、为服务脚本提供配置文件
[root@proxy ~]# vim /etc/sysconfig/mysql-proxy # Options for mysql-proxy ADMIN_USER="admin" ADMIN_PASSWORD="admin" ADMIN_ADDRESS="" ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua" PROXY_ADDRESS="0.0.0.0:3306" PROXY_USER="mysql-proxy" PROXY_OPTIONS="--daemon --log-level=info --log-file="/var/log/mysql-proxy.log" --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.1.8:3306 --proxy-read-only-backend-addresses=192.168.1.10:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"
注意:其中的proxy-backend-addresses选项和proxy-read-only-backend-addresses选项均可重复使用多次,以实现指定多个读写服务器或只读服务器。
6、建立admin.lua脚本
[root@proxy mysql-proxy]# cat /usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua --[[ $%BEGINLICENSE%$ Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA $%ENDLICENSE%$ --]] function set_error(errmsg) proxy.response = { type = proxy.MYSQLD_PACKET_ERR, errmsg = errmsg or "error" } end function read_query(packet) if packet:byte() ~= proxy.COM_QUERY then set_error("[admin] we only handle text-based queries (COM_QUERY)") return proxy.PROXY_SEND_RESULT end local query = packet:sub(2) local rows = { } local fields = { } if query:lower() == "select * from backends" then fields = { { name = "backend_ndx", type = proxy.MYSQL_TYPE_LONG }, { name = "address", type = proxy.MYSQL_TYPE_STRING }, { name = "state", type = proxy.MYSQL_TYPE_STRING }, { name = "type", type = proxy.MYSQL_TYPE_STRING }, { name = "uuid", type = proxy.MYSQL_TYPE_STRING }, { name = "connected_clients", type = proxy.MYSQL_TYPE_LONG }, } for i = 1, #proxy.global.backends do local states = { "unknown", "up", "down" } local types = { "unknown", "rw", "ro" } local b = proxy.global.backends[i] rows[#rows + 1] = { i, b.dst.name, -- configured backend address states[b.state + 1], -- the C-id is pushed down starting at 0 types[b.type + 1], -- the C-id is pushed down starting at 0 b.uuid, -- the MySQL Server's UUID if it is managed b.connected_clients -- currently connected clients } end elseif query:lower() == "select * from help" then fields = { { name = "command", type = proxy.MYSQL_TYPE_STRING }, { name = "description", type = proxy.MYSQL_TYPE_STRING }, } rows[#rows + 1] = { "SELECT * FROM help", "shows this help" } rows[#rows + 1] = { "SELECT * FROM backends", "lists the backends and their state" } else set_error("use 'SELECT * FROM help' to see the supported commands") return proxy.PROXY_SEND_RESULT end proxy.response = { type = proxy.MYSQLD_PACKET_OK, resultset = { fields = fields, rows = rows } } return proxy.PROXY_SEND_RESULT [root@proxy mysql-proxy]# /etc/init.d/mysql-proxy start 正在启动 /usr/local/mysql-proxy/bin/mysql-proxy: [确定] [root@proxy mysql-proxy]# ss -tunlp | grep mysql-proxy tcp LISTEN 0 128 *:4041 *:* users:(("mysql-proxy",2692,11)) tcp LISTEN 0 128 *:3306 *:* users:(("mysql-proxy",2692,10))
7、在主服务器上创建测试的账号与密码
MariaDB [(none)]> grant all on *.* to 'admin'@'192.168.%.%' identified by 'admin'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
8、测试
注意你的proxy主机没有启动mysql,proxy的主要作用就是通过其能做到反向代理的功能实现读写分离。
[root@proxy mysql-proxy]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2 "No such file or directory") [root@proxy mysql-proxy]# mysql -uadmin -padmin -h192.168.1.9 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 10.0.21-MariaDB-log Source distribution Copyright (c) 2000, 2015, 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 | +---------------------+ | #mysql50#lost+found | | information_schema | | mysql | | performance_schema | | test | | testdb | +---------------------+ 6 rows in set (0.00 sec) MariaDB [(none)]> use testdb Database changed MariaDB [testdb]> create table tt (name char(20)); Query OK, 0 rows affected (0.10 sec) MariaDB [testdb]> insert into tt values('bols'),('longls'); Query OK, 2 rows affected (0.08 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [testdb]> select * from tt; +--------+ | name | +--------+ | bols | | longls | +--------+ 2 rows in set (0.00 sec)
在主上能查看到在proxy上写入的数据:
MariaDB [(none)]> select * from testdb.tt; +--------+ | name | +--------+ | bols | | longls | +--------+ 2 rows in set (0.00 sec)
在从上也能查看到在proxy上写入的数据:
MariaDB [(none)]> select * from testdb.tt; +--------+ | name | +--------+ | bols | | longls | +--------+ 2 rows in set (0.00 sec)
通过连接proxy的管理接口就能看到其管理的机器,由于实验中读的数据量较少,故在从上的只读功能没有体现出来。
[root@proxy mydata]# mysql -uadmin -padmin -h192.168.1.9 --port=4041 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, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> SELECT * FROM backends; +-------------+-------------------+-------+------+------+-------------------+ | backend_ndx | address | state | type | uuid | connected_clients | +-------------+-------------------+-------+------+------+-------------------+ | 1 | 192.168.1.8:3306 | up | rw | NULL | 0 | | 2 | 192.168.1.10:3306 | up | ro | NULL | 0 | +-------------+-------------------+-------+------+------+-------------------+ 2 rows in set (0.00 sec)