​​​amoeba实现MySQL读写分离

​​​amoeba实现MySQL读写分离

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

Java环境下载地址(仅供参考)Java Archive Downloads - Java SE 6

amoeba 下载地址(仅供参考)amoeba - Browse Files at SourceForge.net

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

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

1、安装MySQL

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

# useradd -M -s /sbin/nologin mysql

# yum -y install libaio-devel

# mv mysql-5.7.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,

平台已经提供一个具有负载均衡能力的objectPool:com.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>

#更改的第四个区域《配置master和slave负载均衡池(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>

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

<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">server2,server2,server2,server1</property>,也就是说server1和server2都加入读,但是server1:server2=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; #进行写操作查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值