1、介绍下部署环境:
amoeba:60.60.1.32
masterDB:60.60.1.33
slaveDB:60.60.1.34
以上系统全为centos6.9
2、Amoeba框架是居于JDK1.5开发的,采用了JDK1.5的特性,所以还需要安装java环境,建议使用javaSE1.5以上的JDK版本 (不详述)
本次安装使用的java版本如下 [root@60-60-1-32 ~]# java-version
java version "1.7.0_80"
3、安装Amoeba
下载最新版本的Amoeba,我这里下载的是amoeba-mysql-3.0.5-RC-distribution.zip。Amoeba安装非常简单,直接解压即可使用,这里将Amoeba解压到/opt/amoeba目录下,这样就安装完成了
4、配置Amoeba
Amoeba的配置文件在本环境下位于/opt/amoeba/conf目录下。配置文件比较多,但是仅仅使用读写分离功能,只需配置两个文件即可,分别是dbServers.xml和amoeba.xml,如果需要配置ip访问控制,还需要修改access_list.conf文件,下面首先介绍dbServers.xml
[root@60-60-1-32conf]# cat dbServers.xml
<?xmlversion="1.0" encoding="gbk"?>
<!DOCTYPEamoeba:dbServers SYSTEM "dbserver.dtd">
<amoeba:dbServersxmlns:amoeba="http://amoeba.meidusa.com/">
<!--
Each dbServer needs to beconfigured into a Pool,
If you need to configuremultiple dbServer with load balancing that can be simplified by the followingconfiguration:
add attribute with name virtual = "true"in dbServer, but the configuration does not allow the element with namefactoryConfig
such as 'multiPool' dbServer
-->
<dbServername="abstractServer" abstractive="true">
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<propertyname="connectionManager">${defaultManager}</property>
<propertyname="sendBufferSize">64</property>
<propertyname="receiveBufferSize">128</property>
<!-- mysql port -->
<propertyname="port">3306</property>#设置Amoeba要连接的mysql数据库的端口,默认是3306
<!-- mysql schema -->
<propertyname="schema">testdb</property>#设置缺省的数据库,当连接amoeba时,操作表必须显式的指定数据库名,即采用dbname.tablename的方式,不支持 use dbname指定缺省库,因为操作会调度到各个后端dbserver
<!-- mysql user -->
<propertyname="user">test1</property>#设置amoeba连接后端数据库服务器的账号和密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接
<propertyname="password">111111</property>
</factoryConfig>
<poolConfigclass="com.meidusa.toolkit.common.poolable.PoolableObjectPool">
<propertyname="maxActive">500</property>#最大连接数,默认500
<propertyname="maxIdle">500</property>#最大空闲连接数
<propertyname="minIdle">1</property>#最新空闲连接数
<propertyname="minEvictableIdleTimeMillis">600000</property>
<propertyname="timeBetweenEvictionRunsMillis">600000</property>
<propertyname="testOnBorrow">true</property>
<propertyname="testOnReturn">true</property>
<propertyname="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="writedb" parent="abstractServer">#设置一个后端可写的dbServer,这里定义为writedb,这个名字可以任意命名,后面还会用到
<factoryConfig>
<!-- mysql ip -->
<propertyname="ipAddress">60.60.1.33</property>#设置后端可写dbserver
</factoryConfig>
</dbServer>
<dbServer name="readdb" parent="abstractServer">#设置后端可读dbserver
<factoryConfig>
<!-- mysql ip -->
<propertyname="ipAddress">60.60.1.34</property>
</factoryConfig>
</dbServer>
<dbServer name="myslave"virtual="true">#设置定义一个虚拟的dbserver,实际上相当于一个dbserver组,这里将可读的数据库ip统一放到一个组中,将这个组的名字命名为myslave
<poolConfigclass="com.meidusa.amoeba.server.MultipleServerPool">
<!-- Load balancingstrategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<propertyname="loadbalance">1</property>#选择调度算法,1表示复制均衡,2表示权重,3表示HA,这里选择1
<!-- Separated bycommas,such as: server1,server2,server1 -->
<propertyname="poolNames">readdb</property>#myslave组成员
</poolConfig>
</dbServer>
</amoeba:dbServers>
另一个配置文件amoeba.xml
[root@60-60-1-32conf]# cat amoeba.xml
<?xmlversion="1.0" encoding="gbk"?>
<!DOCTYPEamoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configurationxmlns:amoeba="http://amoeba.meidusa.com/">
<proxy>
<!-- service class must implementscom.meidusa.amoeba.service.Service -->
<service name="Amoeba forMysql" class="com.meidusa.amoeba.mysql.server.MySQLService">
<!-- port -->
<propertyname="port">8066</property>#设置amoeba监听的端口,默认是8066
<!-- bind ipAddress -->#下面配置监听的接口,如果不设置,默认监听所以的IP
<!--
<propertyname="ipAddress">127.0.0.1</property>
-->
<propertyname="connectionFactory">
<beanclass="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
<propertyname="sendBufferSize">128</property>
<propertyname="receiveBufferSize">64</property>
</bean>
</property>
<propertyname="authenticateProvider">
<beanclass="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<propertyname="user">root</property># 提供客户端连接amoeba时需要使用这里设定的账号 (这里的账号密码和amoeba连接后端数据库服务器的密码无关)
<property name="password">123456</property>
<propertyname="filter">
<beanclass="com.meidusa.toolkit.net.authenticate.server.IPAccessController">
<propertyname="ipFile">${amoeba.home}/conf/access_list.conf</property>
</bean>
</property>
</bean>
</property>
</service>
<runtimeclass="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
<!-- proxy server clientprocess thread size -->
<propertyname="executeThreadSize">128</property>
<!-- per connection cacheprepared statement size -->
<propertyname="statementCacheSize">500</property>
<!-- default charset -->
<propertyname="serverCharset">utf8</property>
<!-- query timeout( default:60 second , TimeUnit:second) -->
<propertyname="queryTimeout">60</property>
</runtime>
</proxy>
<!--
Each ConnectionManager will start asthread
manager responsible for theConnection IO read , Death Detection
-->
<connectionManagerList>
<connectionManagername="defaultManager"class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper">
<propertyname="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</property>
</connectionManager>
</connectionManagerList>
<!-- default using file loader-->
<dbServerLoaderclass="com.meidusa.amoeba.context.DBServerConfigFileLoader">
<propertyname="configFile">${amoeba.home}/conf/dbServers.xml</property>
</dbServerLoader>
<queryRouterclass="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<propertyname="ruleLoader">
<beanclass="com.meidusa.amoeba.route.TableRuleFileLoader">
<propertyname="ruleFile">${amoeba.home}/conf/rule.xml</property>
<propertyname="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
</bean>
</property>
<propertyname="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
<propertyname="LRUMapSize">1500</property>
<propertyname="defaultPool">writedb</property>#设置amoeba默认的池,这里设置为writedb
<propertyname="writePool">writedb</property>#这两个选项默认是注销掉的,需要取消注释,这里用来指定前面定义好的俩个读写池
<propertyname="readPool">myslave</property>
<propertyname="needParse">true</property>
</queryRouter>
</amoeba:configuration>
5、masterDB:60.60.1.33
slaveDB:60.60.1.34
在上面两台机器上搭建mysql5.5.47源码安装数据库 (不详述)
安装完mysql数据库之后:
5.1 配置两台数据库主从复制 binlog日志(不详述)
5.2分别在masterdb和slavedb上为amoedb授权
mysql>GRANT ALL ON testdb.* TO 'test1'@'60.60.1.32' IDENTIFIED BY '111111';
QueryOK, 0 rows affected (0.05 sec)
mysql>flush privileges;
QueryOK, 0 rows affected (0.02 sec)
6、启动amoeba
[root@bogonamoeba]# /usr/local/amoeba/bin/launcher
JavaHotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=16m; support wasremoved in 8.0
JavaHotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=96m; supportwas removed in 8.0
Thestack size specified is too small, Specify at least 228k
Error:Could not create the Java Virtual Machine.
Error:A fatal exception has occurred. Program will exit.
报错:
Error:Could not create the Java Virtual Machine.
Error:A fatal exception has occurred. Program will exit.
从错误文字上看,应该是由于stack size太小,导致JVM启动失败,要如何修改呢?
其实Amoeba已经考虑到这个问题,并将JVM参数配置写在属性文件里。现在,让我们通过该属性文件修改JVM参数。
修改jvm.properties文件JVM_OPTIONS参数。
[root@bogonamoeba]# vim /usr/local/amoeba/jvm.properties
改成:JVM_OPTIONS="-server-Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"
原为:JVM_OPTIONS="-server-Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"
再次启动查看端口
root@bogon~]# netstat -unlpt | grep java
tcp 0 0 :::8066 :::* LISTEN 1602/java
7、测试
在masterdb上创建数据库testdb
mysql> create database testdb;
QueryOK, 1 row affected (0.08 sec)
mysql>show databases;
+--------------------+
|Database |
+--------------------+
|information_schema |
|mydb |
|mysql |
|performance_schema |
|test |
|testdb |
+--------------------+
6rows in set (0.00 sec)
查看slavedb是否复制成功
mysql>show databases;
+--------------------+
|Database |
+--------------------+
|information_schema |
|mydb |
|mysql |
|performance_schema |
|test |
|testdb |
+--------------------+
6rows in set (0.00 sec)
远程登陆mysql客户端通过指定amoeba配置文件中指定的用户名、密码、和端口以及amoeba服务器ip地址链接mysql数据库
[root@lys2~]# mysql -h60.60.1.32 -uroot -p -P8066 密码是123456
在testdb中创建表test并插入数据
mysql>use testdb;
Databasechanged
mysql>create table test_table(id int,password varchar(40) not null);
QueryOK, 0 rows affected (0.19 sec)
mysql>show tables;
+------------------+
|Tables_in_testdb |
+------------------+
|test_table |
+------------------+
1row in set (0.02 sec)
mysql>insert into test_table(id,password) values('1','test1');
QueryOK, 1 row affected (0.04 sec)
mysql>select * from test_table;
+------+----------+
|id | password |
+------+----------+
| 1 | test1 |
+------+----------+
1row in set (0.02 sec)
分别登陆masterdb和slavedb查看数据
masterdb:
mysql>use testdb;
Databasechanged
mysql>show tables;
+------------------+
|Tables_in_testdb |
+------------------+
|test_table |
+------------------+
1row in set (0.00 sec)
mysql>select * from test_table;
+------+----------+
|id | password |
+------+----------+
| 1 | test1 |
+------+----------+
1row in set (0.03 sec)
slavedb:
mysql>use testdb;
Databasechanged
mysql>show tables;
+------------------+
|Tables_in_testdb |
+------------------+
|test_table |
+------------------+
1row in set (0.00 sec)
mysql>select * from test_table;
+------+----------+
|id | password |
+------+----------+
| 1 | test1 |
+------+----------+
1row in set (0.00 sec)
停掉masterdb,然后在客户端分别执行插入和查询功能
masterdb:
[root@bogon~]# service mysqld stop
Shutting down MySQL. SUCCESS!
客户端:
mysql>insert into test_table(id,password) values('2','test2');
ERROR1044 (42000): Amoeba could not connect to MySQL server[192.168.2.204:3306],拒绝连接
mysql>select * from test_table;
+------+----------+
|id | password |
+------+----------+
| 1 | test1 |
+------+----------+
1row in set (0.01 sec)
可以看到,关掉masterdb和写入报错,读正常
开启masterdb上的msyql 关闭slave上的mysql
masterdb:
[root@bogon~]# service mysqld start
StartingMySQL.. SUCCESS!
slavedb:
[root@localhost~]# service mysqld stop
Shuttingdown MySQL. SUCCESS!
客户端再次尝试
mysql>insert into test_table(id,password) values('2','test2');
QueryOK, 1 row affected (0.19 sec)
mysql>select * from test_table;
ERROR1044 (42000): poolName=myslave, no valid pools
开启slavedb上的mysql,查看数据是否自动同步
slavedb:
mysql>select * from test_table;
+------+----------+
|id | password |
+------+----------+
| 1 | test1 |
| 2 | test2 |
+------+----------+
2rows in set (0.01 sec)
接着客户端:
mysql>insert into test_table(id,password) values('3','test3');
QueryOK, 1 row affected (0.03 sec)
mysql>select * from test_table;
+------+----------+
|id | password |
+------+----------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
+------+----------+
3rows in set (0.02 sec)
Ok搭建完毕!
Amoeba主配置文件($AMOEBA_HOME/conf/amoeba.xml),用来配置Amoeba服务的基本参数,如Amoeba主机地址、端口、认证方式、用于连接的用户名、密码、线程数、超时时间、其他配置文件的位置等。
数据库服务器配置文件($AMOEBA_HOME/conf/dbServers.xml),用来存储和配置Amoeba所代理的数据库服务器的信息,如:主机IP、端口、用户名、密码等。
切分规则配置文件($AMOEBA_HOME/conf/rule.xml),用来配置切分规则。
数据库函数配置文件($AMOEBA_HOME/conf/functionMap.xml),用来配置数据库函数的处理方法,Amoeba将使用该配置文件中的方法解析数据库函数。
切分规则函数配置文件($AMOEBA_HOME/conf/ruleFunctionMap.xml),用来配置切分规则中使用的用户自定义函数的处理方法。
访问规则配置文件($AMOEBA_HOME/conf/access_list.conf),用来授权或禁止某些服务器IP访问Amoeba。
日志规格配置文件($AMOEBA_HOME/conf/log4j.xml),用来配置Amoeba输出日志的级别和方式。