首页 > 代码库 > 搭建基于MySQL的读写分离工具Amoeba
搭建基于MySQL的读写分离工具Amoeba
搭建基于MySQL的读写分离工具Amoeba:
Amoeba工具是实现MySQL数据库读写分离的一个工具,前提是基于MySQL主从复制来实现的;
实验环境(虚拟机):
主机 角色
10.10.10.20 多实例加主从复制
10.10.10.30 Amoeba服务器
10.10.10.40 客户端(最后测试使用)
1、首先搭建MySQL的主从复制(不在多提);
需要注意的是:在主从库上需要创建一个用户,在主库上创建的用户为amoeba,权限是create,update,insert,delete;
在从库上创建的用户也是amoeba,权限是select;
2、由于Amoeba是基于java的环境去实现的,所以要先安装jdk插件:(先通过windows主机去下载)
http://pan.baidu.com/share/link?shareid=2793927523&uk=1678158691&fid=117337971851932
下载完成后通过xshell自带的ftp软件传到服务器上,然后进行安装:
[root@localhost tools]# tar xf jdk-7u79-linux-x64.tar.gz
[root@localhost tools]# vim /etc/profile ##编辑jdk需要的环境变量;
export JAVA_HOME=/root/tools/jdk1.7.0_79
export JRE_HOME=/root/tools/jdk1.7.0_79/jre
export PATH=/root/tools/jdk1.7.0_79/bin:$PATH
export CLASSAPTH=./:/root/tools/jdk1.7.0_79/lib:/root/tools/jdk1.7.0_79/lib
[root@localhost tools]# java -version ##检测jdk是否安装成功,出现如下信息算安装成功;
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
3、下载并安装Amoeba软件:
[root@localhost tools]# mkdir /usr/local/amoeba ##创建一个目录用来放amoeba的软件;
https://sourceforge.net/projects/amoeba/ ##windows主机登录网址到网页中部的地方下载 Released /OldFiles/amoeba-mysql-0.19.zip,通过xshell自带FTP工具放到服务器上;
[root@localhost amoeba]# cd /usr/local/amoeba/
[root@localhost amoeba]# unzip amoeba-mysql-0.19.zip
[root@localhost amoeba]# ls
LICENSE.txt README.html amoeba-mysql-0.19.zip bin build.xml conf doc lib logs src ##主要用到的目录是conf这个配置目录;
[root@localhost conf]# ls
access_list.conf amoeba.dtd amoeba.xml function.dtd functionMap.xml log4j.dtd log4j.xml rule.dtd rule.xml ruleFunctionMap.xml
[root@localhost conf]# vim amoeba.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">
<server>
<!-- proxy server°樵???-->
<property name="port">3306</property> ##配置的是Amoeba的服务端口,这里用3306就可以,默认的是8806;
<!-- proxy server°樵éP -->
<property name="ipAddress">10.10.10.30</property> ##配置的是Amoeba服务器的IP地址,默认的是127.0.0.1,改为物理网卡上的IP地址;
<!-- proxy server net IO Read thread size -->
<property name="readThreadPoolSize">100</property> ##下面的四个数字可以调大一点;
<!-- proxy server client process thread size -->
<property name="clientSideThreadPoolSize">100</property>
<!-- mysql server data packet process thread size -->
<property name="serverSideThreadPoolSize">200</property>
<!-- socket Send and receive BufferSize(unit:K) -->
<property name="netBufferSize">128</property>
<!-- Enable/disable TCP_NODELAY (disable/enable Nagle‘s algorithm). -->
<property name="tcpNoDelay">true</property>
<!-- ????μ??§? -->
<property name="user">root</property> ##配置客户端通过Amoeba连接数据库的用户,默认的是root;
<property name="password">xyp123123</property> ##配置的是客户端通过Amoeba连接数据库的密码,默认是被注释掉的;
</server>
<dbServer name="server1"> ##定义mysql池的名字;
<!-- PoolableObjectFactory?Ж` -->
<factoryConfig>
<className>com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory</className>
<property name="manager">defaultManager</property>
<!-- ??mysql??????-->
<property name="port">3306</property> ##主库的端口号;
<!-- ??mysql???? -->
<property name="ipAddress">10.10.10.20</property> ##主库的IP地址;
<property name="schema">xyp</property> ##需要进行操作的库;
<!-- ??μ??mysqlμ??§? -->
<property name="user">amoeba</property> ##用于Amoeba连接数据库的用户;
<!-- ??μ??mysqlμ??鞭->
<property name="password">123123</property> ##用于Amoeba连接数据库的密码;
</factoryConfig>
<!-- ObjectPool?Ж` -->
<poolConfig>
<className>com.meidusa.amoeba.net.poolable.PoolableObjectPool</className> ##这里的不用动;
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
<dbServer name="server2"> ##定义从库的池;
<!-- PoolableObjectFactory?Ж` -->
<factoryConfig>
<className>com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory</className>
<property name="manager">defaultManager</property>
<!-- ??mysql??????-->
<property name="port">3307</property> ##从库端口;
<!-- ??mysql???? -->
<property name="ipAddress">10.10.10.20</property> ##从库ip地址;
<property name="schema">xyp</property> ##从库上需要进行操作的库;
<!-- ??μ??mysqlμ??§? -->
<property name="user">amoeba</property> ##amoeba连接从库的用户;
<!-- ??μ??mysqlμ??鞭->
<property name="password">123123</property> ##amoeba连接从库的密码;
</factoryConfig>
<!-- ObjectPool?Ж` -->
<poolConfig>
<className>com.meidusa.amoeba.net.poolable.PoolableObjectPool</className> ##下面的不用动;
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="master" virtual="true"> ##定义操作的池,master为主库
<poolConfig>
<className>com.meidusa.amoeba.server.MultipleServerPool</className>
<!-- ?o???? 1=ROUNDROBIN , 2=WEIGHTBASED -->
<property name="loadbalance">1</property>
<!-- 2??pool?o???poolName±?oo???->
<property name="poolNames">server1</property> ##调用上面主库的池;
</poolConfig>
</dbServer>
<dbServer name="slave" virtual="true"> ##定义从库的池,名字为slave;
<poolConfig>
<className>com.meidusa.amoeba.server.MultipleServerPool</className>
<!-- ?o???? 1=ROUNDROBIN , 2=WEIGHTBASED -->
<property name="loadbalance">1</property>
<!-- 2??pool?o???poolName±?oo???->
<property name="poolNames">server2</property> ##调用上面从库的池;
</poolConfig>
</dbServer>
</dbServerList>
<queryRouter>
<className>com.meidusa.amoeba.mysql.parser.MysqlQueryRouter</className>
<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
<property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
<property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
<property name="LRUMapSize">1500</property>
<property name="defaultPool">master</property> ##默认的池的名字;
<property name="writePool">master</property> ##写的池的名字;
<property name="readPool">slave</property> ##读的池的名字;
<property name="needParse">true</property>
</queryRouter>
[root@localhost conf]# chmod +x -R /usr/local/amoeba/bin/ ##给启动命令amoeba授权;
[root@localhost amoeba]# /usr/local/amoeba/bin/amoeba & ##后台启动;
log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml ##这条信息不是报错,属于正常现象;
[root@localhost amoeba]# jobs ##查看后台启动的服务;
[1]- Running ../bin/amoeba & (wd: /usr/local/amoeba/conf)
4、测试:
[root@localhost~]# mysql -uroot -pxyp123123 -h 10.10.10.30 ##通过客户端去连接amoeba服务器来登录到mysql;
mysql> show databases; ##查看数据库,已经存在需要配置的库xyp;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| xyp |
+--------------------+
5 rows in set (0.02 sec)
去从库上停掉slave,方便测试;
mysql> use xyp
Database changed
mysql> create table student(id int(10) not null,name char(20) not null,age int(2) not null); ##在库里面创建一个表;
mysql> insert into student values(1,‘x‘,23); ##插入一条信息做测试;
mysql> select * from xyp.student; ##先去主库上去查,有数据;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | x | 23 |
+----+------+-----+
1 row in set (0.02 sec)
mysql> select * from xyp.student; ##在amoeba上通过select去查结果没东西,因为从库的状态是停掉的
Empty set (0.01 sec)
mysql> start slave; ##在从库上把状态打开;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from xyp.student; ##在从amoeba上查看,数据已经被同步过来了;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | x | 23 |
+----+------+-----+
1 row in set (0.02 sec)
搭建基于MySQL的读写分离工具Amoeba
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。