前言:

  MySQL读写分离是指让master处理写操作,让slave处理读操作,非常适用于读操作量比较大的场景,可减轻master的压力。使用mysql-proxy实现mysql的读写分离,mysql-proxy实际上是作为后端mysql主从服务器的代理,它直接接受客户端的请求,对SQL语句进行分析,判断出是读操作还是写操作,然后分发至对应的mysql服务器上。mysql-proxy是官方提供的mysql中间件产品可以实现负载平衡,读写分离,failover等MySQL Proxy就是这么一个中间层代理,简单的说,MySQL Proxy就是一个连接池,负责将前台应用的连接请求转发给后台的数据库,并且通过使用lua脚本,可以实现复杂的连接控制和过滤,从而实现读写分离和负载平衡。对于应用来说,MySQL Proxy是完全透明的,应用则只需要连接到MySQL Proxy的监听端口即可。当然,这样proxy机器可能成为单点失效,但完全可以使用多个proxy机器做为冗余,在应用服务器的连接池配置中配置到多 个proxy的连接参数即可。

实验系统:CentOS 6.6_x86_64

实验前提:防火墙和selinux都关闭

实验说明:本实验共有4台主机,IP分配如拓扑

实验软件:mariadb-10.0.20  mysql-proxy-0.8.5-linux-el6-x86-64bit

下载地址:http://pan.baidu.com/s/1i3F5Pop

实验拓扑:

    

一、准备工作:

  1.将主机名称改为如下所示:

    

  2.将hosts文件添加如下内容:

    

  3.master、slave1和slave2安装mariadb:

tar xf mariadb-10.0.-linux-x86_64.tar.gz  -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.0.-linux-x86_64 mysql
useradd -r mysql
mkdir -pv /mydata/data
chown -R mysql.mysql /mydata/data/
cd mysql/
chown -R root.mysql .
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

二、配置主从复制

  1.master上配置my.cnf:

[mysqld]
server-id =
datadir = /mydata/data
log-bin = /mydata/data/master-bin
binlog_format = ROW
sync_binlog = 1 //确保每次事务提交之前都能将二进制日志同步磁盘上

  2.slave1上配置my.cnf:

[mysqld]
#log-bin=mysql-bin
#binlog_format=mixed
server-id =
datadir = /mydata/data
relay_log = /mydata/data/relay-log
read_only =
sync_master_info = 1 //及时同步master文件
sync_relay_log = 1 //及时同步relay-log文件
sync_relay_log_info = 1 //及时同步relay-log-info文件

  3.slave2上配置my.cnf:

[mysqld]
#log-bin=mysql-bin
#binlog_format=mixed
server-id =
datadir = /mydata/data
relay_log = /mydata/data/relay-log
read_only =
sync_master_info =
sync_relay_log =
sync_relay_log_info =

  4.在master上创建复制用户:

service mysqld start
/usr/local/mysql/bin/mysql
------------------------------------------->
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'192.168.19.%' IDENTIFIED BY '';
FLUSH PRIVILEGES;

  5.在master上查看二进制日志位置:

SHOW MASTER LOGS;

    

  6.两台slave上操作:

CHANGE MASTER TO MASTER_HOST='master',MASTER_USER='slave',MASTER_PASSWORD='',MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=;
START SLAVE;
SHOW SLAVE STATUS\G

    

三、安装mysql-proxy

  1.此实验中19.79为mysql-proxy服务器,所以软件安装在此主机上:

tar xf mysql-proxy-0.8.-linux-el6-x86-64bit.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mysql-proxy-0.8.-linux-el6-x86-64bit mysql-proxy
useradd -r mysql-proxy

  2.提供服务脚本:

vim /etc/init.d/mysql-proxy
--------------------------------------------------------->
#!/bin/bash
#
# mysql-proxy This script starts and stops the mysql-proxy daemon
#
# chkconfig: -
# 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 # 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= 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 ]; then
touch /var/lock/subsys/mysql-proxy
fi
} stop() {
echo -n $"Stopping $prog: "
killproc -p $PROXY_PID -d $prog
RETVAL=$?
echo
if [ $RETVAL -eq ]; 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=
;;
esac exit $RETVAL
<---------------------------------------------------------
chmod +x /etc/init.d/mysql-proxy
chkconfig --add mysql-proxy

  3.为服务脚本提供配置文件:

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=""
PROXY_USER="mysql-proxy"
PROXY_OPTIONS="--daemon --log-level=info --log-use-syslog --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.19.66:3306 --proxy-read-only-backend-addresses=192.168.19.74:3306
--proxy-read-only-backend-addresses=192.168.19.76: --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua" //--daemon:以守护进程模式启动mysql-proxy
//--proxy-backend-addresses:后端可读写的mysql服务器的地址和端口
//--proxy-read-only-backend-addresses:后端只读mysql服务器的地址和端口
//--proxy-lua-script:完成mysql代理功能的Lua脚本

  4.提供admin.lua文件:

vim /usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua
------------------------------------------------------------------------------>
--[[ $%BEGINLICENSE%$
Copyright (c) , , 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 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., Franklin St, Fifth Floor, Boston, MA
- 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() 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 = , #proxy.global.backends do
local states = {
"unknown",
"up",
"down"
}
local types = {
"unknown",
"rw",
"ro"
}
local b = proxy.global.backends[i] rows[#rows + ] = {
i,
b.dst.name, -- configured backend address
states[b.state + ], -- the C-id is pushed down starting at
types[b.type + ], -- the C-id is pushed down starting at
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 + ] = { "SELECT * FROM help", "shows this help" }
rows[#rows + ] = { "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

  5.为了使实验结果更明显,编辑rw-splitting.lua文件中的其中2个数值:

vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
--------------------------------------------------------------------------->
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = , //默认为4
max_idle_connections = ,    //默认为8 is_debug = false
}
end
//mysql-proxy会检测客户端连接,当连接没有超过min_idle_connections预设值时, 不会进行读写分离, 即查询操作会发生到Master上。

5.启动mysql-proxy:

service mysql-proxy start
ss -tnlp //查看端口

    

  6.连接测试:

yum -y install mysql                               //如果没有mysql客户端的话执行此步
mysql -uadmin -padmin -h192.168.19. --port=
------------------------------------------------------------>
SELECT * FROM backends;
+-------------+--------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+--------------------+---------+------+------+-------------------+
| | 192.168.19.66: | unknown | rw | NULL | |
| | 192.168.19.74: | unknown | ro | NULL | |
| | 192.168.19.76: | unknown | ro | NULL | |
+-------------+--------------------+---------+------+------+-------------------+

四、读写分离测试:

  1.在master上创建测试用户:

GRANT ALL ON *.* TO 'jason'@'192.168.19.%' IDENTIFIED BY '';
FLUSH PRIVILEGES;

  2.分别在三台mariadb服务器上抓包:

    master:

tcpdump -i eth0 -nn -XX ip dst 192.168.19.66 and tcp dst port       //目标是19.66并且端口是3306

    slave1:

tcpdump -i eth0 -nn -XX ip dst 192.168.19.74 and tcp dst port 

    slave2:

tcpdump -i eth0 -nn -XX ip dst 192.168.19.76 and tcp dst port 

  3.mysql-proxy上进行数据库操作:

mysql -ujason -p123456 -h192.168.19.
------------------------------------------------->
CREATE DATABASE hello;
USE mysql;
SELECT * FROM user; //可以用额外的主机多执行几次

    在master上的抓包信息:

    

    在slave上的抓包信息:

    

    

  4.查看状态,在proxy上操作,可以看到状态全部为up:

mysql -uadmin -padmin -h192.168.19. --port=
------------------------------------------------------------->
SELECT * FROM backends;
+-------------+--------------------+-------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+--------------------+-------+------+------+-------------------+
| | 192.168.19.66: | up | rw | NULL | |
| | 192.168.19.74: | up | ro | NULL | |
| | 192.168.19.76: | up | ro | NULL | |
+-------------+--------------------+-------+------+------+-------------------+

五、拓展实验

  1.在proxy上安装httpd和php:

yum install httpd php php-mysql
service httpd start

  2.让httpd可以支持index.php首页,然后放入wordpress页面文件,创建wordpress数据库并安装:

    

  3.安装完后修改wordpress的配置文件,将master地址改为proxy的:

vim /var/www/html/wp-config.php

    

  4.访问测试并抓包:

    master:

    

    slave:

    

    

转载链接:https://www.cnblogs.com/tae44/p/4701226.html

使用mysql-proxy实现mysql的读写分离的更多相关文章

  1. 使用mysql proxy对数据库进行读写分离

    服务器安排如下: 192.168.100.128 主 192.168.100.129 从 192.168.100.130 mysql-proxy 1.在100.130中下载安装mysql-proxy ...

  2. 使用MySQL Proxy和MySQL Replication实现读写分离

    MySQL Replication可以将master的数据复制分布到多个slave上,然后可以利用slave来分担master的读压力.那么对于前台应用来说,就要考虑如何将读的压力分布到多个slave ...

  3. Mysql 之主从复制,mysql-proxy读写分离

    准备两台mysql服务器,master(192.168.43.64).slave(192.168.84.129) master配置: log-bin=mysql-bin binlog_format=m ...

  4. Mysql主从配置,实现读写分离

    大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢 ...

  5. 分布式架构高可用架构篇_08_MyCat在MySQL主从复制基础上实现读写分离

    参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...

  6. 黄聪:Mysql主从配置,实现读写分离

    大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢 ...

  7. Mysql多实例安装+主从复制+读写分离 -学习笔记

    Mysql多实例安装+主从复制+读写分离 -学习笔记 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px ...

  8. 高可用架构篇--MyCat在MySQL主从复制基础上实现读写分离

    实战操作可参考:http://www.roncoo.com/course/view/3117ffd4c74b4a51a998f9276740dcfb 一.环境 操作系统:CentOS-6.6-x86_ ...

  9. MySQL Proxy 实现 MySQL 读写分离提高并发负载

    还在学习,学完了在写笔记 (这个先安装lua:https://www.cnblogs.com/fps2tao/p/9163959.html ) 工作拓扑: MySQL Proxy有一项强大功能是实现“ ...

  10. mysql主从复制之mysql-proxy实现读写分离

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lansgg.blog.51cto.com/5675165/1242674 本文系 ...

随机推荐

  1. Django如何启动源码分析

    Django如何启动源码分析 启动 我们启动Django是通过python manage.py runsever的命令 解决 这句话就是执行manage.py文件,并在命令行发送一个runsever字 ...

  2. Logos讲解--逆向开发

    前言 Logos是CydiaSubstruct框架中提供的一组宏定义.利于开发者使用宏进行Hook操作,其语法简单,功能是非常强大且稳定. 详细内容logos语法为http://iphonedevwi ...

  3. PTA 1140 1141 1142 1143

    1140 Look-and-say Sequence 思路:模拟 #include<bits/stdc++.h> using namespace std; typedef long lon ...

  4. Spring Cloud Hoxton正式发布,Spring Boot 2.2 不再孤单

    距离Spring Boot 2.2.0的发布已经有一个半月左右时间,由于与之匹配的Spring Cloud版本一直没有Release,所以在这期间碰到不少读者咨询的问题都是由于Spring Boot和 ...

  5. 机器学习笔记(十)---- KNN(K Nearst Neighbor)

    KNN是一种常见的监督学习算法,工作机制很好理解:给定测试样本,基于某种距离度量找出训练集中与其最靠近的k个训练样本,然后基于这k个"邻居"的信息来进行预测.总结一句话就是&quo ...

  6. Python开发还在用virtualenv?不如了解下pipenv...#华为云·寻找黑马程序员#

    又见 Kenneth Reitz 之前公众号写了一篇文章爬虫新宠requests_html 带你甄别2019虚假大学,其中主要是为了介绍模块**requests_html,这个模块的作者还开发了req ...

  7. Java基础面试相关

    面试相关的问题(下) 四 Linux高级_ 1.Linux机器 变慢,怎么查看? (1)整机的性能 主要查看的是CPU和内存 先查看整机的top,使用命令 top 虚拟机 使用1可以查看哪个核被占用过 ...

  8. 大数据之Linux网卡配置

    当你安装好一台Linux之后,第一步自然是配置网卡,让你的Linux上网. 配置网卡大多数有两种模式,一种是桥接,一种是NAT 首先讲一下桥接网卡的上网原理,他是等同于在你现有的网络环境上直接现加一台 ...

  9. iOS FMDatabase 本地数据库的创建和几个基本使用方法

    转自:http://blog.it985.com/3677.html 使用数据库之前当然要先在网上下载FMDB的库,然后添加到自己的工程里面去.没有的请点击下面的来下载 fmdb 在FrameWork ...

  10. kubernetes学习笔记(二)——部署服务

    文章目录 (1)创建app (2)创建app的docker镜像 (3)部署app到k8s (4)参考资料 前面一篇文章部署好了minikube环境,这次学习下怎么部署app到kubernetes环境当 ...