Amoeba实现mysql主从读写分离

时间:2021-03-20 11:24:15

Amoeba实现mysql主从读写分离

这段在网上看了下关于amoeba的文章,总体感觉好像要比mysql-proxy好的多,也参考了不少的资料,此文章可能与其他文章作者会有雷同的地方,请谅解,但是此过程确实是本人亲自试验得来的.
 

试验环境

操作系统:  SUSE Linux Enterprise Server 10 (x86_64)
 
服务器IP:  10.10.0.72  10.10.0.77  10.10.0.87
 
服务器角色:  10.10.0.72作为mysql的master服务器, 0.77作为mysql的slave服务
             器,0.87作为amoeba服务器
 
软件列表:    MySQL-client-community-5.1.54-1.sles10.i586.rpm
             MySQL-server-community-5.1.54-1.sles10.i586.rpm
             amoeba-mysql-1.3.1-BETA.zip
             jdk-1_5_0_16-linux-i586-rpm.bin

Amoeba的简介

Amoeba(变形虫)项目,专注分布式数据proxy 开发.座落与Client、DB Server(s)之间.对客户端透明.具有负载均衡、高可用性、sql过滤、读写分离、可路由相关的query到目标数据库、可并发请求多台数据库合并结果.
 
主要解决了哪些问题:

? 降低数据切分带来的复杂多数据库结构

? 提供切分规则并降低数据切分规则给应用带来的影响

? 降低db 与客户端的连接数

? 读写分离

 
这里关于mysql的安装就不说了,网上的教程比较多.

实现mysql的主从复制

在master服务器上执行如下命令:
#mysql –uroot –ptest
mysql> grant replication slave,file on *.* to 'sxit'@'10.10.0.77' identified by '123456';
mysql> flush privileges;
mysql> exit;
 
//修改master的my.cnf的配置
 
log-bin=mysql-bin            //打开mysql二进制日志
server-id = 1                //设置mysql_id,主从不能相同
binlog-do-db=test            //设置二进制日志记录的库
sync_binlog=1      
 
//修改slave的my.cnf的配置
 
log-bin=mysql-bin
server-id = 2
replicate-do-db=test          //设置同步的库
log-slave-updates            //同步后记录二进制日志   
sync_binlog=1
slave-net-timeout=60
 
分别重启主从mysqld服务,登录master服务器.
 
在master服务器上执行如下命令
 
# mysql –u root –ptest
mysql>  flush tables with read lock;
mysql>  show master status\G;
 
*************************** 1. row ***************************
 File: mysql-bin.000002
 Position: 106
 Binlog_Do_DB: test
Binlog_Ignore_DB:
 
mysql> unlock tables;
 
登陆到slave服务器,执行如下命令:
 
# mysql –u root –ptest
 
mysql>  stop slave;
 
mysql> change master to master_host='10.10.0.72', master_user='sxit', master_password='123456',
       master_log_file='mysql-bin.000002',master_log_pos=106;
 
mysql>  start slave;
 
mysql>  show slave status\G;
 
如果出现下面的情况,说明主从同步已经成功!
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

安装amoeba

由于amoeba基于JDK1.5开发的,采用了JDK1.5的特性,所以我们这里要安装JDK,到10.10.0.87服务器上安装相关程序
 
# chmod +x jdk-1_5_0_16-linux-i586-rpm.bin
# ./ jdk-1_5_0_16-linux-i586-rpm.bin
#  vi /etc/profile          //在文件里面添加jdk的环境变量,添加内容如下
 
JAVA_HOME=/usr/java/jdk1.5.0_16
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
export CLASSPATH
 
# source  /etc/profile
 
# mkdir amoeba
# cd  /usr/local/amoeba
# unzip amoeba-mysql-1.3.1-BETA.zip
# chmod -R +x /usr/local/amoeba/bin/
 
Amoeba For MySQL 的使用非常简单,所有的配置文件都是标准的XML 文件,总共有四个配置文件.分别为:
 
◆ amoeba.xml:主配置文件,配置所有数据源以及Amoeba自身的参数设置,实现主从的话配
   置这个文件就可以了;
 
◆ rule.xml:配置所有Query 路由规则的信息;
 
◆ functionMap.xml:配置用于解析Query 中的函数所对应的Java 实现类;
 
◆ rullFunctionMap.xml:配置路由规则中需要使用到的特定函数的实现类;
 
通过更改amoeba.xml配置文件实现mysql主从读写分离,配置如下:
 
<?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">8066</property>
 
                <!-- proxy server绑定的IP -->
                <property name="ipAddress">10.10.0.87</property>
                <!-- proxy server net IO Read thread size -->
                <property name="readThreadPoolSize">20</property>
 
                <!-- proxy server client process thread size -->
                <property name="clientSideThreadPoolSize">30</property>
 
                <!-- mysql server data packet process thread size -->
                <property name="serverSideThreadPoolSize">30</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>
 
                <!-- 对外验证的密码 -->
              
                <property name="password">sxit</property>
 
                <!-- query timeout( default: 60 second , TimeUnit:second) -->
                <property name="queryTimeout">60</property>
        </server>
 
        <!--
                每个ConnectionManager都将作为一个线程启动。
                manager负责Connection IO读写/死亡检测
        -->
        <connectionManagerList>
           <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
  <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager
</property>
     <!--
       default value is avaliable Processors
         <property name="processors">5</property>
     -->
</connectionManager>
</connectionManagerList>
 
<dbServerList>
     <!--
        一台mysqlServer 需要配置一个pool,
        如果多台 平等的mysql需要进行loadBalance,
        平台已经提供一个具有负载均衡能力的objectPool:com.meidusa.amoeba.mysql.server.MultipleServerPool
         简单的配置是属性加上 virtual="true",该Pool 不允许配置factoryConfig
         或者自己写一个ObjectPool。
     -->
<dbServer name="server1">
 
     <!-- PoolableObjectFactory实现类 -->
        <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
             <property name="manager">defaultManager</property>
 
                <!-- 真实mysql数据库端口 -->
                     <property name="port">3306</property>
 
                <!-- 真实mysql数据库IP -->
                      <property name="ipAddress">10.10.0.72</property>
                      <property name="schema">test</property>
 
                <!-- 用于登陆mysql的用户名 -->
                       <property name="user">test</property>
 
                 <!-- 用于登陆mysql的密码 -->
 
                        <property name="password">123456</property>
 
            </factoryConfig>
 
      <!-- ObjectPool实现类 -->
         <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
                <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="server2">
 
             <!-- PoolableObjectFactory实现类 -->
               <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
                   <property name="manager">defaultManager</property>
 
                     <!-- 真实mysql数据库端口 -->
                       <property name="port">3306</property>
 
                      <!-- 真实mysql数据库IP -->
                        <property name="ipAddress">10.10.0.77</property>
                         <property name="schema">test</property>
 
                         <!-- 用于登陆mysql的用户名 -->
                          <property name="user">test</property>
 
                         <!-- 用于登陆mysql的密码 -->
 
                           <property name="password">123456</property>
 
             </factoryConfig>
 
            <!-- ObjectPool实现类 -->
              <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
                   <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">
             <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
                      <!-- 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
                         <property name="loadbalance">1</property>
 
                       <!-- 参与该pool负载均衡的poolName列表以逗号分割 -->
                          <property name="poolNames">server1</property>
              </poolConfig>
       </dbServer>
       <dbServer name="slave" virtual="true">
            <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
                 <!-- 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
                       <property name="loadbalance">1</property>
 
                  <!-- 参与该pool负载均衡的poolName列表以逗号分割 -->
                        property name="poolNames">server1,server2</property>
               </poolConfig>
        </dbServer>
 
 
        </dbServerList>
 
<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
             <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>
</amoeba:configuration>
 
 # /usr/local/amoeba/bin/amoeba &   //启动amoeba服务
 
# ps aux |grep  /usr/local/amoeba
root      9116  0.0  1.0 460460 43028 pts/0    Sl   12:50   0:00 /usr/java/jdk1.5.0_16/bin/java -server -Xms256m -Xmx256m -Xss128k -Damoeba.home=/usr/local/amoeba -Dclassworlds.conf=/usr/local/amoeba/bin/amoeba.classworlds -classpath /usr/local/amoeba/lib/classworlds-1.0.jar org.codehaus.classworlds.Launcher
 
# netstat -lntpu |grep 8066
tcp        0      0 10.10.0.87:8066         :::*      
看到进程以及端口说明amoeba服务启动正常
测试mysql的读写分离
测试之前先要保证amoeba-server有访问两个主从服务器test库的权限,在主从mysql上都执行:
mysql>  grant all on test.* to test@'10.10.0.%' identified by '123456';
mysql>  flush privileges;
 
登录amoeba服务器使用如下命令(用户名密码和上面配置的要一致):
#  mysql -u root -psxit -h10.10.0.87  -P8066  //如果可以登陆并且没有任何错误就说
                                               明是OK的.
 
登录上去后,为了测试读和写必须先把mysql的主从复制停掉,才能更清楚地看出读写的服务器是哪台,在从上使用stop slave;登录到amoeba-mysql上,使用命令mysql -uroot -psxit -h10.10.0.87  -P8066,然后执行写和读操作,查看写的是哪台服务器,读的是哪台服务器,实验结果显示:写只在主上进行,读在主和从都进行,比率是1:1
 
在没有停止同步之前先在master服务器上创建一个表
mysql>  create table sxit (id int(10) ,name varchar(10));
 
然后在slave服务器上停止数据同步
 
mysql> stop slave;
 
在主从上各插入一条不同数据(供测试读的时候用):
在主上插入:insert into sxit values('1','zhangsan');
在从上插入:insert into sxit values('2','lisi');
 
登陆到amoeba服务器,进行读写分离的测试:
 
# mysql -u root -psxit -h10.10.0.87  -P8066
 
mysql> use test;
mysql> select * from sxit;          //第一次执行select是在master服务器上查询的
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)
 
mysql> select * from sxit;           //第二次执行select是在slave服务器上查询的
+------+------+
| id   | name |
+------+------+
|    2 | lisi |
+------+------+
1 row in set (0.01 sec)
 
mysql> insert into sxit values('3','wangmazi');  //插入一条数据,然后在select
Query OK, 1 row affected (0.05 sec)
 
 
mysql> select * from sxit;      //可以看到上面新插入的语句在master上了
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
|    3 | wangmazi |
+------+----------+
2 rows in set (0.00 sec)
 
mysql> select * from sxit;         //slave服务器上是没有这条新的语句的,
+------+------+
| id   | name |
+------+------+
|    2 | lisi |
+------+------+
1 row in set (0.00 sec)
权重的设置
在这里可能会想到,两台数据库服务器,一台主,一台从,按照上面的配置只能是主和从的读取比率是1:1,而写又全部在主上进行,这样主的压力就很大了,所以如果能让主和从的读设置权重,比如设置成1:3,这样就可以很好的解决主从不压力均衡的问题!通过研究确实可以!
配置就是将上面的读的池的配置更改一下:
将<property name="poolNames">server1,server2</property>更改成
<property name="poolNames">server1,server2,server2,server2</property>
 
mysql> select * from sxit;
+------+------+
| id   | name |
+------+------+
|    2 | lisi |
+------+------+
1 row in set (0.00 sec)
 
mysql> select * from sxit;
+------+------+
| id   | name |
+------+------+
|    2 | lisi |
+------+------+
1 row in set (0.01 sec)
 
mysql> select * from sxit;
+------+------+
| id   | name |
+------+------+
|    2 | lisi |
+------+------+
1 row in set (0.00 sec)
 
mysql> select * from sxit;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
|    3 | wangmazi |
+------+----------+
2 rows in set (0.00 sec)
 
//从以上的查询来看,select确实是安装1:3的比例来进行查询的.
 
关于amoeba的其他功能大家可以到官方上去看看,里面有关于垂直和水平切割方面的知识,相关信息如下:
 
开发者博客链接: http://amoeba.sourceforge.net/wordpress/
amoeba 中文文档下载地址:http://amoeba.meidusa.com/amoeba.pdf