简介:

 Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy。它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行。基于此可以实现负载均衡、读写分离、高可用性等需求。


拓扑图:

mysql主从及amoeba读写分离_第1张图片

  


搭建mysql的主从复制环境:


ip地址为192.168.150.135作为MASTER

修改my.cnf
[root@centos-server ~]# vim/etc/my.cnf 
[mysqld]
port            = 3306
binlog-do-db=dragon    #同步的数据库
replicate-ignore-db=mysql    #不同步的数据库
replicate-ignore-db=information_schema
server-id= 1      #server-id要唯一
log-bin=mysql-bin
mysql> show variables like "server_id";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.01 sec)
mysql> create database dragon;    
Query OK, 1 row affected (0.08 sec)
mysql> grant replication slave on *.* to 'back'@'192.168.150.%' identified by '123456';
1 row in set (0.01 sec)        #添加用户"back",SLAVE用于登录本机复制数据库日志文件
mysql> show master status;    #查看MASTER上dragon库的日志的状态信息
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000025 |      560 | dragon       |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
exit
[root@centos-server ~]# cd /usr/local/mysql/var/
[root@centos-server var]# tar zcf dragon.tar.gz dragon/
scp dragon.tar.gz [email protected]:/usr/local/mysql/var/  #把数据库复制到SLAVE上


ip192.168.150.137做为SLAVE

修改my.cnf的server-id为2
[root@centos-server ~]# cd /usr/local/mysql/var/
[root@centos-server ~]#tar xf dragon.tar.gz    #先解压缩复制过来的数据库文件
[root@centos-server ~]#mysql -uroot -pqwe123
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.150.135',master_user='back',master_password='123456',master_log_file='mysql-bin.000025',master_log_pos=560;  #使用用户"back"复制MASTER的"mysql-bin.000025"日志     
Query OK, 0 rows affected (0.03 sec)
mysql> slave start;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
[root@centos-server ~]# service mysqld restart    #重启SLAVE的mysql
[root@drbd2 ~]# mysql -uroot -pqwe123
mysql> show slave status\G  #查看SLAVE是否正常同步,主要看"Slave_IO_Running"和"Slave_SQL_Running"是否为yes
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.150.135
                  Master_User: back
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000025
          Read_Master_Log_Pos: 560
               Relay_Log_File: centos-server-relay-bin.000004
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000025
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 560
              Relay_Log_Space: 414
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
1 row in set (0.00 sec)


部署amoeba:

amoeba是基于jdk环境运行的,所以要先安装jdk
[root@drbd1 ~]# mv jdk1.6.0_31/ /usr/local/
[root@drbd1 ~]# cat >/etc/profile.d/jdk.sh< 
  


配置amoeba:

主要通过"conf"目录下的dbServer.xml和amoeba.xml设置amoeba调度mysql请求:
[root@centos-server conf]#vim dbServer.xml 






${defaultManager}
64
128

3306    #连接mysql的默认端口

dragon    #设置amoeba默认连接的数据库名

amoba    #amoeba连接后端mysql的帐号和密码,后端mysql必须创建
qwe123


500    #设置最大连接数,默认是500
500    #配置最大空闲连接数
1    #配置最少空闲连接数
600000
600000
true
true
true


#定义一个dbserver,名字自定义,后面会用到


192.168.150.135    #dbserver的IP address




192.168.150.137


    #定义一个dbserver组


#调度算法:1.复制均衡,2.表示权重,3.表示HA。
2    

slave1  #设置dbserver成员,根据需要可加多个






[root@centos-server conf]#vim amoeba.xml 






8066    #amoeba负责接受请求侦听的端口



128
64




root    #客户机连接ameba使用的帐号和密码
qwe123


${amoeba.home}/conf/access_list.conf







128

500

utf8

60





com.meidusa.toolkit.net.AuthingableConnectionManager




${amoeba.home}/conf/dbServers.xml




${amoeba.home}/conf/rule.xml
${amoeba.home}/conf/ruleFunctionMap.xml


${amoeba.home}/conf/functionMap.xml
1500
writedb    
writedb    #在dbServer定义的 writedb响应写操作 
slave1    #在dbServer定义的 slave1响应读操作
true


在后端mysql添加amobe帐号:

mysql> grant ALL ON *.* TO 'amoba'@'192.168.150.128' identified by 'qwe123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


启动amoeba:

[root@centos-server ]# /usr/local/amoeba/bin/./launcer




测试读写:

[root@drbd2 conf]# mysql -uroot -pqwe123 -h192.168.150.128 -P8066

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 714931905

Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA Source distribution


Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

This software comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to modify and redistribute it under the GPL v2 license


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> 


mysql> use dragon;

Database changed

mysql> create table user(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

    -> name VARCHAR(20) NOT nULL,

    -> sex BOOLEAN);

Query OK, 0 rows affected (0.05 sec)


mysql> insert into user(name,sex) VALUES ('dragon',1);

Query OK, 1 row affected (0.05 sec)


mysql> insert into user(name,sex) VALUES ('dragon',1),('bbq',1);

Query OK, 2 rows affected (0.01 sec)

Records: 2  Duplicates: 0  Warnings: 0



mysql> select * from user;

+----+--------+------+

| ID | name   | sex  |

+----+--------+------+

|  1 | dragon |    1 |

|  2 | dragon |    1 |

|  3 | bbq    |    1 |

+----+--------+------+

3 rows in set (0.03 sec)