数据库读写分离

MySQL数据库读写分离

1、安装jdk
root@proxy:~# java -version
java version “1.6.0_45”
Java™ SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot™ 64-Bit Server VM (build 20.45-b01, mixed mode)

2、配置dbServer.xml
root@proxy:/usr/local/amoeba/conf# cat dbServers.xml

<?xml version="1.0" encoding="gbk"?>
        <!-- 
                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="manager">${defaultManager}</property>
                <property name="sendBufferSize">64</property>
                <property name="receiveBufferSize">128</property>

                <!-- mysql port -->
                <property name="port">3306</property>

                <!-- mysql schema -->
                <property name="schema">test</property>

                <!-- mysql user -->
                <property name="user">root</property>

                <!-- mysql password -->
                <property name="password">111</property>

        </factoryConfig>

        <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
                <property name="maxActive">500</property>
                <property name="maxIdle">500</property>
                <property name="minIdle">10</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 name="master1" parent="abstractServer">
        <factoryConfig>
                <!-- mysql ip -->
                <property name="ipAddress">10.10.10.40</property>
        </factoryConfig>
</dbServer>

<dbServer name="master2" parent="abstractServer">
        <factoryConfig>
                <!-- mysql ip -->
                <property name="ipAddress">10.10.10.50</property>
        </factoryConfig>
</dbServer>

<dbServer name="slave1" parent="abstractServer">
        <factoryConfig>
                <!-- mysql ip -->
                <property name="ipAddress">10.10.10.41</property>
        </factoryConfig>
</dbServer>

<dbServer name="slave2" parent="abstractServer">
        <factoryConfig>
                <!-- mysql ip -->
                <property name="ipAddress">10.10.10.42</property>
        </factoryConfig>
</dbServer>

rp池配置的是salve1和slave2节点



1

                <!-- Separated by commas,such as: server1,server2,server1 -->
                <property name="poolNames">slave1,slave2</property>
        </poolConfig>
</dbServer>

wp池配置的是master1和master2节点



1

                <!-- Separated by commas,such as: server1,server2,server1 -->
                <property name="poolNames">master1,master2</property>
        </poolConfig>
</dbServer>

3、配置amoeba.xml
root@proxy:/usr/local/amoeba/conf# cat amoeba.xml

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

        <!-- service class must implements com.meidusa.amoeba.service.Service -->
        <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
                <!-- port -->
                <property name="port">8066</property>

                <!-- bind ipAddress -->
                <!-- 
                <property name="ipAddress">127.0.0.1</property>
                 -->

                <property name="manager">${clientConnectioneManager}</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="authenticator">
                        <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

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

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

                                <property name="filter">
                                        <bean class="com.meidusa.amoeba.server.IPAccessController">
                                                <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
                                        </bean>
                                </property>
                        </bean>
                </property>

        </service>

        <!-- server class must implements com.meidusa.amoeba.service.Service -->
        <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">
                <!-- port -->
                <!-- default value: random number
                <property name="port">9066</property>
                -->
                <!-- bind ipAddress -->
                <property name="ipAddress">127.0.0.1</property>
                <property name="daemon">true</property>
                <property name="manager">${clientConnectioneManager}</property>
                <property name="connectionFactory">
                        <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>
                </property>

        </service>

        <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
                <!-- 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>

                <!-- per connection cache prepared statement size -->
                <property name="statementCacheSize">500</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="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
                <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>
                <!-- 
                  default value is avaliable Processors 
                <property name="processors">5</property>
                 -->
        </connectionManager>
        <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>

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

default池只配置了master1节点
${amoeba.home}/conf/functionMap.xml
1500
master1

        <property name="writePool">wp</property>
        <property name="readPool">rp</property>

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

4、启动amoeba
root@proxy:/usr/local/amoeba# bin/amoeba start &
[1] 2511
root@proxy:/usr/local/amoeba#
The stack size specified is too small, Specify at least 160k
Could not create the Java virtual machine.

[1]+ Exit 1 bin/amoeba start
更改bin/amoeba文件
DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k"
更改为
DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss256k"

启动正常之后,连接amoeba

root@proxy:~# mysql -uroot -p -h10.10.10.39 -P8066
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 307988519
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0

Copyright © 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.

java.util.NoSuchElementException: Could not create a validated object, cause: ValidateObject failed
root@10.10.10.39:(none) MySQL-> at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1191)

at com.meidusa.amoeba.net.poolable.GenericObjectPool.borrowObject(GenericObjectPool.java:381)
at com.meidusa.amoeba.mysql.handler.CommandMessageHandler.startSession(CommandMessageHandler.java:633)
at com.meidusa.amoeba.mysql.handler.MySqlCommandDispatcher.handleMessage(MySqlCommandDispatcher.java:123)
at com.meidusa.amoeba.mysql.net.MysqlClientConnection$2.run(MysqlClientConnection.java:291)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)

连接报错,查看root.log的输出
2015-08-29 16:01:03,299 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.40:3306]
2015-08-29 16:01:03,303 ERROR net.MysqlServerConnection - handShake with /10.10.10.40:3306 error:Access denied for user ‘root’@‘10.10.10.39’ (using password: NO),hashCode=661272757
2015-08-29 16:01:03,324 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.50:3306]
2015-08-29 16:01:03,327 ERROR net.MysqlServerConnection - handShake with /10.10.10.50:3306 error:Access denied for user ‘root’@‘10.10.10.39’ (using password: NO),hashCode=505588567
2015-08-29 16:01:03,331 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.41:3306]
2015-08-29 16:01:03,334 ERROR net.MysqlServerConnection - handShake with /10.10.10.41:3306 error:Access denied for user ‘root’@‘10.10.10.39’ (using password: NO),hashCode=530654357
2015-08-29 16:01:03,335 INFO net.BackendConnectionFactory - open socket channel to server[10.10.10.42:3306]
2015-08-29 16:01:03,338 ERROR net.MysqlServerConnection - handShake with /10.10.10.42:3306 error:Access denied for user ‘root’@‘10.10.10.39’ (using password: NO),hashCode=848649429

查看数据库连接权限

root@localhost:mysql MySQL-> select host,user from user;
host user
% rep_user
% root
127.0.0.1 root
::1 root
localhost
localhost root
ubuntu
ubuntu root
8 rows in set (0.00 sec)

数据库权限正常,使用客户端连接测试
root@proxy:~# mysql -uroot -p -h10.10.10.40
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 417
Server version: 5.6.19-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright © 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.

root@10.10.10.40:(none) MySQL-> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

root@10.10.10.40:test MySQL-> show tables;
Tables_in_test
t1
1 row in set (0.00 sec)

客户端连接也正常,猜想应该是amoeba配置有问题

                <!-- mysql password
                <property name="password">111</property>
                -->

发现密码被注释了,更改

                <!-- mysql password -->
                <property name="password">111</property>

启动一切正常

root@proxy:~# mysql -uroot -p -h10.10.10.39 -P8066

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1113757163
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright © 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

[root@10.10.10.39:(none)][12:06:15am] MySQL-> use test;
Database changed
[root@10.10.10.39:test][12:06:18am] MySQL->
[root@10.10.10.39:test][12:06:18am] MySQL-> show tables;
±---------------+
| Tables_in_test |
±---------------+
| t1 |
±---------------+
1 row in set (0.00 sec)

weixin073智慧旅游平台开发微信小程序+ssm后端毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值