搭建Amoeba读写分离和测试v1.0

 

1.          首先部署两台mysql ,建立测试库和表,并建立起主从复制

#yum 安装mysql

yum –y install mysql

 

#设置/etc/my.cnf

[client]

port            = 3306

socket          = /var/lib/mysql/mysql.sock

[mysqld]

port            = 3306

socket          = /var/lib/mysql/mysql.sock

datadir         = /data

skip-locking

key_buffer = 16M

max_allowed_packet = 1M

table_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

skip-federated

log-bin=mysql-bin

server-id       = 1      #master id=1 ;slave id =2

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

[isamchk]

key_buffer = 20M

sort_buffer_size = 20M

read_buffer = 2M

write_buffer = 2M

[myisamchk]

key_buffer = 20M

sort_buffer_size = 20M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

 

#建立测试库dbtest

create database dbtest;

#建立测试表tb1

use dbtest;

 

CREATE TABLE `tb1` (

  `id` int(11) NOT NULL auto_increment,

  `name` varchar(32) default NULL,

   PRIMARY KEY  (`id`)

);

 

#设置响应的复制权限

GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'rep'@'%' IDENTIFIED BY 'rep123';

FLUSH PRIVILEGES;

#建立主从复制关系

reset master;

show master status;

 

change master to

master_host='192.168.100.14',

master_port=3306,

master_user='rep',

master_password='rep123',

master_log_file='mysql-bin.000001',

master_log_pos=98;

 

slave start;

show slave status\G;

show full processlists;

#master插入数据查看binlog是否同步

insert into tb1(name) values ('123');

 

#建立amoeba使用的帐号并设置权限

grant all on dbtest.* to webuser@'%' identified by 'webuser123';

grant all on dbtest.* to webuser@'cloud1' identified by 'webuser123';

grant all on dbtest.* to webuser@'cloud2' identified by 'webuser123';

grant all on dbtest.* to webuser@'cloud3' identified by 'webuser123';

FLUSH PRIVILEGES;

 

2.          设置amoeba所需要的JDK环境

#下载安装jdk1.7

http://www.oracle.com/technetwork/java/javase/downloads/index.html

tar zxvf jdk-7u10-linux-i586.gz

#/etc/profile增加环境变量

pathmunge /opt/jdk1.7.0_10/bin

export JAVA_HOME=/opt/jdk1.7.0_10

export JRE_HOME=/opt/jdk1.7.0_10/jre

export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

#验证java

java -version

 

3.          下载安装amoeba

#下载Amoeba

http://sourceforge.net/projects/amoeba/files/

tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C aomeba-mysql/

 

#修改配置文件,指定访问mysqlport schema;访问mysqluserpassword; mysqlmaster slave ip

cd /opt/amoeba-mysql/conf

vi dbServers.xml

 

                        

                        3306

 

                       

                        dbtest

 

                       

                        webuser

 

                        

                        webuser123

     ……

master"  parent="abstractServer">

               

                      

                       192.168.100.14

               

       

 

   slave1"  parent="abstractServer">

               

                     

                     192.168.100.16</property>

               

       

multiPool" virtual="true">

               

       

                        1

 

          

                        slave1</property>

               

       

 

#修改配置文件,指定对外的用户名密码,以及读写分离的策略

vi amoeba.xml

 

webuser

    webuser123

….

    1500

    master

    master

    multiPool

        true

 

#启动amoeba

/opt/amoeba-mysql/bin/amoeba start &>/dev/null &

#关闭 amoeba

/opt/amoeba-mysql/bin/amoeba stop

 

4.          利用php进行读写分离测试

#写入脚本 insert_dbtest.php

$mysqli=mysqli_connect("192.168.100.13","webuser","webuser123","dbtest","8066"); 

class runtime 

    var $StartTime = 0; 

    var $StopTime = 0; 

 

    function get_microtime() 

    { 

        list($usec, $sec) = explode(' ', microtime()); 

        return ((float)$usec + (float)$sec); 

    } 

 

    function start() 

    { 

        $this->StartTime = $this->get_microtime(); 

    } 

 

    function stop() 

    { 

        $this->StopTime = $this->get_microtime(); 

    } 

 

    function spent() 

    { 

        return round(($this->StopTime - $this->StartTime) * 1000, 1); 

    } 

 

$runtime= new runtime; 

$runtime->start(); 

 

for($i=1;$i<10;$i++){ 

$ins = "insert into `tb1`(`name`) values ('$i')"; 

mysqli_query($mysqli,$ins); 

$runtime->stop(); 

echo "insert sucess! 页面执行时间: ".$runtime->spent()." 毫秒\n"; 

mysqli_close($mysqli); 

?> 

 

 

#读脚本select_dbtest.php

$mysqli=mysqli_connect("192.168.100.13","webuser","webuser123","dbtest","8066"); 

class runtime 

    var $StartTime = 0; 

    var $StopTime = 0; 

 

    function get_microtime() 

    { 

        list($usec, $sec) = explode(' ', microtime()); 

        return ((float)$usec + (float)$sec); 

    } 

 

    function start() 

    { 

        $this->StartTime = $this->get_microtime(); 

    } 

 

    function stop() 

    { 

        $this->StopTime = $this->get_microtime(); 

    } 

 

    function spent() 

    { 

        return round(($this->StopTime - $this->StartTime) * 1000, 1); 

    } 

 

$runtime= new runtime; 

$runtime->start(); 

 

$ins = "select * from tb1;"; 

$res = mysqli_query($mysqli,$ins); 

$rows = mysqli_num_rows($res);

echo $rows;

 

$runtime->stop(); 

//echo "insert sucess! 页面执行时间: ".$runtime->spent()." 毫秒\n"; 

mysqli_close($mysqli); 

?> 

 

 

5.         利用tcpdump进行抓包分析 读写是否分离到masterslave

#mysql mastersalve上分别进行抓包,192.168.100.13amoeba server

tcpdump -i any -s 0 -A -n -p port 3306 and src 192.168.100.13|grep -i 'SELECT\|INSERT'

 

#运行insert脚本

php insert_dbtest.php

 

#看到master上有数据产生

[root@cloud3 ~]# tcpdump -i any -s 0 -A -n -p port 3306 and src 192.168.100.13|grep -i 'SELECT\|INSERT'

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes

.m..$\=:'....insert into `tb1`(`name`) values ('1')erisign-grs.com.w...

'....insert into `tb

'....insert into `tb1`(`name`) values ('2')ervers.net..nstld.ve

.m..$].1'....insert into `tb1`(`name`) values ('3')..O...l.G.a...d{}..Q

.m..$]..'....insert into `tb1`(`name`) values ('4')ervers.net..nstld.ve

.m..$]..'....insert into `tb1`(`name`) values ('5')ervers.net..nstld.ve

.m.&$]..'....insert into `tb1`(`name`) values ('6')ervers.net..nstld.ve

.m..$].!'....insert into `tb1`(`name`) values ('7')..O...l.G.a...d{}..Q

 

#运行select脚本

php select_dbtest.php

 

#看到slave上有数据产生

[root@cloud2 scripts]# tcpdump -i any -s 0 -A -n -p port 3306 and src 192.168.100.13|grep -i 'SELECT\|INSERT' 

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes

.o9.$\.).....select * from tb1;.X7.:`@...>._..b..

.o9.$`._.....select * from

.ow.$`._.....select * from tb1;.X7.:`@...>._..b..

.ow.$a.!.....select * from

.p=^$a.!!....select @@version_comment limit 1.b....TRIBES-E..e,

.p=_$a..!....select @@vers

.p{h$b.......select * from tb1;.............webus

.p{i$b.......select * from tb1;

 

#就此最简单的读写分离测试成功