顾名思义, 在mysql负载均衡中有多种方式, 本人愚钝,只了解驱动中间件和mysql_proxy两种方式, 对于驱动,利用的是ReplicationDriver,具体请看远哥的这篇文章: MySQL读写分离又一好办法 使用 com.mysql.jdbc.ReplicationDriver

本次我要实现利用的方式是:mysql_proxy

下面进入主题

mysql_proxy:192.168.99.55

master:192.168.99.61

slave:192.168.99.62

1.安装mysql_proxy

  1. tar -zxvf mysql-proxy-0.8.4-linux-rhel5-x86-64bit.tar.gz -C /usr/local
  2.  
  3. cd /usr/local
  4.  
  5. mv mysql-proxy-0.8.4-linux-rhel5-x86-64bit mysql-proxy

下面是需要的依赖

  1. gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig*
  2. libevent* glib* readline-devel

2.添加代理用户

  1. useradd -r mysql-proxy

3.sysv服务脚本mysql_proxy

这个脚本放置的位置是:/etc/init.d

记得授权,chmod +x /etc/init.d/mysql_proxy

这个脚本是用来做快捷启动的。

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

4.为服务脚本提供配置文件mysql_proxy

这个脚本放在的位置是:/etc/sysconfig(我是根据上面服务脚本的位置放的, 喜欢放在别处的地方可以自行修改)。

  1. # Options for mysql-proxy
  2. ADMIN_USER="admin"
  3. ADMIN_PASSWORD="admin"
  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.99.61:3306
    --proxy-read-only-backend-addresses=192.168.99.62:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"

5.创建admin.lua

这个脚本放置的位置是:/usr/local/mysql-proxy/share/doc/mysql-proxy

  1. --[[ $%BEGINLICENSE%$
  2. Copyright (c) 2007, 2012, 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 2 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., 51 Franklin St, Fifth Floor, Boston, MA
  17. 02110-1301 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(2)
  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 = 1, #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 + 1] = {
  70. i,
  71. b.dst.name, -- configured backend address
  72. states[b.state + 1], -- the C-id is pushed down starting at 0
  73. types[b.type + 1], -- the C-id is pushed down starting at 0
  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 + 1] = { "SELECT * FROM help", "shows this help" }
  86. rows[#rows + 1] = { "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

6.修改rw-splitting.lua

修改的是min_idle_connections和max_idle_connections的值,都等于1

  1. local commands = require("proxy.commands")
  2. local tokenizer = require("proxy.tokenizer")
  3. local lb = require("proxy.balance")
  4. local auto_config = require("proxy.auto-config")
  5.  
  6. --- config
  7. --
  8. -- connection pool
  9. if not proxy.global.config.rwsplit then
  10. proxy.global.config.rwsplit = {
  11. min_idle_connections = 1,
  12. max_idle_connections = 1,
  13.  
  14. is_debug = false
  15. }
  16. end

7.测试mysql_proxy

7.1启动mysql_proxy

  1. service mysql_proxy start

7.2端口进程状态

进程

  1. ps aux | grep mysql-proxy
  2.  
  3. root 14230 0.0 0.0 103244 836 pts/0 S+ 11:25 0:00 grep mysql-proxy
  4. 496 28239 0.0 0.0 45772 2104 ? S Dec04 0:03 /usr/local/mysql-proxy/libexec/mysql-proxy --daemon --log-level=info --log-use-syslog --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.75.61:3306 --proxy-read-only-backend-addresses=192.168.75.62:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua --pid-file=/var/run/mysql-proxy.pid --proxy-address= --user=mysql-proxy --admin-username=admin --admin-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua --admin-password=admin

端口

  1. netstat -tlnp | grep mysql-proxy
  2.  
  3. tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 28239/mysql-proxy
  4. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 28239/mysql-proxy

看到上面, 端口有4041与3306, 这两个端口都是关键所在,下面将进行解题。

7.3登录mysql_proxy

随便找一台有mysql客户端的机器登录到mysql_proxy192.168.99.55(也可以在99.55上面安装mysql客户端, 我是在别的机器连得), 这个登录使用的账号密码还记得把?它就是脚本中指定的账号密码,admin:admin

  1. mysql -u admin -p -h 192.168.99.55 --port=4041

必须指定端口, 不然跑的是3306

7.4查询状态

7.4.1 select * from help

  1. +------------------------+------------------------------------+
  2. | command | description |
  3. +------------------------+------------------------------------+
  4. | SELECT * FROM help | shows this help |
  5. | SELECT * FROM backends | lists the backends and their state |
  6. +------------------------+------------------------------------+
  7. 2 rows in set (0.00 sec)

7.4.2 SELECT * FROM backends

  1. +-------------+--------------------+-------+------+------+-------------------+
  2. | backend_ndx | address | state | type | uuid | connected_clients |
  3. +-------------+--------------------+-------+------+------+-------------------+
  4. | 1 | 192.168.99.61:3306 | down | rw | NULL | 0 |
  5. | 2 | 192.168.99.62:3306 | down | ro | NULL | 0 |
  6. +-------------+--------------------+-------+------+------+-------------------+
  7. 2 rows in set (0.00 sec)

看到上面的信息, master99.61的type是rw(支持读写),slave99.62的type是ro(只读)

7.5抓包分析

在master上:

  1. tcpdump -i eth0 -nn -XX ip dst 192.168.99.61 and tcp dst port 3306

在slave上:

  1. tcpdump -i eth0 -nn -XX ip dst 192.168.99.62 and tcp dst port 3306

7.5.1主从分析

为了更明显操作结果, 停止主从

登录mysql_proxy

  1. mysql -u admin -p -h 192.168.75.55 --port=3306

这次登录的是3306, 上面登录的是4041,请大家不要混淆。

至于3306与4041有什么不一样, 下面我解析一下, 4041就是查询读写分离的状态的, 3306无疑就是提供给外部的接口。

现在往test表插入一条数据

  1. insert into test.testtest values(80,'80');

下面查询, 是查询不到的, 因为主从已经断开, 查的是往62跑, 写是写进61了, 只要把主从配置回来一切正常。

上面的抓包就是没操作一下都会有记录, 这次很明显看出来读写分析所跑的机器。

进行写时, master抓到的包是:

  1. 14:25:28.835455 IP 192.168.99.55.45648 > 192.168.99.61.3306: Flags [P.], seq 2082548409:2082548453, ack 3795613073, win 173, options [nop,nop,TS val 2915927263 ecr 2948974813], length 44
  2. 0x0000: 0050 5689 cba6 0050 5689 9616 0800 4508 .PV....PV.....E.
  3. 0x0010: 0060 994f 4000 4006 897b c0a8 4b37 c0a8 .`.O@.@..{..K7..
  4. 0x0020: 4b3d b250 0cea 7c21 2ab9 e23c 7591 8018 K=.P..|!*..<u...
  5. 0x0030: 00ad eed3 0000 0101 080a adcd 84df afc5 ................
  6. 0x0040: c8dd 2800 0000 0369 6e73 6572 7420 696e ..(....insert.in
  7. 0x0050: 746f 2074 6573 742e 7465 7374 7465 7374 to.test.testtest
  8. 0x0060: 2076 616c 7565 7328 322c 2732 2729 .values(2,'2')
  9. 14:25:28.836543 IP 192.168.99.55.45648 > 192.168.99.61.3306: Flags [.], ack 12, win 173, options [nop,nop,TS val 2915927264 ecr 2949040785], length 0
  10. 0x0000: 0050 5689 cba6 0050 5689 9616 0800 4508 .PV....PV.....E.
  11. 0x0010: 0034 9950 4000 4006 89a6 c0a8 4b37 c0a8 .4.P@.@.....K7..
  12. 0x0020: 4b3d b250 0cea 7c21 2ae5 e23c 759c 8010 K=.P..|!*..<u...
  13. 0x0030: 00ad f329 0000 0101 080a adcd 84e0 afc6 ...)............

slave此时的状态是:

  1. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  2. listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

进行读时, slave的状态是:

  1. 15:00:36.252068 IP 192.168.99.55.40191 > 192.168.99.62.3306: Flags [P.], seq 853416170:853416202, ack 2118301929, win 184, options [nop,nop,TS val 2915944710 ecr 2911691799], length 32
  2. 0x0000: 0050 5689 a328 0050 5689 9616 0800 4508 .PV..(.PV.....E.
  3. 0x0010: 0054 7caa 4000 4006 a62b c0a8 4b37 c0a8 .T|.@.@..+..K7..
  4. 0x0020: 4b3e 9cff 0cea 32de 18ea 7e42 b8e9 8018 K>....2...~B....
  5. 0x0030: 00b8 0093 0000 0101 080a adcd c906 ad8c ................
  6. 0x0040: e417 1c00 0000 0373 656c 6563 7420 2a20 .......select.*.
  7. 0x0050: 6672 6f6d 2074 6573 742e 7465 7374 7465 from.test.testte
  8. 0x0060: 7374 st
  9. 15:00:36.253339 IP 192.168.99.55.40191 > 192.168.99.62.3306: Flags [.], ack 144, win 223, options [nop,nop,TS val 2915944712 ecr 2912255989], length 0
  10. 0x0000: 0050 5689 a328 0050 5689 9616 0800 4508 .PV..(.PV.....E.
  11. 0x0010: 0034 7cab 4000 4006 a64a c0a8 4b37 c0a8 .4|.@.@..J..K7..
  12. 0x0020: 4b3e 9cff 0cea 32de 190a 7e42 b978 8010 K>....2...~B.x..
  13. 0x0030: 00df 8c29 0000 0101 080a adcd c908 ad95 ...)............
  14. 0x0040: 7ff5 ..

最终拓扑

mysql读写分离[高可用]的更多相关文章

  1. MySQL读写分离高可用集群及读操作负载均衡(Centos7)

    目录 概述 keepalived和heartbeat对比 一.环境 二.部署 部署lvs代理和keepalived MySQL+heartbeat+drbd的部署 MySQL主从复制 web服务器及a ...

  2. MHA+ProxySQL实现读写分离高可用

    最近在研究ProxySQL,觉得还挺不错的,所以就简单的折腾了一下,ProxySQL目前也是Percona在推荐的一个读写分离的中间件.关于详细的介绍可以参考官方文档.https://github.c ...

  3. MySQL for OPS 09:MHA + Atlas 实现读写分离高可用

    写在前面的话 前面做了 MHA 高可用,但是存在这样一个问题,我们花了 4 台机器,但是最终被利用起来的也就一台,主库.这样硬件利用率才 25%,这意味着除非发生故障,不然其他几台机器都是摆设.明显的 ...

  4. Atlas读写分离[高可用]

    Atlas下载地址: https://github.com/Qihoo360/Atlas/releases Atlas是出于360的, 比mysql-proxy更稳定, 部署起来更方便. 环境: pr ...

  5. MHA+ProxySQL 读写分离高可用

    文档结构如下: 1.ProxySQL说明 ProxySQL是mysql的一款中间件的产品,是灵活的mysql代理层,可以实现读写分离,支持query路由器的功能,支持动态指定sql进行缓存,支持动态加 ...

  6. 高可用Mysql架构_Mysql主从复制、Mysql双主热备、Mysql双主双从、Mysql读写分离(Mycat中间件)、Mysql分库分表架构(Mycat中间件)的演变

    [Mysql主从复制]解决的问题数据分布:比如一共150台机器,分别往电信.网通.移动各放50台,这样无论在哪个网络访问都很快.其次按照地域,比如国内国外,北方南方,这样地域性访问解决了.负载均衡:M ...

  7. MySQL数据库的优化(下)MySQL数据库的高可用架构方案

    MySQL数据库的优化(下)MySQL数据库的高可用架构方案 2011-03-09 08:53 抚琴煮酒 51CTO 字号:T | T 在上一篇MySQL数据库的优化中,我们跟随笔者学习了单机MySQ ...

  8. mysql读写分离总结

    随着一个网站的业务不断扩展,数据不断增加,数据库的压力也会越来越大,对数据库或者SQL的基本优化可能达不到最终的效果,我们可以采用读写分离的策略来改变现状.读写分离现在被大量应用于很多大型网站,这个技 ...

  9. mysql读写分离实战

    一个完整的MySQL读写分离环境包括以下几个部分: 应用程序client database proxy database集群 在本次实战中,应用程序client基于c3p0连接后端的database ...

随机推荐

  1. 使用 nghttpx 搭建 HTTP/2 代理 (转)

    来自http://www.fanyue.info/2015/08/nghttpx-http2.html 使用 nghttpx 搭建 HTTP/2 代理 [转] HTTP/1.1,定义于 1999 年, ...

  2. linux安装svn客户端rabbitvcs

    我们都知道,自从svn出道以来,很多人都预言,cvs将会被其取代.就如同他们预言maven要取代ant一样.可见,svn的流行.在Windows中,最常用到的开源免费的svn客户端就是Tortoise ...

  3. 01JAVA语言基础课后作业

    1.问题 一个Java类文件中真的只能有一个公有类吗? 请使用Eclipse或javac检测一下以下代码,有错吗? 回答  真的只能有一个公有类 一个Java源文件中最多只能有一个public类,当有 ...

  4. supervison

    http://blog.csdn.net/kongxx/article/details/50452357

  5. SQL Server如何将Id相同的字段合并,并且以逗号隔开

    需要用到stuff函数: 例: id             name           1               张三           1               李四       ...

  6. Oracle查询表占用空间的大小

    select * from (select OWNER, segment_name, segment_type, sum(bytes) mmm from dba_segments where /*ta ...

  7. Python记录5:函数1

    函数 ''' 1. 什么是函数     在程序中具备某一功能的工具->函数     函数的使用必须遵循原则:         先定义         后调用 函数分为两大类:         1 ...

  8. 初识gispro

    因为之前一直用的arcmap,由于项目中用到三维数据的服务发布,需要用到gispro.Gispro与arcmap用法还是有些不同.仅用此文来记录一些简易操作. Gispro简介 ArcGIS Pro是 ...

  9. binTreePosterorderTraversal二叉树的后序遍历

    描述: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

  10. python socket编程笔记

    用python实现一个简单的socket网络聊天通讯 (Linux --py2.7平台与windows--py3.6平台) 人生苦短之我用Python篇(socket编程) python之路 sock ...