mysql中间件

Wesley13
• 阅读 567

中间件:一种提供在不同技术、不同的软件之间共享资源的程序,更大化了利用了数据库的性能,可以无限扩展(注:真实环境中并非如此)

数据库的中间件:

  mysql proxy (官方版本)性能低,需要lua 脚本

  atlas 性能低,响应时间长

  amoeba 陈思儒研发

一、 先搭建一个主从关系的服务器

在主、从服务器上安装mysql mysql-server

1. 开启二进制日志

[root@localhost ~]# vim /etc/my.cnf
...
log-bin=mysql-bin
server-id = 132
...

[root@localhost ~]# vim /etc/my.cnf
...l
og-bin=mysql-bin
server-id = 134 
...

2. 在主服务器上授权,从服务器上保存授权信息,并开启从服务线程

mysql> grant replication slave on *.* to root@'192.168.80.134' identified by '123456';

mysql> show master status;

mysql> change master to
    -> master_user='root',
    -> master_password='123456',
    -> master_host='192.168.80.132',
    -> master_log_file='mysql-bin.000008',
    -> master_log_pos=260;
Query OK, 0 rows affected (0.35 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;

mysql中间件

二、配置读写分离

1. 安装 gcc 环境

[root@localhost ~]# yum -y install gcc*

2. 安装jdk

前往官网下载:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

[root@localhost ~]# tar -zxvf jdk-8u231-linux-x64.tar.gz /amoeba/
[root@localhost /amoeba]# ln -s jdk1.8.0_231/ jdk
[root@localhost /amoeba]# vim /etc/profile

JAVA_HOME=/amoeba/jdk
export JAVA_HOME

PATH=$JAVA_HOME/bin:$PATH
export PATH


[root@localhost /amoeba]# source /etc/profile

3. 安装amoeba

[root@localhost ~]# wget https://jaist.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip
[root@localhost ~]# mkdir /amoeba
[root@localhost ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /amoeba

4. 配置amoeba

amoeba 的配置文件位于安装目录下的conf目录中,实现读写分离功能,只需修改两个配置文件,dbServers.xml和amoeba.xml。

[root@localhost /amoeba/amoeba-mysql-3.0.5-RC/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">root</property>  #登录使用的用户名
            
            <property name="password">123456</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="server1"  parent="abstractServer">    #改名字可任意命名,后续有用
        <factoryConfig>
            <!-- mysql ip -->
            <property name="ipAddress">192.168.80.132</property>  #数据库ip
        </factoryConfig>
    </dbServer>
    
    <dbServer name="server2"  parent="abstractServer">
        <factoryConfig>
            <!-- mysql ip -->
            <property name="ipAddress">192.168.80.134</property>  #数据库ip
        </factoryConfig>
    </dbServer>
    
    <dbServer name="write" virtual="true">    #定义一个dbserver 组名
        <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">server1</property>  #write组成员
        </poolConfig>
    </dbServer>
    <dbServer name="read" 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">server1,server2</property>
        </poolConfig>
    </dbServer>
        
</amoeba:dbServers>

[root@localhost /amoeba/amoeba-mysql-3.0.5-RC/conf]# cat 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>    #amoeba监听端口,默认8066
            
            <!-- bind ipAddress -->
            <property name="ipAddress">192.168.80.133</property>  #amoeba安装地址
            
            <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>  #设置客户端连接amoeba所需的用户名和密码
                    
                    <property name="password">123456</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">write</property>  #设置amoeba默认池
        <property name="writePool">write</property>  #设置amoeba 写池,默认注释
        <property name="readPool">read</property>   #设置amoeba读池,默认注释
        <property name="needParse">true</property>
    </queryRouter>
</amoeba:configuration>

5. 在主从服务器上授权

mysql>  grant all on *.* to 'root'@'192.168.80.133' identified by '123456';Query OK, 0 rows affected (0.00 sec)

6. 修改jvm.properties

[root@localhost /amoeba/amoeba-mysql-3.0.5-RC]# vim jvm.properties 

JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"

7. 启动

[root@localhost /amoeba/amoeba-mysql-3.0.5-RC]# nohup bash +x /amoeba/amoeba-mysql-3.0.5-RC/bin/launcher &

[root@localhost /amoeba/amoeba-mysql-3.0.5-RC]# tail -f nohup.out 
2019-12-24 18:34:02,897 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA
log4j:WARN ip access config load completed from file:/amoeba/amoeba-mysql-3.0.5-RC/conf/access_list.conf
2019-12-24 18:34:03,331 INFO  net.ServerableConnectionManager - Server listening on /192.168.80.133:8066.
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=16m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=96m; support was removed in 8.0
 2019-12-24 18:59:15 [INFO] Project Name=Amoeba-MySQL, PID=7607 , starting...
log4j:WARN log4j config load completed from file:/amoeba/amoeba-mysql-3.0.5-RC/conf/log4j.xml
2019-12-24 18:59:16,333 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA
log4j:WARN ip access config load completed from file:/amoeba/amoeba-mysql-3.0.5-RC/conf/access_list.conf
2019-12-24 18:59:16,669 INFO  net.ServerableConnectionManager - Server listening on /192.168.80.133:8066.

[root@localhost ~]# netstat -antp |grep 8066
tcp6       0      0 192.168.80.133:8066     :::*                    LISTEN      7607/java

[root@localhost ~]# firewall-cmd --zone=public --add-port=8066/tcp
[root@localhost ~]# firewall-cmd --zone=public --add-port=8066/tcp --permanent

8. 测试

[root@localhost ~]# mysql -uroot -p123456 -P8066 -h192.168.80.133
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 151281375
Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA Source distribution

Copyright (c) 2000, 2016, 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.

mysql> use test;
Database changed
mysql> select * from ceshi2;
+--------+
| name   |
+--------+
| master |
| amoeba |
+--------+
2 rows in set (0.01 sec)

mysql> select * from ceshi2;
+--------+
| name   |
+--------+
| master |
| slave  |
| amoeba |
+--------+
3 rows in set (0.01 sec)

mysql> select * from ceshi2;
+--------+
| name   |
+--------+
| master |
| amoeba |
+--------+
2 rows in set (0.01 sec)

mysql> select * from ceshi2;
+--------+
| name   |
+--------+
| master |
| slave  |
| amoeba |
+--------+
3 rows in set (0.01 sec)

mysql> select * from ceshi2;
+--------+
| name   |
+--------+
| master |
| amoeba |
+--------+
2 rows in set (0.01 sec)

mysql> select * from ceshi2;
+--------+
| name   |
+--------+
| master |
| slave  |
| amoeba |
+--------+
3 rows in set (0.02 sec)

三、amoeba配置文件

主配置文件 $AMOEBA_HOME/conf/amoeba.xml,用来配置Amoeba服务的基本参数,如Amoeba主机地址、端口、认证方式、用于连接的用户名、密码、线程数、超时时间、其他配置文件的位置等。

数据库服务配置文件 $AMOEBA_HOME/conf/dbServers.xml,用来存储和配置Amoeba所代理的数据库服务器的信息,如:主机IP、端口、用户名、密码等。

切分规则配置文件 $AMOEBA_HOME/conf/rule.xml,用来配置切分规则。

数据库函数配置文件 $AMOEBA_HOME/conf/functionMap.xml,用来配置数据库函数的处理方法,Amoeba将使用该配置文件中的方法解析数据库函数。

切分规则函数配置文件 $AMOEBA_HOME/conf/ruleFunctionMap.xml,用来配置切分规则中使用的用户自定义函数的处理方法。

访问规则配置文件 $AMOEBA_HOME/conf/access_list.conf,用来授权或禁止某些服务器IP访问Amoeba。

日志规格配置文件 $AMOEBA_HOME/conf/log4j.xml,用来配置Amoeba输出日志的级别和方式。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
3年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这