mysql数据库读写分离

 1. 读写分离

    读写分离的前提需要数据库已经做好了主从同步,如何实现主从同步,可以参考:https://www.cnblogs.com/alamisu/p/11840749.html

    为了实现读写分离,出现了很多解决方案,其中比较流行的是采用中间件做为Proxy,保持应用层代码不随数据库的变动而发生变化,这里包括Amoeba、Atlas、Cobar、Mycat、MySQL Proxy、MySQL Router等。

  

  • 需要注意Amoeba管理的数据库服务器要能被访问;默认mysql服务器只监听127.0.0.1,也就是说mysql监听的端口需要设置成你需要的值,并且用来做远程登陆的用户需要被赋予对应的权限,否则amoeba无法访问数据库。
  • 同时我们可以在数据库上创建不同用户,并赋予不同权限来使用数据库,实现读写分离。

 

2. 以Amoeba举例说明,主/从数据库服务器安装配置JDK环境,并且要求JDK至少1.7

下载安装JDK。需要主/从数据库服务器都安装配置JDK环境
# sudo wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"
# sudo mkdir /opt/JAVA
# sudo tar xf
jdk-8u141-linux-x64.tar.gz -C /opt/JAVA

设置profile环境变量,将以下内容添加到/etc/profile末尾:

JAVA_HOME=/opt/JAVA/jdk1.8.0_141
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

# sudo source /etc/profile
# java -version
以下回显则表示成功:
  

 

 

3. 安装Amoeba

# sudo wget http://nchc.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/2.x/amoeba-mysql-binary-2.1.0-RC5.tar.gz
# sudo mkdir /usr/local/amoeba
# sudo tar xf amoeba-mysql-binary-2.1.0-RC5.tar.gz -C /usr/local/amoeba
切换至root用户,方便操作
# sudo su
# cd /usr/local/amoeba/bin/
修改 DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k" ---> DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss256k" ,否则会启动失败,报错如下
# ./amoeba
# cd /usr/local/amoeba/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="manager">${defaultManager}</property>
                        <property name="sendBufferSize">64</property>
                        <property name="receiveBufferSize">128</property>

                        <!-- mysql port 数据库端口-->
                        <property name="port">3306</property>

                        <!-- mysql schema amoeba 需要访问的数据库表-->
                        <property name="schema">test</property>

                        <!-- mysql user 主从数据库分配给amoeba访问数据的用户-->
                        <property name="user">root</property>

                        <!--  mysql password 主从数据库分配给amoeba访问数据的密码-->
                        <property name="password">123456</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="testWhileIdle">true</property>
                </poolConfig>
        </dbServer>

        <dbServer name="master"  parent="abstractServer">
                <factoryConfig>
                        <!-- mysql ip 主数据库地址-->
                        <property name="ipAddress">10.100.0.249</property>
                </factoryConfig>
        </dbServer>

        <dbServer name="slave"  parent="abstractServer">
                <factoryConfig>
                        <!-- mysql ip 从数据库地址,如果有多个从数据库需要重复多个DBServer模块-->
                        <property name="ipAddress">10.100.0.147</property>
                </factoryConfig>
        </dbServer>

        <!-- 数据库池,虚拟服务器,实现读取的负载均衡,如果有多个Slave,则<property name="poolNames">slave1,slave2</property>用逗号隔开 -->
        <dbServer name="multiPool" 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">slave</property>
                </poolConfig>
        </dbServer>

</amoeba:dbServers>

 

修改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.net.ServerableConnectionManager">
                        <!-- port Amoeba 端口号 ,客户端client 链接amoeba端口号,不能和主从数据库冲突-->
                        <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">
                                        <!-- Amoeba 账号密码 ,客户端client 链接amoeba端 账号-->
                                        <property name="user">admin</property>

                                        <property name="password">123123</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>
                <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
                <property name="LRUMapSize">1500</property>
                <!-- 默认数据库,主数据库 -->
                <property name="defaultPool">master</property>

                <!-- 写数据库 -->
                <property name="writePool">master</property>
                <!-- 读数据库,dbServer.xml 中配置的虚拟数据库,数据库池 -->
                <property name="readPool">slave</property>

                <property name="needParse">true</property>
        </queryRouter>
</amoeba:configuration>

 

# cd /usr/local/amoeba/bin/
# ./amoeba start

 

4.然后就可以使用Amoeba客户端操作数据库了

使用Amoeba登陆数据库:
# mysql -h xxx.xxx.xxx.xxx -P 8066 -u admin -p # xxx.xxx.xxx.xxx 是amoeba服务端ip,8066是服务端口, admin是配置好的amoeba用户 最后再输入amoeba密码就可以登陆了
然后就可以按照数据库来执行操作了,Amoeba会根据配置好的信息来选择对应的数据库执行命令

 

posted @ 2019-11-13 19:12  阿拉米苏  阅读(232)  评论(0编辑  收藏  举报