首页 > 代码库 > Amoeba实现Mysql读写分离

Amoeba实现Mysql读写分离

环境规划


Amoeba读写分离器172.16.254.144/16
Mysql服务器(master)172.16.254.150/16
Mysql服务器(slave)172.16.254.151/16
一、先实现mysql主从复制,在master上使用yum安装mysql
[root@localhost mysql]# yum -y install mysql mysql-server

   启用二进制日志,编辑/etc/my.cnf在[mysqld]下添加如下行:

log-bin = /var/lib/mysql/mysql-bin-log
binlog_format=mixed
server_id = 1

   重启mysqld,连接mysql,授权远程复制账户

mysql> grant replication slave, replication client on *.* to ‘repluser‘@‘172.16.%.%‘ identified by ‘mypass‘;

  查看当前正在使用的二进制日志文件及位置

wKiom1N4T2qTvFgAAAC9ZbNV220309.jpg


 在slave上启用中继日志及修改server_id,编辑/etc/my.cnf在[mysqld]下添加如下行:

relay-log = /var/lib/mysql/relay-bin
server_id = 11

 连接master服务器,并启动复制功能

mysql> change master to master_host=‘172.16.254.150‘, master_user=‘repluser‘,master_password=‘mypass‘,master_log_file=‘mysql-bin-log.000002‘,master_log_pos=106;
mysql> start slave;

 查看Slave_IO、Slave_SQL线程是否为yes

wKiom1N4U9ST-sXEAAFDaZxtNZI113.jpg  


二、安装配置Amoeba

   安装配置jdk

[root@localhost ~]chmod +x ./jdk-6u31-linux-x64-rpm.bin
[root@localhost ~]./jdk-6u31-linux-x64-rpm.bin
[root@localhost ~]# vim /etc/profile.d/java.sh  添加如下行:
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
[root@localhost ~]# . /etc/profile.d/java.sh


   安装Amoeba

[root@localhost ~]# mkdir /usr/local/amoeba
[root@localhost ~]# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
[root@localhost ~]# cd /usr/local/amoeba/
[root@localhost ~]# bin/amoeba start

   安装mysql客户端,验证是否能够连接

[root@localhost ~]# yum -y install mysql
[root@localhost ~]# mysql -uroot -h172.16.254.144 -P8066 -p

  编辑/usr/local/amoeba/conf/amoeba.xml主配置文件

wKiom1N4a-KBKSehAAEzMmDHLbU238.jpg

wKiom1N4bEvRtdzFAAGTS-f8yzk155.jpg

wKioL1N4bVKhjebrAADctn-g_Lc769.jpg

   编辑/usr/local/amoeba/conf/dbServers.xml配置文件,定义master和readservers

wKiom1N4buOj2bwgAAInLGnWEx8390.jpg

wKioL1N4b3_iZs-1AAL-yLqXxhg352.jpg


   启动amoeba使其运行在后台,连接amoeba,使用tcpdump在master、slave服务器上抓包,创建一个数据库,并多使用几次查询语句,验证写操作是否被调度至master(172.16.254.150),而读操作会以轮叫的方式调度至master和slave,可以分摊mysql的读请求

[root@localhost mysql]# tcpdump -i eth0 -nn -XX ip dst host 172.16.254.150 and tcp dst port 3306
[root@localhost mysql]# tcpdump -i eth0 -nn -XX ip dst host 172.16.254.151 and tcp dst port 3306


本文出自 “Dream come true” 博客,请务必保留此出处http://lyd0721.blog.51cto.com/4894702/1413191