mysql读写分离的实现

注意:本文只讲解mysql结合amoeba实现读写分离的具体配置,”主从复制”、“读写分离”、“二进制日志”、“中继日志”等概念性的东西不做描述;另外,本实验基于Cenots 6.7 系统。

  • 前提准备

    • 三台虚拟机:node1, node2, node3
      • 主机node1,ip地址为192.168.180.121,这台机器上需要安装mysql
      • 主机node2,ip地址为192.168.180.122,这台机器上需要安装amoeba(自行下载安装),不需要安装mysql
      • 主机node3,ip地址为192.168.180.123,这台机器山需要安装mysql
  • 第一步

    • 实现主从复制

      • 主机node1这台机器作为master,配置如下:

        • 在配置文件/etc/my.cnf中作如下配置(以追加形式,不要删除原来的配置,如果原来的配置文件已经存在对应配置,建议注释掉):
          log-bin=master-bin
          log-bin-index=master-bin.index(可选)
          server-id={0-2^32}(例如:server-id=1)
          (前2行在master机器上开启mysql的二进制日志;第三行为master选择一个在当前复制架构中惟一的server-id)
        • 创建一个具有复制权限的用户帐号(为了方便,直接用root账户)
          mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘root’@’192.168.180.121’ IDENTIFIED BY ‘123456’;
          mysql> FLUSH PRIVILEGES;
      • 主机node3这台机器作为slave,配置如下:

        • 在配置文件/etc/my.cnf中作如下配置(以追加形式,不要删除原来的配置,如果原来的配置文件已经存在对应配置,建议注释掉):
          • relay-log=relay-bin
            relay-log-index=relay-bin.index(可选)
            #log-bin=mysql-bin
            #binlog_format=mixed
            server-id={0-2^32}(例如server-id=2)
            (前2行开启slave这台机器的中继日志;接着2行关闭salve这台机器的二进制日志;最后一行为slave选择一个在当前复制架构中惟一的server-id,注意:这个server-id不能和master主机的server-id相同
        • 从服务器slave连接到主服务器master:
          • mysql> CHANGE MASTER TO MASTER_HOST=’192.168.180.121’, MASTER_USER=’root’, MASTER_PASSWORD=’123456’;
          • mysql> START SLAVE;
第一步实现简单的主从复制,可以在主服务器上create database mydb;在从服务器上show databases;进行查看验证
  • 第二步
    • 配置amoeba
      • 1、配置java环境变量(略)
      • 2、(可选)
        vim /etc/priflie.d/amoeba.d
        export AMOEBA_HOME=/usr/local/amoeba
        export PATH=$AMOEBA_HOME/bin:$PATH
        . /etc/profile.d/amoeba.d
      • 3、配置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 -->
                            <property name="schema">test</property>
                            <!-- mysql user -->
                            <property name="user">root</property>
                            <!-- mysql password <property name="password">password</property> -->
                            <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="testOnReturn">true</property>
                            <property name="testWhileIdle">true</property>

                        </poolConfig>

                    </dbServer>

                    <dbServer name="node1" parent="abstractServer"><!--局部定义,继承自全局-->
                        <factoryConfig>
                            <!-- mysql ip -->
                            <property name="ipAddress">192.168.180.121</property>
                        </factoryConfig>
                    </dbServer>

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

                    <dbServer name="readPool" virtual="true"><!--定义一个读pool-->
                        <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">node3,node3,node3,node1</property>
                        </poolConfig>
                    </dbServer>

                </amoeba:dbServers>
  • 4、配置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 -->
                        <!-- <property name="port">8066</property> -->
                        <property name="port">8066</property><!--端口Amoeba for Mysql listening on-->
                        <!-- bind ipAddress -->
                        <!-- <property name="ipAddress">127.0.0.1</property> -->
                        <property name="ipAddress">0.0.0.0</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><!--前端登陆amoeba的用户名称-->
                                <property name="password">amoebapass</property><!--前端登陆amoeba的用户密码-->
                                <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> -->
                        <!--Amoeba Monitor Server listening on 不指定就是随机的-->

                        <!-- 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">node1</property><!--默认pool-->
                    <!-- <property name="writePool">server1</property> <property name="readPool">server1</property> -->
                    <property name="writePool">node1</property><!--写pool,因为本次只有一个,所以直接写node1-->
                    <property name="readPool">readPool</property><!--读pool,见dbServers.xml-->
                    <property name="needParse">true</property>
                </queryRouter>
            </amoeba:configuration>
  • 第三步
    • 测试
      • 在任意主机上执行命令 tcpdump -i eth1 -nn -A tcp dst port 3306 and dst host 192.168.180.121 监控主服务器
      • 在任意主机上执行命令 tcpdump -i eth1 -nn -A tcp dst port 3306 and dst host 192.168.180.123 监控从服务器
      • 在任意主机上执行命令 mysql -uroot -h192.168.180.122 -P8066 -pamoebapass 通过amoeba代理登陆mysql
      • 执行sql语句,分别从2个tcpdump的输出中查看(可以验证写操作只在node1进行;读操作多数[约75%]在node3进行,跟dbServers.xml中readpool配置有关–node3配置了3个,node1配置了1个)

你可能感兴趣的:(mysql)