Mysql读写分离-amoeba

Posted liang-yao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql读写分离-amoeba相关的知识,希望对你有一定的参考价值。

转载自http://www.cnblogs.com/liuyisai/p/6009379.html

 

Amoeba主配置文件($AMOEBA_HOME/conf/amoeba.xml),用来配置Amoeba服务的基本参数,如Amoeba主机地址、端口、认证方式、用于连接的用户名、密码、线程数、超时时间、其他配置文件的位置等。

数据库服务器配置文件($AMOEBA_HOME/conf/dbServers.xml),用来存储和配置Amoeba所代理的数据库服务器的信息,如:主机IP、端口、用户名、密码等。

切分规则配置文件($AMOEBA_HOME/conf/rule.xml),用来配置切分规则。

数据库函数配置文件($AMOEBA_HOME/conf/functionMap.xml),用来配置数据库函数的处理方法,Amoeba将使用该配置文件中的方法解析数据库函数。

切分规则函数配置文件($AMOEBA_HOME/conf/ruleFunctionMap.xml),用来配置切分规则中使用的用户自定义函数的处理方法。

访问规则配置文件($AMOEBA_HOME/conf/access_list.conf),用来授权或禁止某些服务器IP访问Amoeba。

日志规格配置文件($AMOEBA_HOME/conf/log4j.xml),用来配置Amoeba输出日志的级别和方式。

环境:

主:   SQL-Master      192.168.200.101/24

从:   SQL-Slave      192.168.200.102/24,192.168.200.103/24

mysql-amoeba 192.168.200.106/24

 

JAVA环境配置:http://liang-yao.cnblogs.com/p/8448739.html

 Amoeba下载地址:https://sourceforge.net/projects/amoeba/files/

Amoeba解压即安装

[[email protected] amoeba]# ll

总用量 8

drwxrwxrwx 2 root root   63 7月   5 2013 benchmark

drwxrwxrwx 2 root root  166 7月   5 2013 bin

drwxrwxrwx 2 root root  243 7月   5 2013 conf

-rwxrwxrwx 1 root root  728 7月   5 2013 jvm.properties

drwxrwxrwx 2 root root 4096 7月   5 2013 lib

[[email protected] amoeba]# pwd

/usr/local/amoeba

配置读写分离仅需配置dbServers.xml和amoeba.xml

vim conf/dbServers.xml

<?xml version="1.0" encoding="gbk"?>

 

<!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">

<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">

 

<!--

Each dbServer needs to be configured into a Pool,

If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:

add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig

such as ‘multiPool‘ dbServer

-->

 

<dbServer name="abstractServer" abstractive="true">

<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">

<property name="connectionManager">${defaultManager}</property>

<property name="sendBufferSize">64</property>

<property name="receiveBufferSize">128</property>

 

<!-- mysql port -->

<property name="port">3306</property>

#Amoeba要连接的mysql数据库端口

<!-- mysql schema -->

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

#设置缺省的数据库,当连接amoeba时,操作表必须显式的指定数据库名,即采用dbname.tablename的方式,不支持 use dbname指定缺省库,因为操作会调度到各个后端dbserver

<!-- mysql user -->

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

#设置amoeba连接后端数据库服务器的账号和密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接

<property name="password">123456</property>

</factoryConfig>

 

<poolConfig class="com.meidusa.toolkit.common.poolable.PoolableObjectPool">

<property name="maxActive">500</property>  #最大连接数

<property name="maxIdle">500</property>#最大空闲连接数

<property name="minIdle">1</property>#最小空闲连接

<property name="minEvictableIdleTimeMillis">600000</property>

<property name="timeBetweenEvictionRunsMillis">600000</property>

<property name="testOnBorrow">true</property>

<property name="testOnReturn">true</property>

<property name="testWhileIdle">true</property>

</poolConfig>

</dbServer>

#定义一个后端可写的dbserver名称

<dbServer name="writedb"  parent="abstractServer">

<factoryConfig>

<!-- mysql ip -->

<property name="ipAddress">192.168.200.101</property>

</factoryConfig>#  后端可写dbserver

</dbServer>

#定义后端可读dbserver

<dbServer name="slave1"  parent="abstractServer">

<factoryConfig>

<!-- mysql ip -->

<property name="ipAddress">192.168.200.102</property>

</factoryConfig>

</dbServer>

 

<dbServer name="slave2"  parent="abstractServer">

<factoryConfig>

<!-- mysql ip -->

<property name="ipAddress">192.168.200.103</property>

</factoryConfig>

</dbServer>

 

#定义一个虚拟dbserver,将只读dbserver加入此组

<dbServer name="slaves" virtual="true">

<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">

<!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->

<property name="loadbalance">1</property>

 

<!-- Separated by commas,such as: server1,server2,server1 -->#调度算法,1表示复制均衡,2表示权重,3表示HA

<property name="poolNames">slave1,slave2</property>

</poolConfig>#slaves组成员

</dbServer>

 

</amoeba:dbServers>

 

 

vim conf/amoeba.xml

<?xml version="1.0" encoding="gbk"?>

 

<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">

<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">

 

<proxy>

 

<!-- service class must implements com.meidusa.amoeba.service.Service -->

<service name="Amoeba for Mysql" class="com.meidusa.amoeba.mysql.server.MySQLService">

<!-- port -->

<property name="port">8066</property>

#监听端口

<!-- bind ipAddress -->

<property name="ipAddress">192.168.200.106</property>

#监听地址

<property name="connectionFactory">

<bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">

<property name="sendBufferSize">128</property>

<property name="receiveBufferSize">64</property>

</bean>

</property>

 

<property name="authenticateProvider">

<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

 

<property name="user">user123</property>

#客户端连接amoeba所需账号密码

<property name="password">123456</property>

 

<property name="filter">

<bean class="com.meidusa.toolkit.net.authenticate.server.IPAccessController">

<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>

</bean>

</property>

</bean>

</property>

 

</service>

 

<runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">

 

<!-- proxy server client process thread size -->

<property name="executeThreadSize">128</property>

 

<!-- per connection cache prepared statement size  -->

<property name="statementCacheSize">500</property>

 

<!-- default charset -->

<property name="serverCharset">utf8</property>

 

<!-- query timeout( default: 60 second , TimeUnit:second) -->

<property name="queryTimeout">60</property>

</runtime>

 

</proxy>

 

<!--

Each ConnectionManager will start as thread

manager responsible for the Connection IO read , Death Detection

-->

<connectionManagerList>

<connectionManager name="defaultManager" class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper">

<property name="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</property>

</connectionManager>

</connectionManagerList>

 

<!-- default using file loader -->

<dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">

<property name="configFile">${amoeba.home}/conf/dbServers.xml</property>

</dbServerLoader>

 

<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">

<property name="ruleLoader">

<bean class="com.meidusa.amoeba.route.TableRuleFileLoader">

<property name="ruleFile">${amoeba.home}/conf/rule.xml</property>

<property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>

</bean>

</property>

<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>

<property name="LRUMapSize">1500</property>

<property name="defaultPool">writedb</property>

#amoeba默认池

<property name="writePool">writedb</property>#取消注释,定义读写池

<property name="readPool">slaves</property>

<property name="needParse">true</property>

</queryRouter>

</amoeba:configuration>

 

vim jvm.properties

32 JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"      #Xss256K 小于256K无法正常启动

 

 

主从数据库配置

mysql -u root -p123456

创建用户test设置密码123456

MariaDB [(none)]> grant all on *.* to [email protected]‘127.0.0.1‘ identified by "hello";

MariaDB [(none)]> use mysql;

修改host权限

MariaDB [(none)]> update user set host = ‘%‘ where user = ‘test‘;

MariaDB [mysql]> select user, host from user;

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

| user | host          |

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

| test | %             |

刷新数据库

MariaDB [mysql]> flush privileges;

 

启动:

chmod 777 logs

./bin/launcher

 

ss -ntlu

 

 

测试

mysql -uuser123 -p123456 -h192.168.200.106 -P8066

断开mysql-master网络,发现不能写入,仅能查询

MySQL  [test]> use test;

技术分享图片

MySQL [test]> select * from test2

技术分享图片

断开mysql-slave1、2网络,发现不能查询,仅能写入

技术分享图片

技术分享图片

 

以上是关于Mysql读写分离-amoeba的主要内容,如果未能解决你的问题,请参考以下文章

mysql读写分离之amoeba

mysql读写分离之amoeba

mysql读写分离之amoeba

使用Amoeba实现mysql读写分离机制

MySQL数据库--主从复制读写分离

mysql基础之七-;mysql读写分离之amoeba