linux上使用amoeba实现MySql集群,以及读写分离,主从复制

时间:2022-09-20 15:04:19

  一、由于是MySql集群,所以就不可能只有一个MySql,需要多个MySql,具体安装步骤,可以参考http://www.cnblogs.com/ywzq/p/4882140.html这个地址进行安装配置和启动。

 

  二、现在MySql有了,开始安装Ameoba

 

    1.下载文件:amoeba-mysql-0.32.tar.gz

 

    2.创建文件amoeba

    mkdir amoeba

    cd amoeba

 

    3.解压文件(由于amoeba的作者打包时没太注意,所以你要先件个文件)

      tar amoeba-mysql-0.32.tar.gz

      cd conf/

        配置前端访问接口<server></server>如:     

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

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

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

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

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

              <property name="netBufferSize">200</property>

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

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

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

        配置监听:

            

            <connectionManagerList>
              <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.AuthingableConnectionManager"/>
            </connectionManagerList>

     搭建主从复制,读写分离的架构

        配置多个数据<dbServerList></dbServerList>

            配置Master(主)MySql数据库:     

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

              <property name="manager">defaultManager</property>

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

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

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

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

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

             </factoryConfig>

                <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
                <property name="maxActive">500</property>
                <property name="maxIdle">200</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>

            配置Slave(从>1)

            <dbServer name="slave1">
              <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
                <property name="manager">defaultManager</property>

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

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

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

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

              </factoryConfig>
                <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
                <property name="maxActive">500</property>
                <property name="maxIdle">200</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>

            配置Slave(从>1)

            <dbServer name="slave2">

              <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
                <property name="manager">defaultManager</property>

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

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

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

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

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

              </factoryConfig>
              <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
                <property name="maxActive">500</property>
                <property name="maxIdle">200</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>

            配置写入MySql

            <dbServer name="virtualSlave" virtual="true">
              <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
                <!-- 1=ROUNDROBIN , 2=WEIGHTBASED -->
                <property name="loadbalance">1</property>
                <property name="poolNames">slave1,slave2</property>
              </poolConfig>
            </dbServer>

 

            分配读写使用的数据库  

            <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
              <property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
              <property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
              <property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
              <property name="LRUMapSize">1500</property>
              <property name="defaultPool">master</property>
              <property name="writePool">master</property>
              <property name="readPool">virtualSlave</property>
              <property name="needParse">true</property>
            </queryRouter>

 

            修改权限文件

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

            <tableRule name="test_horiz" schema="test" defaultPools="master">

              <rule name="rule1">
                <parameters>ID</parameters>
                <expression><![CDATA[ ID <= 1000000]]></expression>
                <defaultPools>master</defaultPools>
                <readPools>slave1</readPools>
                <writePools>master</writePools>
              </rule>

              <rule name="rule2">    

                ==========

                <expression><![CDATA[ ID between 1000001 and 2000000 ]]></expression>

                <defaultPools>master</defaultPools>

                <writePools>master2</writePools>

                <readPools>slave2</readPools>

              </rule>

              <rule name="rule3">

                ==========

                <expression><![CDATA[ ID > 20000000 ]]></expression>

                <defaultPools>master</defaultPools>

              </rule>  

            </tableRule>

          

            <tableRule name="master" schema="test" defaultPools="master"/>
            <tableRule name="slave1" schema="test" defaultPools="slave1"/>
            <tableRule name="slave2" schema="test" defaultPools="slave1"/>
            <tableRule name="master" schema="t" defaultPools="master"/>

          </amoeba:rule>

  三.主从复制

    1、修改MySql配置文件my.cnf

      mast(主库)配置文件:

        server-id = 4308
        log-bin = mysql-bin
        log-bin-index = mysql-bin.index
        binlog-do-db= ng_base
        binlog-do-db= ng_push
        binlog-ignore-db = test
        binlog-ignore-db = mysql
        binlog-ignore-db = information_schema

      slave(从库1)配置文件:

        server-id = 4307

        log-bin = mysql-bin
        log-bin-index = mysql-bin.index
        relay-log = slave-relay-bin
        relay-log-index = slave-relay-bin.index
        sync_master_info = 1
        sync_relay_log = 1
        sync_relay_log_info = 1

  二、配置主从复制  

    1、主库操作

      授权给从数据库服务器192.168.10.131
      mysql> GRANT REPLICATION slave ON *.* to 'rep1'@'192.168.10.131' identified by ‘root4308’;

      查询主数据库状态
      Mysql> show master status;

        | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
        +------------------+----------+--------------+------------------+
        | mysql-bin.000005 | 261 | | |

 

    -- SLAVE STOP;

    2、 从库操作

    change master to
    master_host='101.201.140.214',
    master_user='slave',
    master_password='root4308',
    master_log_file='binlog.000005',
    master_log_pos=107;

    正确执行后启动Slave同步进程

    SLAVE START;

    可以使用show slave status\G检查