假设有这样的使用场景,有三个数据库节点分别命名为Master、Slave1、Slave2如下:
Amoeba <10.105.11.150> (Amoeba 服务器) Master <10.105.11.151> (可读写) Slave1 <10.105.11.152> (2个平等的数据库。只读/负载均衡) Slave2 <10.105.11.153> (2个平等的数据库。只读/负载均衡)
在 主从数据库 的复制的部分, 任然需要使用数据库自己的复制机制, Amoeba 不提供复制功能。
一、开启MYSQL数据库的主从复制功能
1.修改配置文件
Master.cnf
server-id = 1 # 主数据库标志 log-bin=mysql-bin # 日志的名称 binlog-do-db=skg # 同步的数据库 binlog-ignore-db="mysql" # 不同步的数据库
slave1.cnf
server-id = 2 # 备数据库标志 replicate-do-db=skg # 需要同步的库 replicate-ignore-db=mysql # 忽略的数据库 replicate-ignore-db=test # 忽略的数据库
slave2.cnf
server-id = 3 # 备数据库标志 replicate-do-db=skg # 需要同步的库 replicate-ignore-db=mysql # 忽略的数据库 replicate-ignore-db=test # 忽略的数据库
2.Maste中创建两个只读权限的用户
用户名均为:repl_user 密码均为:copy 分别开放给 slave1, slave2
mysql> grant replication slave on *.* to [email protected] identified by 'copy'; mysql> grant replication slave on *.* to [email protected] identified by 'copy';
3.查看 Master 信息
mysql> show master status;
4.Slave1 ,Slave2 中 启动 Master - Slave 复制功能。
分别执行以下命令
mysql> slave stop; mysql> change master to master_host='10.105.11.151', master_user='repl_user', master_password='copy', master_log_file='mysql-bin.000001', master_log_pos=0; mysql> start slave; mysql> show slave status \G
当Slave_IO_Running 和 Slave_SQL_Running 两列的值都为 "Yes",表明 Slave 的 I/O 和 SQL 线程都在正常运行,如果出现错误,可以从Last_Error这个参数中看出哪里出错,然后进行排查
5.Amoeba 读写分离的配置
Master、Slave1、Slave2 中开放权限给 Amoeba 访问。在 Master、Slave1、Slave2 中分别执行。
mysql> grant all on skg.* to [email protected] identified by 'skg';
二、安装运行Amoeba
1.下载 Amoeba
在SourceForge的主页下载Amoeba,http://sourceforge.net/projects/amoeba/files/
本例使用版本:amoeba-mysql-3.0.5-RC-distribution.zip
2.安装 Amoeba
Amoeba框架是基于Java SE1.5开发的,建议使用Java SE1.5以上的版本
注:JDK的安装方法不再阐述
把下载的压缩包解压完了之后放到 /data/setup/ 目录下即可。
# mkdir /data/soft/amoeba # unzip amoeba-mysql-3.0.5-RC-distribution.zip # /bin/cp –rf amoeba-mysql-3.0.5-RC/* /data/setup/amoeba
3.修改 Amoeba 的配置文件
配置文件详细说明请查看 官方文档:http://docs.hexnova.com/amoeba/rw-splitting.html
dbServers.xml
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd"> <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/"> <dbServer name="abstractServer" abstractive="true"> <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <property name="connectionManager">${defaultManager}</property> <property name="sendBufferSize">64</property> <property name="receiveBufferSize">128</property> <!-- mysql port --> <property name="port">3306</property> <!-- mysql schema --> <property name="schema">test</property> <!-- mysql user --> <property name="user">skg</property> <property name="password">skg</property> </factoryConfig> <poolConfig class="com.meidusa.toolkit.common.poolable.PoolableObjectPool"> <property name="maxActive">500</property> <property name="maxIdle">500</property> <property name="minIdle">1</property> <property name="minEvictableIdleTimeMillis">600000</property> <property name="timeBetweenEvictionRunsMillis">600000</property> <property name="testOnBorrow">true</property> <property name="testOnReturn">true</property> <property name="testWhileIdle">true</property> </poolConfig> </dbServer> <dbServer name="master" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">10.105.11.151</property> </factoryConfig> </dbServer> <dbServer name="slave1" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">10.105.11.152</property> </factoryConfig> </dbServer> <dbServer name="slave2" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">10.105.11.153</property> </factoryConfig> </dbServer> <dbServer name="slaves" 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> </poolConfig> </dbServer> </amoeba:dbServers>
amoeba.xml
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd"> <amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/"> <proxy> <!-- service class must implements com.meidusa.amoeba.service.Service --> <service name="Amoeba for Mysql" class="com.meidusa.amoeba.mysql.server.MySQLService"> <!-- port --> <property name="port">8066</property> <!-- bind ipAddress --> <!-- <property name="ipAddress">127.0.0.1</property> --> <property name="connectionFactory"> <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory"> <property name="sendBufferSize">128</property> <property name="receiveBufferSize">64</property> </bean> </property> <!-- Amoeba 账号,密码 --> <property name="authenticateProvider"> <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> <property name="user">root</property> <property name="password">root</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.mysql.context.MysqlRuntimeContext"> <!-- proxy server client process thread size --> <property name="executeThreadSize">128</property> <!-- per connection cache prepared statement size --> <property name="statementCacheSize">500</property> <!-- default charset --> <property name="serverCharset">utf8</property> <!-- query timeout( default: 60 second , TimeUnit:second) --> <property name="queryTimeout">60</property> </runtime> </proxy> <connectionManagerList> <connectionManager name="defaultManager" class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper"> <property name="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</property> </connectionManager> </connectionManagerList> <!-- default using file loader --> <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader"> <property name="configFile">${amoeba.home}/conf/dbServers.xml</property> </dbServerLoader> <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> <property name="ruleLoader"> <bean class="com.meidusa.amoeba.route.TableRuleFileLoader"> <property name="ruleFile">${amoeba.home}/conf/rule.xml</property> <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property> </bean> </property> <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property> <property name="LRUMapSize">1500</property> <!-- 默认数据库,主数据库 --> <property name="defaultPool">master</property> <!-- 写数据库 --> <property name="writePool">master</property> <!-- 读数据库,dbServer.xml 中配置的 虚拟数据库,数据库池 --> <property name="readPool">slaves</property> <property name="needParse">true</property> </queryRouter> </amoeba:configuration>
Rule.xml
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:rule SYSTEM "rule.dtd"> <amoeba:rule xmlns:amoeba="http://amoeba.meidusa.com/"> <tableRule name="TestTable,user,message" schema="test" defaultPools="master"> </tableRule> </amoeba:rule>
不需要 数据库分片时,不用配置。 但是不能没有 tableRule 元素, 否则报错。 随便写个空规则就行了。
4.启动 master Amoeba
/data/soft/amoeba/bin/lanuncher
客户端连接:
db.url=jdbc:mysql://10.105.11.150:8066/skg
db.username=root
db.password=root