前言
提升mysql性能,解决生产环境中读多写少等问题
一、什么是amoeba?
1、Amoeba(变形虫)项目,专注分布式数据库proxy开发。座落与Client、DB Server(s)之间。对客户端透明。具有负载均衡、高可用性、sql过滤、读写分离、可路由相关的query到目标数据库、可并发请求多台数据库合并结果。
主要解决:
·降低数据切分带来的复杂多数据库结构
·提供切分规则并降低数据切分规则给应用带来的影响·降低db 与客户端的连接数
·读写分离
2、为什么要用Amoeba
目前要实现mysql的主从读写分离,主要有以下几种方案:
1、通过程序实现,网上很多现成的代码,比较复杂,如果添加从服务器要更改多台服务器的代码。2、通过mysql-proxy来实现,由于mysql-proxy的主从读写分离是通过lua脚本来实现,目前lua的脚本的开发跟不上节奏,而写没有完美的现成的脚本,因此导致用于生产环境的话风险比较大,据网上很多人说mysql-proxy的性能不高。
3、自己开发接口实现,这种方案门槛高,开发成本高,不是一般的小公司能承担得起。
4、利用阿里巴巴的开源项目Amoeba来实现,具有负载均衡、高可用性、sql过滤、读写分离、可路由相关的query到目标数据库,并且安装配置非常简单。国产的开源软件,应该支持,目前正在使用,不发表太多结论,一切等测试完再发表结论吧,哈哈!
二、读写分离配置
1、硬件配置
master 192.168.85.11
s1ave192.168.85.12
proxy 192,168.85.14
2、proxy的相关配置
#1、下载mysql-proxy
https://downloads .mysq].com/archives/proxy/#downloads
#2、上传软件到proxy的机器
直接通过xftp进行上传
#3、解压安装包
tar -zxvf mysql-proxy-0.8.5-1inux-glibc2.3-x86-64bit.tar.gz
#4、修改解压后的目录
mv mysql-proxy-0.8.5-1inux-glibc2.3-x86-64bit mysql-proxy
#5、进入mysql-proxy的目录
cd mysq1-proxy
#6、创建目录
mkdir conf
mkdir 1ogs
#7、添加环境变量
#打开/etc/profile文件
vi /etc/profile
#在文件的最后面添加一下命令
export PATH=SPATH:/root/mysq1-proxy/bin#8、执行命令让环境变量生效
source /etc/profile
#9、进入conf目录,创建文件并添加一下内容vi mysq1-proxy .conf
添加内容
[mysq1-proxy]user=root
proxy-address=192.168.85.14:4040
proxy-backend-addresses=192.168.85.11:3306
proxy-read-on1y-backend-addresses=192.168.85.12:3306
proxy-lua-script=/root/mysq7-proxy/share/doc/mysq1-proxy/rw-splitting.lualog-file=/root/mysq1-proxy/logs/mysq1-proxy. log
log-level=debug
daemon=true
#10、开启mysq1-proxcy
mysq1-proxy --defaults-file=/root/mysq1-proxy/conf/mysq1-proxy.conf
#11、查看是否安装成功,打开日志文件
cd /root/mysql-proxy / logs
tai1 -100 mysq1-proxy. log#内容如下:表示安装成功
2019-10-11 21:49:41: (debug) max open file-descriptors = 1024
2019-10-11 21:49:41:(message) proxy listening on port 192.168.85.14:40402019-10-11 21:49:41: (message) added read/write backend: 192.168.85.11:33062019-10-11 21:49:41:(message) added read-only backend: 192.168.85.12:33062019-10-1121:49:41: (debug)now running as user: root (0/0)
3、进行连接
#mysq1的命令行会出现无法连接的情况,所以建议使用客户端
mysq1 -uroot -p123-h192.168.85.14 -p 4040
4、安装Amoeba
1、首先安装jdk,直接使用rpm包安装即可
2、下载amoeba对应的版本https://sourceforge.net/projects/amoebal,直接解压即可3、配置amoeba的配置文件
dbServers.xml
<?xml version="1.0" encoding="gbk "?>
<IDOCTYPE amoeba:dbservers SYSTEM "dbserver.dtd">
<amoeba: dbservers xm1ns :amoeba="http://amoeba.meidusa.com/"><!---
Each dbserver needs to be configured into a Poo1,
If you need to configure multiple dbserver with load balancing that can be
simp1ified by the fo11owing configu
ration:add attribute with name virtual '= "true" in dbServer,but the
configuration does not a1Tow the element with n
amefactoryconfig
such as" mu1tiPoo1 dbServer---!>
<dbServer name-"abstractserver" abstractive="true ">
<factoryConfig class="com. meidusa . amoeba.mysq1.net.Mysq7ServerConnectionFactory">
<property name="connectionManager" ${defau1tManager}</property>
<property name="sendBuffersize">64</property>
<property name="receiveBuffersize">128</property>
<I-- mysq1 port ->
<property name="port”>3306</property>
<!-- mysql schema-->
<property name="schema">msb</property>
<!-- mysgl user<propenty name="user">root</property>
<property name="password"s123</property></factorycontig>
<poo1Config class="com.meidusa.toolkit.common.poolable.PoolableobjectPoo1"><property name="maxActive">500</property>
name="miaxId1e">500</property><property
name="minIdle>1</property><property
name="minEvictableldleTimeMi11is">600000</property><property
<property name="timeBetweenEvictionRunsMilis">600000</property>
name='testOnBorrow'strue</property>cproperty
name="testOnReturn'>true</property><property
<property name="testwhileIdle">true</property>
</poo1Config><dbserver name="slave"parent="abstractserver"">
factoryconfig>
<1-- mysq1 ip-->
<property name="ipAddress">192.168.85.12</property></factoryconfig>
</dbserver>
<dbServer name="mys lave"" virtua1="true">
<poo1config class="com.meidusa. amoeba.server.Mu1tipleServerPoo1">
<l-- Load balancing strategy: 1=ROUNDROBIN ,2=WEIGHTBASED,3=HA--><property name="loadbalance">l</property>
<l-- Separated by commas , such as: serverl,server2,server1 --><property name="poo7Names ">s1ave</property>
</poo1config>
</dbserver>
/amoeba: dbservers>
amoeba.xml
<?xm1 version="1.0"encoding="gbk"?>
<! DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xm7ns: amoeba="http://amoeba.meidusa.com/">
<proxy>
<l-- service class must implements com.meidusa.amoeba.service.service --><service name="Amoeba for Mysq1"
class="com.mei dusa.amoeba.mysq1.server.MysQL Service">
<!-- port -->
<property name="port">8066</property>
<l-- bind ipAddress -->
<I--
<property name="ipAddress">127.0.0.1</property
<property name="connectionFactory">
<bean class="com.meidusa.amoeba.mysql.net.Mysq1c7ientConnectionFactory""
<property name="sendBufferSize">128</property>
<property name="receiveBuffersize">64</property></bean>
</property>
eee
<property name="authenticateProvider">
<bean class="com.meidusa.amoeba.mysq1.server.Mysq1clientAuthenticator">
<property name="user">root</property>
<property name="password">123</property>
<property name="filter>
</bean>
class="com.meidusa.toolkit.net. authenticate.server. IPAccessController">
<property
name="ipFile">${amoeba.home}/conf/access_list.conf</property>
</bean>
</property></bean>
</property>
</service>
<runtime class="com . meidusa.amoeba.mysq1.context.MysqlRuntimeContext">
<!-- proxy server client process thread size -->
<property name="executeThreadsize">128</property>
<l-- per connection cache prepared statement size --><property name=""statementcachesize">500</property>
<!-- default charset -->
<property name="servercharset">utf8</property>
<l-- query timeout( default: 60 second , Timeunit:second) --><property name=""queryTimeout"">60</property>
</runtime>
</proxy>
<l--
Each connectionManager wi11 start as thread
manager responsible for the connection Io read , Death Detection-->
<connectionManagerList>
<connectionManager name=""defau1tManager""
class="com.meidusa.toolkit.net.MultiConnectionManagerwrapper">
<property
name="subManagerClassName">com.meidusa.toolkit.net.AuthingableconnectionManager</propert
</ connectionManager>
</connectionManagerList>
<!-- default using file loader -->
<dbServerLoader class="com.meidusa.amoeba.context. DBServerconfigFileLoader">
<property name=" configFile">S{amoeba.home]/conf/dbservers.xml</property></ dbserverLoader>
<queryRouter class="com.meidusa. amoeba.mysq1.parser.Mysq1QueryRouter">
<property name="ruTeLoader"">
<bean class="com.meidusa. amoeba.route.TableRuleFileLoader"">
<property name="ruleFile">${amoeba. home}/conf/rule.xml</property><property
name="functionFle">S{amoeba.home}/conf/ruleFunctionMap . xmI</property>
</bean>
</property>
<property name="sq1FunctionFi1e">S{amoeba. home}/conf/functionMap.xm1</propertyproperty name="LRUMapsize"">1500</property
<property name="defau 1tPoo7">writedb</property>
<property name=""writePoo7"">writedb</property><property name="readPoo1"">myslave</property><property name=""needParse"">true</property></queryRouten>
</ amoeba: configuration>
5、启动amoeba
/root/amoeba-mysq1-3.0.5-RC/bin/launcher
6、测试amoeba
#测试的sq1
#在安装amoeba的服务器上登录mysql
mysql -h192.168.85.13 -uroot -p123 -P8066--分别在master. slave、 amoeba上登录mysqluse msb
select * from user;--在amoeba上插入数据
insert into user values (2,2);--在master和s lave上分别查看表中的数据select $ from user;
#将master上的mysq1服务停止,继续插入数据会发现插入不成功,但是能够查询
#将master上的msyq1服务开启,停止slave上的mysql,发现插入成,但是不能够查询