基于 Amoeba 实现 MySQL 读写分离

基于 Amoeba 实现 MySQL 读写分离

环境要求  

Master  Centos7.x    192.168.1.101     MySQL   

Slave1  Centos7.x    192.168.1.101     MySQL  

Slave2  Centos7.x    192.168.1.101      MySQL   

Slave1  Centos7.x    192.168.1.101    Amoeba-MySQL-binary    jdk1.8 

客户端   Centos7.x    192.168.1.101    MySQL—Client

一 、 部署 MySQL 主从复制

1 建立时间同步环境 在主节点部署时间同步服务器

   1、安装 NTP
       [root@localhost ~]# yum install ntp ntpdate -y
   2、 配置 NTP
      [root@localhost ~]#  vi /etc/ntp.conf 
      server 127.127.1.0
      fudge   127.127.1.0 stratum 8
   3 、重启 NTP 服务
     [root@localhost ~]#  systemctl restart  ntpd

2 在从节点进行时间同步

    [root@localhost ~]#  yum install ntpdate -y 
    [root@localhost ~]# /usr/sbin/ntpdate 192.168.1.101  
 
其他节点相同操作

3 在每台服务器关闭 firewalld 或者 iptables 防火墙

 [root@localhost ~]# systemctl stop firewalld
 [root@localhost ~]# systemctl disable firewalld
 centos7.x 以上版本操作

 [root@localhost ~]# service iptables stop
 [root@localhost ~]# chkconfig iptables off
 centos6.x 包括 centos5.x版本操作防火墙方法

4  安装 MySQL 数据库  在 Master Slave1 Slave2 节点

  安装 mysql 源

rpm -vih https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm 

安装 yum 扩展工具

yum install yum-utils  -y

启用mysql80-community 源  

启用mysql80-community 源  

yum-config-manager  --enable mysql57-community  

禁用mysql80-community 源   默认是启用的

yum-config-manager  --disable mysql80-community 

在 Master Slave1 Slave2 执行安装 Mysql 服务

yum install mysql-community-server  -y   

在客户端安装

yum install mysql-community-client  -y  

 配置主从 MySQL 同步

   配置 Master 主服务器

修改/etc/my.cnf 配置文件
[root@localhost ~]# vi /etc/my.cnf
server-id = 11
log-bin = master-bin
log-slave-updatees = true
重启 Master 节点服务

配置 Slave 从服务器

修改/etc/my.cnf 配置文件
[root@localhost ~]# vi /etc/my.cnf
server-id = 22   #多个从节点服务器 需要配置不同的 id 
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin 

连接Master

change master to master_host='192.168.0.104', //Master 服务器Ip
master_port=3306,
master_user='repl',
master_password='mysql', 
master_log_file='master-bin.000001',//Master服务器产生的日志
master_log_pos=0;

amoeba 下载地址

第一种方法
curl https://jaist.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip >amoeba-mysql-3.0.5-RC-distribution.zip 第二种方法 wget https://jaist.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip

配置 amoeba

[root@localhost conf]# 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">1234</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>   # 池  主节点  Master 数据库
         
        <!--
        <property name="writePool">server1</property>     # 池 主节点   写  Master 数据库
        <property name="readPool">server1</property>      # 池 从节点   读 Slaves  数据库
        -->     # 默认有注释  需要手动删除注释
        <property name="needParse">true</property>
    </queryRouter>
</amoeba:configuration>

编辑 dbServerers.xml 配置文件

[root@localhost conf]# cat 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="connectionManager">${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">test111</property>  # 用户名
            
            <property name="password">111111</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>

    <dbServer name="master"  parent="abstractServer">  # 写池  
        <factoryConfig>
            <!-- mysql ip -->
            <property name="ipAddress">127.0.0.1</property>
        </factoryConfig>
    </dbServer>
    
    <dbServer name="slave1"  parent="abstractServer">   # 读池1
        <factoryConfig>
            <!-- mysql ip -->
            <property name="ipAddress">127.0.0.1</property>
        </factoryConfig>
    </dbServer>

    <dbServer name="slave2"  parent="abstractServer">   # 读池2
        <factoryConfig>
            <!-- mysql ip -->
            <property name="ipAddress">127.0.0.1</property>
        </factoryConfig>
    </dbServer>
    
    <dbServer name="slaves" 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,slave2 </property>
        </poolConfig>
    </dbServer>
        
</amoeba:dbServers>

  温馨提示:红色表示需要修改的

 启动 amoeba 服务

 /usr/local/amoeba/bin/launcher

 

posted @ 2019-05-04 22:43  Boks  阅读(172)  评论(0)    收藏  举报