首页 > 数据库技术 > 详细

amoeba实现MySQL读写分离

时间:2015-11-04 22:41:25      阅读:588      评论:0      收藏:0      [点我收藏+]

amoeba实现MySQL读写分离

准备环境:主机A和主机B作主从配置,IP地址为192.168.131.129192.168.131.130,主机C作为中间件,也就是作为代理服务器,IP地址为192.168.131.136。三台服务器操作系统为RHEL6.4 x86_64,为最小化安装,

主从配置使用通用二进制包,版本为MySQL5.6.26。三台服务器均关闭防火墙和SELINUX

一、主机A和主机B做主从同步

1、安装MySQL

# tar xf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz

# useradd -M -s /sbin/nologin mysql

# yum -y install libaio-devel

# mv mysql-5.6.26-linux-glibc2.5-x86_64/ /usr/local/mysql

# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# vim /etc/my.cnf

[mysqld]

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

user=mysql

symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

# service mysqld start

# chkconfig mysqld on

# mysql -uroot password

# echo "PATH=${PATH}:/usr/local/mysql/bin" >> /etc/profile

# source /etc/profile

# ss -tunlp | grep 3306

2、配置主从同步

①主机A配置

# vim /etc/my.cnf

server-id=1

log-bin=mysql-bin

log-bin-index=mysql-bin.index

max-binlog-size=100M

# service mysqld restart

# mysql -u root –p

mysql> GRANT ALL PRIVILEGES ON *.* TO ‘wdd‘@‘192.168.131.%‘ IDENTIFIED BY ‘123456‘;

mysql> FLUSH PRIVILEGES;

mysql> SHOW MASTER STATUS;

技术分享

②主机B配置

# vim /etc/my.cnf

server-id=2

relay-log=relay-bin

relay-log-info-file=relay-log.info

# service mysqld restart

# mysql -u root –p

mysql> CHANGE MASTER TO

MASTER_HOST=‘192.168.131.129‘,

MASTER_USER=‘wdd‘,

MASTER_PASSWORD=‘123456‘,

MASTER_LOG_FILE=‘mysql-bin.000001‘,

MASTER_LOG_POS=405;

mysql> START SLAVE;

mysql> SHOW SLAVE STATUS\G;

技术分享

mysql> GRANT ALL PRIVILEGES ON *.* TO ‘wdd‘@‘192.168.131.%‘ IDENTIFIED BY ‘123456‘;

mysql> FLUSH PRIVILEGES;

二、主机C作为中间件

1、搭建amoeba运行环境

# mkdir /amoeba

# tar xf jdk-7u40-linux-x64.gz -C /amoeba/

# vim /etc/profile

JAVA_HOME=/amoeba/jdk1.7.0_40

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

# yum -y install java

# java –version

2、安装amoeba

# unzip amoeba-mysql-1.3.1-BETA.zip -d /usr/local/amoeba

# chmod -R +x /usr/local/amoeba/bin/

3、配置amoeba文件

# cd /usr/local/amoeba/conf/

# vim amoeba.xml

<?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">192.168.131.136</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">123456</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

平台已经提供一个具有负载均衡能力的objectPoolcom.meidusa.amoeba.mysql.server.MultipleServerPool

简单的配置是属性加上 virtual="true",Pool 不允许配置factoryConfig

或者自己写一个ObjectPool

-->

#更改的第二个区域《配置master端登录的信息》

<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">192.168.131.129</property>

<property name="schema">test</property>

<!-- 用于登陆mysql的用户名 -->

<property name="user">wdd</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>

#更改的第四个区域《配置masterslave负载均衡池(pool)

<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>

#更改的第三个区域《配置slave端登录的信息》

<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">192.168.131.130</property>

<property name="schema">test</property>

<!-- 用于登陆mysql的用户名 -->

<property name="user">wdd</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>

#更改的第五个区域《配置masterslave读写分离》

<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">server2</property>

注:上面可以写成<property name="poolNames">server2server2,server2,server1</property>,也就是说server1server2都加入读,但是server1server2=1:3,也就是读一次server1读三次server2

</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>

4、修改log4j.xml 取消日志文件生成(太大了,磁盘很容易满)

# vim log4j.xml

<param name="file" value="${amoeba.home}/logs/project.log"/>

改成

<param name="file" value="<![CDATA[${amoeba.home}/logs/project.log>/dev/null]]>"/>

5、修改amoeba启动脚本

# vim /usr/local/amoeba/bin/amoeba

DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k"

改为

DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss256k"

6、启动Amoeba脚本

# nohup bash -x /usr/local/amoeba/bin/amoeba & #放入后台运行

# cat nohup.out #查看日志看是否启动成功

# ps -ef | grep amoeba | grep -v grep #查看进程是否启动成功

技术分享

三、测试读写分离是否成功

1、主机C

# yum -y install mysql

# mysql -u root -p -P 8066 -h 192.168.131.136

技术分享

2、主机A主服务器操作

# mysql -u root –p

mysql> use test;

mysql> create table info(

id int(10) unsigned not null auto_increment primary key,

name char(20));

mysql> show tables;

技术分享

3、主机B从服务器操作

# mysql -u root –p

mysql> use test;

mysql> show tables;

技术分享

mysql> STOP SLAVE; #为了看到试验结果,停止从服务器

4、主机A操作

mysql> insert into info values (1,‘master‘);

5、主机B操作

mysql> insert into info values (2,‘slave‘);

6、主机C进行测试

mysql> use test; #进行读操作

mysql> select * from info;

技术分享

mysql> insert into info values (3,‘wdd‘);

7、主机A进行操作

mysql> select * from info; #进行写操作查询

技术分享

?

?

amoeba实现MySQL读写分离

原文:http://www.cnblogs.com/fengyutech/p/4937410.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!