一、简述

MySQL Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测、分析或改变它们的通信。它使用灵活,没有限制,常见的用途包括:负载平衡,故障、查询分析,查询过滤和修改等等。
MySQL Proxy就是这么一个中间层代理,简单的说,MySQL Proxy就是一个连接池,负责将前台应用的连接请求转发给后台的数据库,并且通过使用lua脚本,可以实现复杂的连接控制和过滤,从而实现读写分离和负 载平衡。对于应用来说,MySQL Proxy是完全透明的,应用则只需要连接到MySQL Proxy的监听端口即可。当然,这样proxy机器可能成为单点失效,但完全可以使用多个proxy机器做为冗余,在应用服务器的连接池配置中配置到多 个proxy的连接参数即可。
MySQL Proxy更强大的一项功能是实现“读写分离”,基本原理是让主数据库处理事务性查询,让从库处理SELECT查询。数据库复制被用来把事务性查询导致的变更同步到集群中的从库。  

二、对MariaDB做主从复制

关于如何对MariaDB做主从复制,请移步本人博客MariaDB 主从复制

三、安装

1.可以通过rpm安装,其会提供配置文件及服务脚本,但是没有读写分享脚本

2.通过编译安装

①、源码安装时,MySQL proxy的依赖关系:

libevent 1.x or higher (1.3b or later is preferred).

lua 5.1.x or higher.

glib2 2.6.0 or higher.

pkg-config.

libtool 1.5 or higher.

MySQL 5.0.x or higher developer files.

②、下载源码包,编译安装

  1. # tar zxf mysql-proxy-0.8.2.tar.gz
  2. # cd mysql-proxy-0.8.2
  3. # ./configure
  4. # make
  5. # make check
  6. 如果管理员有密码,上面的步骤则需要使用如下格式进行:
  7. # MYSQL_PASSWORD=root_pwd make check
  8. # make install
  9. 默认情况下, mysql-proxy安装在/usr/local/sbin/mysql-proxy,而Lua示例脚本安装在/usr/local/share目录中。

3.通过通用二进制格式安装

①、下载解压。这里的系统平台为rhel6.5 64位系统

  1. [root@httpweb ~]# wget http://mirror.sohu.com/mysql/MySQL-Proxy/mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz
  2. [root@httpweb ~]# tar xf mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz
  3. [root@httpweb ~]# mv mysql-proxy-0.8.4-linux-el6-x86-64bit /usr/local/mysql-proxy

②、添加代理用户

  1. [root@httpweb mysql-proxy]# useradd mysql-proxy

③、为mysql-proxy提供SysV服务脚本

  1. #!/bin/bash
  2. #
  3. # mysql-proxy This script starts and stops the mysql-proxy daemon
  4. #
  5. # chkconfig: -
  6. # processname: mysql-proxy
  7. # description: mysql-proxy is a proxy daemon for mysql
  8. # Source function library.
  9. . /etc/rc.d/init.d/functions
  10. prog="/usr/local/mysql-proxy/bin/mysql-proxy"
  11. # Source networking configuration.
  12. if [ -f /etc/sysconfig/network ]; then
  13. . /etc/sysconfig/network
  14. fi
  15. # Check that networking is up.
  16. [ ${NETWORKING} = "no" ] && exit
  17. # Set default mysql-proxy configuration.
  18. ADMIN_USER="admin"
  19. ADMIN_PASSWD="admin"
  20. ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"
  21. PROXY_OPTIONS="--daemon"
  22. PROXY_PID=/var/run/mysql-proxy.pid
  23. PROXY_USER="mysql-proxy"
  24. # Source mysql-proxy configuration.
  25. if [ -f /etc/sysconfig/mysql-proxy ]; then
  26. . /etc/sysconfig/mysql-proxy
  27. fi
  28. RETVAL=
  29. start() {
  30. echo -n $"Starting $prog: "
  31. 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"
  32. RETVAL=$?
  33. echo
  34. if [ $RETVAL -eq ]; then
  35. touch /var/lock/subsys/mysql-proxy
  36. fi
  37. }
  38. stop() {
  39. echo -n $"Stopping $prog: "
  40. killproc -p $PROXY_PID -d $prog
  41. RETVAL=$?
  42. echo
  43. if [ $RETVAL -eq ]; then
  44. rm -f /var/lock/subsys/mysql-proxy
  45. rm -f $PROXY_PID
  46. fi
  47. }
  48. # See how we were called.
  49. case "$1" in
  50. start)
  51. start
  52. ;;
  53. stop)
  54. stop
  55. ;;
  56. restart)
  57. stop
  58. start
  59. ;;
  60. condrestart|try-restart)
  61. if status -p $PROXY_PIDFILE $prog >&/dev/null; then
  62. stop
  63. start
  64. fi
  65. ;;
  66. status)
  67. status -p $PROXY_PID $prog
  68. ;;
  69. *)
  70. echo "Usage: $0 {start|stop|restart|reload|status|condrestart|try-restart}"
  71. RETVAL=
  72. ;;
  73. esac
  74. exit $RETVAL

将上述内容保存为/etc/init.d/mysql-proxy,给予执行权限,而后添加至服务列表

  1. [root@httpweb mysql-proxy]# vi /etc/init.d/mysql-proxy
  2. [root@httpweb mysql-proxy]# chmod +x /etc/init.d/mysql-proxy
  3. [root@httpweb mysql-proxy]# chkconfig --add mysql-proxy

④、为服务脚本提供配置文件/etc/sysconfig/mysql-proxy

  1. #Options for mysql-proxy
  2. ADMIN_USER="firefox"
  3. ADMIN_PASSWORD="firefox"
  4. ADMIN_ADDRESS=""
  5. ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"
  6. PROXY_ADDRESS=""
  7. PROXY_USER="mysql-proxy"
  8. PROXY_OPTIONS="--daemon --log-level=info --log-use-syslog --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.1.200:3306 --proxy-read-only-backend-addresses=192.168.1.202:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"
  9. 其中的proxy-backend-addresses选项和proxy-read-only-backend-addresses选项均可重复使用多次,以实现指定多个读写服务器或只读服务器。

⑤、mysql-proxy的配置选项

  1. mysql-proxy的配置选项大致可分为帮助选项、管理选项、代理选项及应用程序选项几类,下面一起去介绍它们。
  2.  
  3. --help
  4.  
  5. --help-admin
  6.  
  7. --help-proxy
  8.  
  9. --help-all ———— 以上四个选项均用于获取帮助信息;
  10.  
  11. --proxy-address=host:port ———— 代理服务监听的地址和端口;
  12.  
  13. --admin-address=host:port ———— 管理模块监听的地址和端口;
  14.  
  15. --proxy-backend-addresses=host:port ———— 后端mysql服务器的地址和端口;
  16.  
  17. --proxy-read-only-backend-addresses=host:port ———— 后端只读mysql服务器的地址和端口;
  18.  
  19. --proxy-lua-script=file_name ———— 完成mysql代理功能的Lua脚本;
  20.  
  21. --daemon ———— 以守护进程模式启动mysql-proxy
  22.  
  23. --keepalive ———— mysql-proxy崩溃时尝试重启之;
  24.  
  25. --log-file=/path/to/log_file_name ———— 日志文件名称;
  26.  
  27. --log-level=level ———— 日志级别;
  28.  
  29. --log-use-syslog ———— 基于syslog记录日志;
  30.  
  31. --plugins=plugin,.. ———— mysql-proxy启动时加载的插件;
  32.  
  33. --user=user_name ———— 运行mysql-proxy进程的用户;
  34.  
  35. --defaults-file=/path/to/conf_file_name ———— 默认使用的配置文件路径;其配置段使用[mysql-proxy]标识;
  36.  
  37. --proxy-skip-profiling ———— 禁用profile
  38.  
  39. --pid-file=/path/to/pid_file_name ———— 进程文件名; 

⑥、提供admin.lua文件,将其保存至/usr/local/mysql-proxy/share/mysql-proxy/中

  1. --[[ $%BEGINLICENSE%$
  2. Copyright (c) , , Oracle and/or its affiliates. All rights reserved.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; version of the
  7. License.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., Franklin St, Fifth Floor, Boston, MA
  17. - USA
  18.  
  19. $%ENDLICENSE%$ --]]
  20.  
  21. function set_error(errmsg)
  22. proxy.response = {
  23. type = proxy.MYSQLD_PACKET_ERR,
  24. errmsg = errmsg or "error"
  25. }
  26. end
  27.  
  28. function read_query(packet)
  29. if packet:byte() ~= proxy.COM_QUERY then
  30. set_error("[admin] we only handle text-based queries (COM_QUERY)")
  31. return proxy.PROXY_SEND_RESULT
  32. end
  33.  
  34. local query = packet:sub()
  35.  
  36. local rows = { }
  37. local fields = { }
  38.  
  39. if query:lower() == "select * from backends" then
  40. fields = {
  41. { name = "backend_ndx",
  42. type = proxy.MYSQL_TYPE_LONG },
  43.  
  44. { name = "address",
  45. type = proxy.MYSQL_TYPE_STRING },
  46. { name = "state",
  47. type = proxy.MYSQL_TYPE_STRING },
  48. { name = "type",
  49. type = proxy.MYSQL_TYPE_STRING },
  50. { name = "uuid",
  51. type = proxy.MYSQL_TYPE_STRING },
  52. { name = "connected_clients",
  53. type = proxy.MYSQL_TYPE_LONG },
  54. }
  55.  
  56. for i = , #proxy.global.backends do
  57. local states = {
  58. "unknown",
  59. "up",
  60. "down"
  61. }
  62. local types = {
  63. "unknown",
  64. "rw",
  65. "ro"
  66. }
  67. local b = proxy.global.backends[i]
  68.  
  69. rows[#rows + ] = {
  70. i,
  71. b.dst.name, -- configured backend address
  72. states[b.state + ], -- the C-id is pushed down starting at
  73. types[b.type + ], -- the C-id is pushed down starting at
  74. b.uuid, -- the MySQL Server's UUID if it is managed
  75. b.connected_clients -- currently connected clients
  76. }
  77. end
  78. elseif query:lower() == "select * from help" then
  79. fields = {
  80. { name = "command",
  81. type = proxy.MYSQL_TYPE_STRING },
  82. { name = "description",
  83. type = proxy.MYSQL_TYPE_STRING },
  84. }
  85. rows[#rows + ] = { "SELECT * FROM help", "shows this help" }
  86. rows[#rows + ] = { "SELECT * FROM backends", "lists the backends and their state" }
  87. else
  88. set_error("use 'SELECT * FROM help' to see the supported commands")
  89. return proxy.PROXY_SEND_RESULT
  90. end
  91.  
  92. proxy.response = {
  93. type = proxy.MYSQLD_PACKET_OK,
  94. resultset = {
  95. fields = fields,
  96. rows = rows
  97. }
  98. }
  99. return proxy.PROXY_SEND_RESULT
  100. end

⑦、测试

启动服务;

  1. service mysql-proxy start

管理功能测试

可以看到4041端口和3306端口以及处于监听状态

我们在主服务器上授予201数据库写的权限

  1. MariaDB [(none)]> grant all on *.* to 'firefox'@'192.168.1.201' identified by 'firefox';
  2. MariaDB [(none)]> flush privileges;
  3. [root@httpweb mysql-proxy]# mysql -ufirefox -pfirefox -h192.168.1. --port=
  4. 我们可以看到我们在mysql-proxy的可以登录数据库了

查看是否配置成功

  1. [root@httpweb ~]# mysql -ufirefox -pfirefox -h192.168.1. --port=
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is
  4. Server version: 5.0.-agent-admin
  5. Copyright (c) , , Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. mysql> select * from backends;
  10. +-------------+--------------------+---------+------+------+-------------------+
  11. | backend_ndx | address | state | type | uuid | connected_clients |
  12. +-------------+--------------------+---------+------+------+-------------------+
  13. | | 192.168.1.200: | up | rw | NULL | |
  14. | | 192.168.1.202: | up | ro | NULL | |
  15. +-------------+--------------------+---------+------+------+-------------------+

我们的程序就可以实现真正意义上的读写分离了,大功告成,由于本人水平有限,请各位大神多多批评指正

MySQL Proxy 实现MySQLDB 读写分离的更多相关文章

  1. MySql的主从复制以及读写分离详解

    MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中, ...

  2. 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下)

    基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下) 昨天谈到了Mysql实现主从复制,但由于时间原因并未讲有关读写分离的实现,之所以有读写分离,是为了使数据库拥有双机热备功能,至于双 ...

  3. 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上)

    基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上) 上周BOSS给分配任务让实现一下Mysql数据库的主从复制以及读写分离,然后花了一盏茶的功夫进行了调研,发现主从复制数据库进行一番 ...

  4. MySQL主从复制技术与读写分离技术amoeba应用

    MySQL主从复制技术与读写分离技术amoeba应用 前言:眼下在搭建一个人才站点,估计流量会非常大,须要用到分布式数据库技术,MySQL的主从复制+读写分离技术.读写分离技术有官方的MySQL-pr ...

  5. MySQL中间件之ProxySQL_读写分离/查询重写配置

    MySQL中间件之ProxySQL_读写分离/查询重写配置 Posted on 2016-12-25 by mark blue, mark Leave a comment MySQL 1.闲扯几句 读 ...

  6. Mysql 高可用(MHA)-读写分离(Atlas)-分布式架构(Mycat)

    Mysql 高可用(MHA)-读写分离(Atlas) 1. 搭建主从复制(一主两从) 1.1 准备环境 1 主库:10.0.0.51/db01 2 从库:10.0.0.52/db02,10.0.0.5 ...

  7. mysql数据库主从同步读写分离(一)主从同步

    1.mysql数据库主从同步读写分离 1.1.主要解决的生产问题 1.2.原理 a.为什么需要读写分离? 一台服务器满足不了访问需要.数据的访问基本都是2-8原则. b.怎么做?  不往从服务器去写了 ...

  8. MySQL主从同步、读写分离配置步骤、问题解决笔记

    MySQL主从同步.读写分离配置步骤.问题解决笔记 根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记:       现在使用的两台服务器已经 ...

  9. redis 作为 mysql的缓存服务器(读写分离)

    redis 作为 mysql的缓存服务器(读写分离) 一.redis简介 Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会 ...

随机推荐

  1. Android图片异步加载

    原:http://www.cnblogs.com/angeldevil/archive/2012/09/16/2687174.html 相关:https://github.com/nostra13/A ...

  2. python网络编程-Select\Poll\Epoll异步IO

    首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select ...

  3. 简单ORACLE分区表、分区索引

    前一段听说CSDN.COM里面很多好东西,同事建议看看合适自己也可以写一写,呵呵,今天第一次开通博客,随便写点东西,就以第一印象分区表简单写第一个吧. ORACLE对于分区表方式其实就是将表分段存储, ...

  4. thinkphp框架if标签条件表达式

    eq 等于neq 不等于gt 大于egt 大于等于lt 小于elt 小于等于

  5. 防止一个exe被打开多次

    mutex有一个名字,如果这个exe已经打开了,createNew返回的就是false,程序就退出了. 这是个wpf application的例子 protected override void On ...

  6. GUC-6 Callable 接口

    import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.ut ...

  7. python处理汉字转拼音pypinyin

    主要是pypinyin 包,官网: http://pypinyin.readthedocs.io/zh_CN/master/index.html jieba包,主要是用来分词的,我之前的博文有介绍:h ...

  8. java.lang.NoClassDefFoundError: javax/persistence/EntityListeners

    在使用 Hibernate 进行数据库操作的时候,在启动 Tomcat 服务器后,Console 控制台可能会打印出这样的异常:java.lang.NoClassDefFoundError: java ...

  9. oracle中vsize和length

    其实LENGTH与VSIZE这两个函数联系不大,区别很大.虽然都是“取长度”,但是LENGTH函数结果是“有多少个字符”,VSIZE结果是“需要多少bytes”.简单看一下这两个函数. 1.创建表T并 ...

  10. JDK源码分析(四)——LinkedHashMap

    目录 LinkedHashMap概述 内部字段及构造方法 存储元素 取出元素 删除元素 迭代器 利用LinkedHashMap简单实现LRU算法 总结 LinkedHashMap概述   JDK对Li ...