Mysql读写分离(mysql-proxy)
MySQL-Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测、分析或改变它们的通信。它使用灵活,没有限制,常见的用途包括:负载平衡,故障、查询分析,查询过滤和修改等等。
MySQL-Proxy就是这么一个中间层代理,简单的说,MySQL-Proxy就是一个连接池,负责将前台应用的连接请求转发给后台的数据库,并且通过使用lua脚本,可以实现复杂的连接控制和过滤,从而实现读写分离和负载平衡。对于应用来说,MySQL-Proxy是完全透明的,应用则只需要连接到MySQL-Proxy的监听端口即可。当然,这样proxy机器可能成为单点失效,但完全可以使用多个proxy机器做为冗余,在应用服务器的连接池配置中配置到多个proxy的连接参数即可。
MySQL-Proxy更强大的一项功能是实现“读写分离”,基本原理是让主数据库处理事务性查询,让从库处理SELECT查询。数据库复制被用来把事务性查询导致的变更同步到集群中的从库。
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主从复制)基础上完成。
http://www.cnblogs.com/Eivll0m/p/3776496.html
1.环境及系统软件版本介绍:
系统版本:CentOS 6.3_x86_64
Mysql版本:mysql-5.6.17
mysql-proxy版本:mysql-proxy-0.8.4(下载地址:http:
//cdn
.mysql.com
/Downloads/MySQL-Proxy/mysql-proxy-0
.8.4-linux-glibc2.3-x86-64bit.
tar
.gz
)
2.部署mysql-proxy,以下操作未经特殊说明,均表示在mysql-prxoy(172.16.10.60)服务器上进行。
2.1检查是否安装了lua包,mysql-proxy需要用到lua脚本,以下表示已经安装过。
# rpm -q lua
lua-5.1.4-4.1.el6.x86_64
2.2创建代理用户
# useradd -r mysql-proxy
2.3下载通用的压缩包mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz(直接解压使用)
# wget http://cdn.mysql.com/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz
# tar xf mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit mysql-proxy
2.4设置mysql-proxy相关文件权限
chown -R root:mysql-proxy /usr/local/mysql-proxy/*
2.5更新环境变量
# vi /etc/profile.d/mysql-proxy.sh
export PATH=$PATH:/usr/local/mysql-proxy/bin
# . /etc/profile
2.6启动mysql-proxy
# mysql-proxy --daemon --log-level=debug --log-file=/var/log/mysql-proxy.log --plugins="proxy" --proxy-backend-addresses="172.16.10.72:3306" --proxy-read-only-backend-addresses="172.16.10.61:3306"
2.7检查mysql-proxy是否正常启动
通过命令netstat -antpl|grep mysql-prxoy或tail /var/log/mysql-proxy.log进行查看
2.8在master服务器(172.16.10.72)上创建测试账号
mysql> GRANT ALL ON *.* TO proxy@'172.16.10.%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.22 sec) mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.03 sec)'
2.9在slave服务器(172.16.10.61)上测试是否能通过代理端口4040 端口连接到 mysql-proxy(以下表示测试成功)
root@mysql_slave ~]# mysql -uproxy -p -h172.16.10.60 -P 4040
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 456
Server version: 5.6.17-log Source distribution Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
2.10配置读写分离,mysql-proxy 的安装目录中有一个名为rw-splitting.lua的 脚本,通过它可以实现读写分离,路径是/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua。
# ls /usr/local/mysql-proxy/share/doc/mysql-proxy/|grep rw-splitting.lua
rw-splitting.lua
杀掉当前mysql-proxy进程,然后把lua脚本加入到启动选项里面重新启动mysql-proxy,实现读写分离
# mysql-proxy --daemon --log-level=debug --log-file=/var/log/mysql-proxy.log --plugins="proxy" --proxy-backend-addresses="172.16.10.72:3306" --proxy-read-only-backend-addresses="172.16.10.61:3306" --proxy-lua-script=" /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"
查看日志
# tail /var/log/mysql-proxy.log
2014-06-13 16:23:04: (message) added read/write backend: 172.16.10.72:3306
2014-06-13 16:23:04: (message) added read-only backend: 172.16.10.61:3306
2014-06-13 16:40:11: (message) Initiating shutdown, requested from signal handler
2014-06-13 16:40:11: (message) shutting down normally, exit code is: 0
2014-06-13 16:40:17: (debug) chassis-path.c.122: adjusting relative path ( /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua) to base_dir (/usr/local/mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit). New path: /usr/local/mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit/ /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
2014-06-13 16:40:17: (critical) plugin proxy 0.8.4 started
2014-06-13 16:40:17: (debug) max open file-descriptors = 1024
2014-06-13 16:40:17: (message) proxy listening on port :4040
2014-06-13 16:40:17: (message) added read/write backend: 172.16.10.72:3306
2014-06-13 16:40:17: (message) added read-only backend: 172.16.10.61:3306
2.11为mysql-proxy 提供一个管理接口,方便以后随时查看后端mysql 服务器的状态和访问类型,实现管理功能
# vi /usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua
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
end
2.12重新启动mysql-proxy
注,这次启动要添加以下启动选项 ,因为我们添加了额外的插件,把新加功能添加进来,选项如下。
--plugins=admin 在mysql-proxy启动时加载的插件;
--admin-username="admin" 运行mysql-proxy进程管理的用户;
--admin-password="admin" 密码
--admin-lua-script="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua" 插件使用的配置文件路径;
# killall mysql-proxy
# mysql-proxy --daemon --log-level=debug --log-file=/var/log/mysql-proxy.log --plugins="proxy" --proxy-backend-addresses="172.16.10.72:3306" --proxy-read-only-backend-addresses="172.16.10.61:3306" --proxy-lua-script=" /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua" --plugins=admin --admin-username="admin" --admin-password="admin" --admin-lua-script="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"
查看mysql-proxy是否启
# netstat -antpl|grep mysql
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 9511/mysql-proxy
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 9511/mysql-proxy #4041为新生成的管理端口
2.13在slave服务器(172.16.10.61)上进行测试,使用管理端口4041 登陆,密码是admin。(测试成功!)
Bye
[root@mysql_slave ~]# mysql -uadmin -p -h172.16.10.60 -P 4041
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
2.14测试是否可以连接
root@mysql_slave ~]# mysql -uadmin -p -h172.16.10.60 -P 4041
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select * from backends;
+-------------+-------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+-------------------+---------+------+------+-------------------+
| 1 | 172.16.10.72:3306 | unknown | rw | NULL | 0 |
| 2 | 172.16.10.61:3306 | unknown | ro | NULL | 0 |
+-------------+-------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec) mysql>
由于此时没有进行任何操作,所以主从状态都是unknown。
2.15配置启动脚本,并修连接端口(连接mysql-proxy时,原来用的端口是4040,下面修改为3306)
# vi /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=172.16.10.72:3306 --proxy-read-only-backend-addresses=172.16.10.61:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"
添加sysV脚本
# vi /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
# chmod +x /etc/rc.d/init.d/mysql-proxy
# chkconfig --add mysql-proxy
# chkconfig mysql-proxy on
2.16重启mysql-proxy并查看端口(4040端口已消失)
# service mysql-proxy restart
# netstat -antpl|grep mysql
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 12427/mysql-proxy
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 12427/mysql-proxy
2.17测试读写分离
执行读写操作
[root@mysql_slave ~]# mysql -uproxy -p -h172.16.10.60 -e "show databases;"
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| eivll0m |
| mysql |
| performance_schema |
| test |
+--------------------+
[root@mysql_slave ~]# mysql -uproxy -p -h172.16.10.60 -e "create database mydb1;"
Enter password:
mysql> select * from backends;
+-------------+-------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+-------------------+---------+------+------+-------------------+
| 1 | 172.16.10.72:3306 | up | rw | NULL | 0 |
| 2 | 172.16.10.61:3306 | unknown | ro | NULL | 0 |
+-------------+-------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)
由于master是支持读写的,所以只发现master状态变为了up是正常现象,要想观看到从的状态,可以执行多次查询。
# mysql -uproxy -p -h172.16.10.60 -e "select user,host from mysql.user"
Enter password:
+-------+--------------+
| user | host |
+-------+--------------+
| root | 127.0.0.1 |
| proxy | 172.16.10.% |
| root | ::1 |
| | localhost |
| root | localhost |
| | mysql\_slave |
| root | mysql\_slave |
+-------+--------------+ mysql> select * from backends;
+-------------+-------------------+-------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+-------------------+-------+------+------+-------------------+
| 1 | 172.16.10.72:3306 | up | rw | NULL | 0 |
| 2 | 172.16.10.61:3306 | up | ro | NULL | 0 |
+-------------+-------------------+-------+------+------+-------------------+
2 rows in set (0.00 sec)
现在,主从状态全部变成了up状态。
Mysql读写分离(mysql-proxy)的更多相关文章
- 三十五.MySQL读写分离 MySQL多实例 、MySQL性能调优
1.实现MySQL读写分离 搭建一主一从结构 配置maxscale代理服务器 测试分离配置 1.1 搭建一主一从结构 192.168.4.51 主 192.168.4.52 从 测试OK 1. ...
- MySQL读写分离之Proxy
MySQL Proxy: ======================================================== MySQL_Proxy Master Slave1 Slav ...
- Mysql读写分离-Amoeba Proxy
参考:http://www.linuxidc.com/Linux/2015-10/124115.htm 一个完整的MySQL读写分离环境包括以下几个部分: 应用程序client database pr ...
- amoeba实现MySQL读写分离
amoeba实现MySQL读写分离 准备环境:主机A和主机B作主从配置,IP地址为192.168.131.129和192.168.131.130,主机C作为中间件,也就是作为代理服务器,IP地址为19 ...
- MySQL读写分离技术
1.简介 当今MySQL使用相当广泛,随着用户的增多以及数据量的增大,高并发随之而来.然而我们有很多办法可以缓解数据库的压力.分布式数据库.负载均衡.读写分离.增加缓存服务器等等.这里我们将采用读写分 ...
- mysql读写分离[高可用]
顾名思义, 在mysql负载均衡中有多种方式, 本人愚钝,只了解驱动中间件和mysql_proxy两种方式, 对于驱动,利用的是ReplicationDriver,具体请看远哥的这篇文章: MySQL ...
- mysql读写分离 主从同步
MySQL主从复制与读写分离的实现 转载 2013年01月17日 18:20:12 MySQL主从复制与读写分离 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy) ...
- 构建高性能web之路------mysql读写分离实战(转)
一个完整的mysql读写分离环境包括以下几个部分: 应用程序client database proxy database集群 在本次实战中,应用程序client基于c3p0连接后端的database ...
- mysql读写分离配置(整理)
mysql读写分离配置 环境:centos7.2 mysql5.7 场景描述: 数据库Master主服务器:192.168.206.100 数据库Slave从服务器:192.168.206.200 M ...
- mysql读写分离实战
一个完整的MySQL读写分离环境包括以下几个部分: 应用程序client database proxy database集群 在本次实战中,应用程序client基于c3p0连接后端的database ...
随机推荐
- Apache Options Indexes FollowSymLinks之讲解
禁止显示Apache目录列表-Indexes FollowSymLinks 如何修改目录的配置以禁止显示 Apache 目录列表. 缺省情况下如果你在浏览器输入地址: http://localhost ...
- winform 开发之Control.InvokeRequired
Control.InvokeRequired 获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中. InvokeRequir ...
- C#实现身份证号码验证的方法
本文实例讲述了C#实现身份证号码验证的方法.分享给大家供大家参考.具体实现方法如下: 随着现在互联网的发展,越来越多的注册用户的地方都用到了身份证,那么对于输入的身份证如何验证呢?看下面的代码,其实很 ...
- Java 编译错误:缺少返回语句
示例: import java.util.*; import java.io.*; public class tt { public static void main(String[] args) { ...
- Python str字符串常用到的函数
# -*- coding: utf-8 -*- x='pythonnnnnnoooo' print type(x) # <type 'str'> 输出类型 print x.capitali ...
- 国内为什么没有好的 Stack Overflow 的模仿者?
国内为什么没有好的 Stack Overflow 的模仿者? 个人觉得, 高端的程序员会直接上stackoverflow提问, 所以国内中文的stackoverflow必然面对低端程序员. 鉴于中国程 ...
- Sina App Engine(SAE)入门教程(3)-KVDB使用
简介 因为传统关系型数据库在分布式环境下表现的扩展性不足等缺点,近年来NoSQL的概念渐渐成为业界关注的焦点,越来越多的技术人员也习惯于使用NoSQL数据库进行日常开发,SAE为了应对这种新需求,也进 ...
- JTable指定单元格加控件
原文链接:http://blog.csdn.net/transit136/article/details/2133638 JTable可以给表格的某一列加入控件,下面方法可以实现 try{ T ...
- App应用与思考
我为什么没有加入苹果的iOS APP移动大军?http://blog.csdn.net/Code_GodFather/article/details/7956858 ----------------- ...
- 使用CXF暴露您的REST服务
使用CXF暴露您的REST服务 REST应用服务器SpringBeanServlet 1. 前言 现在互联网Open API流行,将您的Web应用也可以开放Open API给其他第三方使用.达到一 ...