Mysql 实战之——读写分离方案
Linux环境:Centos 6.8 64-bitMysql 版本:5.1.7 一、准备工作:部署Mysql主从复制二、使用Amoeba数据库代理来实现读写分离Amoeba作为数据库代理,以中间件的形式存在,拓扑图如下所示:(Amoeba官网的一张图)1、下载Amoeba:https://sourceforge.net/projects/amoeba/fi
Linux环境:Centos 6.8 64-bit
Mysql 版本:5.1.7
一、准备工作:部署Mysql主从复制
二、使用Amoeba数据库代理来实现读写分离
Amoeba作为数据库代理,以中间件的形式存在,拓扑图如下所示:(Amoeba官网的一张图)
1、下载Amoeba:https://sourceforge.net/projects/amoeba/files/
2、解压并复制到local目录
[root@ linux ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip
[root@ linux ~]# cp -rf amoeba-mysql-3.0.5-RC /usr/local
3、启动Amoeba。
[root@chenllcentos ~]# /usr/local/amoeba-mysql-3.0.5-RC/bin/launcher
如果出现fatal exception:
The stack size specified is too small, Specify at least 228k
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
原因是由于stack size太小,导致JVM启动失败,要如何修改呢?
解决:
[root@ linux ~]# vi /usr/local/amoeba-mysql-3.0.5-RC/jvm.properties
将内容:
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"
替换为:
JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"
4、修改配置文件:Amoeba完成读写分离的实现主要是依靠2个非常重要的配置文件:
dbServers.xml 和 amoeba.xml
4.1、首先,看看dbServers.xml文件
[root@localhost conf]# cat dbServer.xml
这个文件主要是配置数据库链接,主库,还有从库(slave)节点的信息,以及数据库连接池信息
如下内容就是这个文件的配置
<?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节点abstractive="true",包含Mysql的公共配置信息,其他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 -->
<!-- Mysql默认端口 -->
<property name="port">3306</property>
<!-- mysql schema -->
<!-- 默认连接的数据库,若不存在需要事先创建,否则Amoeba启动报错 -->
<property name="schema">testdb</property>
<!-- mysql user -->
<property name="user">root</property>
<property name="password">222</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>
<!-- Mysql主服务器master节点继承abstractServer -->
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- master数据库主机地址 -->
<property name="ipAddress">192.168.48.133</property>
</factoryConfig>
</dbServer>
<!-- Mysql从服务器slave节点继承abstractServer -->
<dbServer name="slave1" parent="abstractServer">
<factoryConfig>
<!-- slave数据库主机地址 -->
<property name="ipAddress">192.168.48.132</property>
</factoryConfig>
</dbServer>
<!-- 如果有多个从服务器节点, 可以复制多个dbServer -->
<!-- 配置数据库读取连接池 Amoeba实现读写分离根据readPool 连接池来实现-->
<dbServer name="readPool" 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 -->
<property name="poolNames">slave1</property>
</poolConfig>
</dbServer>
配置完成之后:
4.2、接着配置amoeba.xml
vi 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">127.0.0.1</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">root</property>
<property name="password"></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">multiPool</property>
<!--
<property name="writePool">server1</property>
<property name="readPool">server1</property>
-->
<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>
我们需要改的有3个地方:
5、启动
cd /usr/local/amoeba-mysql-3.0.5-RC/bin/
./launcher
出现如下信息就是启动成功了
2016-10-11 23:20:55 [INFO] Project Name=Amoeba-MySQL, PID=4097 , System shutdown ....
2016-10-11 23:27:49 [INFO] Project Name=Amoeba-MySQL, PID=4162 , starting...
log4j:WARN log4j config load completed from file:/usr/local/amoeba-mysql-3.0.5-RC/conf/log4j.xml
2016-10-11 23:27:50,736 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA
log4j:WARN ip access config load completed from file:/usr/local/amoeba-mysql-3.0.5-RC/conf/access_list.conf
2016-10-11 23:27:51,863 INFO net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:8066.
6、测试
先停止从库的主从复制
mysql>slave stop;
查询主库的数据
mysql> select * from t_test;
+------+------+
| id | name |
+------+------+
| 3 | t3 |
+------+------+
查询从库的数据
mysql> select * from t_test;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
+------+------+
登录Amoeba
mysql -uroot -p111111 -h192.168.48.133 -P8066
先使用查询语句
-- 经过多次测试查询,始终返回从库的数据
mysql> select * from t_test ;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
+------+------+
1 row in set (0.00 sec)
再使用insert语句
mysql> insert into t_test (id,name) values (100,'100');
Query OK, 1 row affected (0.02 sec)
mysql> insert into t_test (id,name) values (200,'200');
Query OK, 1 row affected (0.00 sec)
现在看看insert语句的结果:
-- 查询从库的结果:
mysql> select * from t_test;
+------+------+
| id | name |
+------+------+
| 100 | 100 |
| 200 | 200 |
+------+------+
-- 查询主库的结果
mysql> select * from t_test;
+------+------+
| id | name |
+------+------+
| 200 | 200 |
| 100 | 100 |
+------+------+
2 rows in set (0.00 sec)
--在Amoeba执行查询的结果:
mysql> select * from t_test ;
+------+------+
| id | name |
+------+------+
| 100 | 100 |
| 200 | 200 |
+------+------+
2 rows in set (0.07 sec)
已经成功了。。。查询操作,始终查询的是从库的数据,而insert操作,始终是对主库进行操作
JAVA程序中整合amoeba的话,只需要把数据库链接改成amoeba的就行了。。
在企业的实际项目中,这样做的好处是,不需要对代码进行硬编码,具备非常高的解耦作用,而且,Amoeba的配置也非常的简单,其实还有分库和分表的操作,这里就不做演示
三、报错信息
amoeba里查询的时候报错:
amoeba ERROR 1044 (42000): Could not create a validated object, cause: ValidateObject failed
这个问题,我在网上找到了一个原因,就是,amoeba去从库查询的时候,账号没权限。grant 赋予从库那个账号所有权限就好了。
更多推荐
所有评论(0)