Amoeba for MySQL致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当query 路由功能,专注 分布式数据库 proxy 开发。座落与Client、DB Server(s)之间。对客户端透明。具有负载均衡、高可用性、Query过滤、读写分离、可路由相关的query到目标数据库、可并发请求多台数据库合并结果。 在Amoeba上面你能够完成多数据源的高可用、负载均衡、数据切片的功能。目前在很多企业的生产线上面使用。
www.centos.bz这里使用Amoeba for mysql来实现mysql的读写分离,起到缓解主数据库服务器的压力,下面是实现这一方案的架构图:

硬伤:不支持事务~~~~生产怎么用?
一:mysql主从复制的搭建
因为读写分离,所以一台负责mysql的写操作,另一台负责mysql的读操作,所以我们这里使用mysql的主从复制再合适不过了。关于这一配置,请移步:
https://www.centos.bz/2011/07/linux-mysql-replication-sync-configure/
二:java环境的配置
java环境配置
Amoeba框架是基于Java SE1.5开发的,建议使用Java SE 1.5版本。目前Amoeba经验证在JavaTM SE 1.5和Java SE 1.6能正常运行,(可能包括其他未经验证的版本)。
http://blog.itpub.net/29096438/viewspace-1743529/ ---另一篇博客
三:Amoeba的安装
Amoeba下载地址:http://sourceforge.net/projects/amoeba/
下面是安装步骤:
-
mkdir /usr/local/amoeba
-
wget http://softlayer.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/2.x/amoeba-mysql-binary-2.1.0-RC5.tar.gz
-
tar xzf amoeba-mysql-binary-2.1.0-RC5.tar.gz -C /usr/local/amoeba
Amoeba for mysql配置
配置Amoeba for mysql的读写分离主要涉及两个文件:
1、/usr/local/amoeba/conf/dbServers.xml
此文件定义由Amoeba代理的数据库如何连接,比如最基础的:主机IP、端口、Amoeba使用的用户名和密码等等。
2、/usr/local/amoeba/conf/amoeba.xml
此文件定义了Amoeba代理的相关配置。
dbServers.xml文件配置
abstractServer配置:
-
此部分定义真实mysql服务器的端口,数据库名称,mysql用户及密码。
-
<dbServer name="abstractServer" abstractive="true">
-
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
-
<property name="manager">${defaultManager}</property>
-
<property name="sendBufferSize">64</property>
-
<property name="receiveBufferSize">128</property>
-
-
<!-- mysql port -->
-
<property name="port">3306</property> ----mysql数据库的端口
-
-
<!-- mysql schema -->
-
<property name="schema">dbname</property> -----选择数据库
-
-
<!-- mysql user -->
-
<property name="user">root</property> ----连接用户名
-
-
<!-- mysql password -->
-
<property name="password">root</property> -----连接密码
-
-
</factoryConfig>
-
-
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
-
<property name="maxActive">500</property>
-
<property name="maxIdle">500</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>
此部分定义主服务器,从服务器,及从服务器连接池。这里只定义数据库地址,它们的用户及密码就是上面的abstractServer里的设置。注意用来连接真实mysql服务器的用户必须拥有远程连接权限。
-
<dbServer name="Master" parent="abstractServer"> -----主
-
<factoryConfig>
-
<!-- mysql ip -->
-
<property name="ipAddress">192.168.0.1</property>
-
</factoryConfig>
-
</dbServer>
-
<dbServer name="Slave1" parent="abstractServer"> ------从1
-
<factoryConfig>
-
<!-- mysql ip -->
-
<property name="ipAddress">192.168.0.2</property>
-
</factoryConfig>
-
</dbServer>
-
<dbServer name="Slave2" parent="abstractServer"> ----从2
-
<factoryConfig>
-
<!-- mysql ip -->
-
<property name="ipAddress">192.168.0.3</property>
-
</factoryConfig>
-
</dbServer>
-
<dbServer name="virtualSlave" virtual="true">
-
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
-
<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
-
<property name="loadbalance">1</property>
-
-
<!-- Separated by commas,such as: server1,server2,server1 -->
-
<property name="poolNames">Slave1,Slave2</property> ---salve1 slave2放入virtualslave连接池
-
</poolConfig>
-
</dbServer>
amoeba.xml配置
amoeba连接验证配置:
-
这里定义连接amoeba时用来验证的用户及密码。
-
<property name="authenticator">
-
<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
-
-
<property name="user">root</property>
-
-
<property name="password">ESBecs00</property>
-
-
<property name="filter">
-
<bean class="com.meidusa.amoeba.server.IPAccessController">
-
<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
-
</bean>
-
</property>
-
</bean>
-
</property>
读写分离配置:
-
<property name="defaultPool">Master</property>
-
<property name="writePool">Master</property>
-
<property name="readPool">virtualSlave</property> -----连接池
defaultPool:配置了默认的数据库节点,一些除了SELECT\UPDATE\INSERT\DELETE的语句都会在defaultPool执行。
writePool :配置了数据库写库,通常配为Master,如这里就配置为之前定义的Master数据库。
readPool :配置了数据库读库,通常配为Slave或者Slave组成的数据库池,如这里就配置之前的virtualSlave数据库池。
四:启动Amobe
启动命令:
此命令以前台的方式启动,会输出启动时的信息,检查没有错误信息后,中断,并后台运行:
五:运行及验证
-
通过这个命令在test数据库中创建了STAFF表,这里连接的是真实的MySQL数据库。
-
$ mysql -uroot -ppassword -h127.0.0.1 -P3306
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 125
-
Server version: 5.5.9 MySQL Community Server (GPL)
-
-
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
-
-
Oracle is a registered trademark of Oracle Corporation and/or its
-
affiliates. Other names may be trademarks of their respective
-
owners.
-
-
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
-
mysql> create table `test`.`staff`( 1
-
-> `ID` int NOT NULL AUTO_INCREMENT,
-
-> `NAME` varchar(10),
-
-> PRIMARY KEY (`ID`)
-
-> );
-
Query OK, 0 rows affected (0.00 sec)
-
mysql> use test
-
Database changed
-
mysql> show tables;
-
+----------------+
-
| Tables_in_test |
-
+----------------+
-
| staff |
-
+----------------+
-
1 row in set (0.00 sec)
通过amoeba连接mysql
-
启动Amoeba后这次通过mysql客户端来连接Amoeba,注意端口从真实的MySQL端口3306改为Amoeba端口8066:
-
$ mysql -uroot -ppassword -h127.0.0.1 -P8066
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 27778511
-
Server version: 5.1.45-mysql-amoeba-proxy-2.0.1-BETA MySQL Community Server (GPL)1 ----通过Server version可以得知连接的是Amoeba实例而不是MySQL实例
-
-
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
-
-
Oracle is a registered trademark of Oracle Corporation and/or its
-
affiliates. Other names may be trademarks of their respective
-
owners.
-
-
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
-
mysql> use test;
-
Database changed
-
mysql> INSERT INTO test.staff (ID, NAME)2
-
-> VALUES (12345, 'Daisy Li.');
-
Query OK, 1 row affected (0.03 sec)
-
-
mysql> SELECT * FROM test.staff LIMIT 0, 50;3
-
+-------+-----------+
-
| ID | NAME |
-
+-------+-----------+
-
| 12345 | Daisy Li. |
-
+-------+-----------+
-
1 row in set (0.00 sec)
-
-
mysql>
原文地址:https://www.centos.bz/2012/05/amoeba-for-mysql/
参考:http://docs.hexnova.com/amoeba/single-dbserver.html
http://docs.hexnova.com/amoeba/rw-splitting.html
http://www.cnblogs.com/lhj588/archive/2012/11/19/2777897.html
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-1819139/
,如需转载,请注明出处,否则将追究法律责任。