Mysql 基于 Amoeba 的 水平和垂直 分片(上)

2024-04-24 22:18

本文主要是介绍Mysql 基于 Amoeba 的 水平和垂直 分片(上),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 环境:

Servers

 

Amoeba Server (Linux): 192.168.14.129

Mysql 1 Server  (Linux): 192.168.14.131

Mysql 2 Server  (Linux): 192.168.14.133

 

Clients

 

Mysql GUI Tools (Windows): 192.168.14.28

Java Programs  (Eclipse): 192.168.14.28

 

假设以上程序都已经安装好了。

 

1. Mysql数据库远程访问授权

 

mysql 1 server 和 mysql 2 server 的 test 数据库,允许 amoeba server 访问。用户名:test_user 密码:1234

 

Sql代码   收藏代码
  1. grant all on test.* to test_user@192.168.14.129 identified by '1234';  
 

2.  创建测试表

 

在 mysql 1 server 中:

 

t_user:

Sql代码   收藏代码
  1. mysql> create table test.t_user (  
  2.     -> user_id integer unsigned not null,  
  3.     -> user_name varchar(45),  
  4.     -> user_address varchar(100),  
  5.     -> primary key (user_id)  
  6.     -> )engine=innodb;  
  7. Query OK, 0 rows affected (0.01 sec)  

 

t_blog:

Sql代码   收藏代码
  1. mysql> create table test.t_blog (  
  2.     -> blog_id integer unsigned not null,  
  3.     -> blog_title varchar(45),  
  4.     -> blog_content text,  
  5.     -> user_id integer,  
  6.     -> primary key (blog_id)  
  7.     -> )engine=innodb;  
  8. Query OK, 0 rows affected (0.00 sec)  
 

t_message:

Sql代码   收藏代码
  1. mysql> create table test.t_message (  
  2.     -> message_id integer unsigned not null,  
  3.     -> message_content varchar(255),  
  4.     -> user_id integer  
  5.     -> )engine=innodb;  
  6. Query OK, 0 rows affected (0.01 sec)  
 

在 mysql 2 server  中

 

t_user  同上。

 

t_attention:

Sql代码   收藏代码
  1. mysql> create table test.t_attention (  
  2.     -> attention_id integer unsigned not null,  
  3.     -> user_id integer,  
  4.     -> blog_id integer  
  5.     -> )engine=innodb;  
  6. Query OK, 0 rows affected (0.01 sec)  
 

t_blog_comment:

Sql代码   收藏代码
  1. mysql> create table test.t_blog_comment (  
  2.     -> comment_id integer unsigned not null,  
  3.     -> commnet_content text,  
  4.     -> blog_id integer  
  5.     -> )engine=innodb;  
  6. Query OK, 0 rows affected (0.01 sec)  
 

3. 配置 Amoeba 的切分数据库的规则

 

Amoeba 的详细使用说明请参见:http://docs.hexnova.com/amoeba/

 

dbServers.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="gbk"?>  
  2.   
  3. <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd">  
  4. <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">  
  5.   
  6.                 <!--  
  7.                         Each dbServer needs to be configured into a Pool,  
  8.                         If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration:  
  9.                          add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig  
  10.                          such as 'multiPool' dbServer  
  11.                 -->  
  12.   
  13.         <dbServer name="abstractServer" abstractive="true">  
  14.                 <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">  
  15.                         <property name="manager">${defaultManager}</property>  
  16.                         <property name="sendBufferSize">64</property>  
  17.                         <property name="receiveBufferSize">128</property>  
  18.   
  19.                         <!-- mysql port -->  
  20.                         <property name="port">3306</property>  
  21.   
  22.                         <!-- mysql schema -->  
  23.                         <property name="schema">test</property>  
  24.   
  25.                         <!-- mysql user -->  
  26.                         <property name="user">test_user</property>  
  27.   
  28.                         <!--  mysql password -->  
  29.                         <property name="password">1234</property>  
  30.   
  31.                 </factoryConfig>  
  32.   
  33.                 <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">  
  34.                         <property name="maxActive">500</property>  
  35.                         <property name="maxIdle">500</property>  
  36.                         <property name="minIdle">10</property>  
  37.                         <property name="minEvictableIdleTimeMillis">600000</property>  
  38.                         <property name="timeBetweenEvictionRunsMillis">600000</property>  
  39.                         <property name="testOnBorrow">true</property>  
  40.                         <property name="testWhileIdle">true</property>  
  41.                 </poolConfig>  
  42.         </dbServer>  
  43.   
  44.         <dbServer name="server1"  parent="abstractServer">  
  45.                 <factoryConfig>  
  46.                         <!-- mysql ip -->  
  47.                         <property name="ipAddress">192.168.14.131</property>  
  48.                 </factoryConfig>  
  49.         </dbServer>  
  50.   
  51.         <dbServer name="server2"  parent="abstractServer">  
  52.                 <factoryConfig>  
  53.                         <!-- mysql ip -->  
  54.                         <property name="ipAddress">192.168.14.133</property>  
  55.                 </factoryConfig>  
  56.         </dbServer>  
  57.   
  58. </amoeba:dbServers>  

 

amoeba.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="gbk"?>  
  2.   
  3. <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">  
  4. <amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">  
  5.   
  6.         <proxy>  
  7.   
  8.                 <!-- service class must implements com.meidusa.amoeba.service.Service -->  
  9.                 <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">  
  10.                         <!-- port -->  
  11.                         <property name="port">8066</property>  
  12.   
  13.                         <!-- bind ipAddress -->  
  14.                         <!-- 
  15.                         <property name="ipAddress">127.0.0.1</property> 
  16.                          -->  
  17.   
  18.                         <property name="manager">${clientConnectioneManager}</property>  
  19.   
  20.                         <property name="connectionFactory">  
  21.                                 <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">  
  22.                                         <property name="sendBufferSize">128</property>  
  23.                                         <property name="receiveBufferSize">64</property>  
  24.                                 </bean>  
  25.                         </property>  
  26.   
  27.                         <property name="authenticator">  
  28.                                 <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">  
  29.   
  30.                                         <property name="user">root</property>  
  31.   
  32.                                         <property name="password">root</property>  
  33.   
  34.                                         <property name="filter">  
  35.                                                 <bean class="com.meidusa.amoeba.server.IPAccessController">  
  36.                                                         <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>  
  37.                                                 </bean>  
  38.                                         </property>  
  39.                                 </bean>  
  40.                         </property>  
  41.   
  42.                 </service>  
  43.   
  44.                 <!-- server class must implements com.meidusa.amoeba.service.Service -->  
  45.                 <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">  
  46.                         <!-- port -->  
  47.                         <!--  default value: random number  
  48.                         <property name="port">9066</property>  
  49.                         -->  
  50.                         <!-- bind ipAddress -->  
  51.                         <property name="ipAddress">127.0.0.1</property>  
  52.                         <property name="daemon">true</property>  
  53.                         <property name="manager">${clientConnectioneManager}</property>  
  54.                         <property name="connectionFactory">  
  55.                                 <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>  
  56.                         </property>  
  57.   
  58.                 </service>  
  59.   
  60.                 <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">  
  61.                         <!-- proxy server net IO Read thread size -->  
  62.                         <property name="readThreadPoolSize">20</property>  
  63.   
  64.                         <!-- proxy server client process thread size -->  
  65.                         <property name="clientSideThreadPoolSize">30</property>  
  66.   
  67.                         <!-- mysql server data packet process thread size -->  
  68.                         <property name="serverSideThreadPoolSize">30</property>  
  69.   
  70.                         <!-- per connection cache prepared statement size  -->  
  71.                         <property name="statementCacheSize">500</property>  
  72.   
  73.                         <!-- query timeout( default: 60 second , TimeUnit:second) -->  
  74.                         <property name="queryTimeout">60</property>  
  75.                 </runtime>  
  76.   
  77.         </proxy>  
  78.         <!--  
  79.                 Each ConnectionManager will start as thread  
  80.                 manager responsible for the Connection IO read , Death Detection  
  81.         -->  
  82.         <connectionManagerList>  
  83.                 <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">  
  84.                         <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>  
  85.                         <!--  
  86.                           default value is avaliable Processors  
  87.                         <property name="processors">5</property>  
  88.                          -->  
  89.                 </connectionManager>  
  90.                 <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">  
  91.                         <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>  
  92.   
  93.                         <!--  
  94.                           default value is avaliable Processors  
  95.                         <property name="processors">5</property>  
  96.                          -->  
  97.                 </connectionManager>  
  98.         </connectionManagerList>  
  99.   
  100.                 <!-- default using file loader -->  
  101.         <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">  
  102.                 <property name="configFile">${amoeba.home}/conf/dbServers.xml</property>  
  103.         </dbServerLoader>  
  104.   
  105.         <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">  
  106.                 <property name="ruleLoader">  
  107.                         <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">  
  108.                                 <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>  
  109.                                 <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>  
  110.                         </bean>  
  111.                 </property>  
  112.                 <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>  
  113.                 <property name="LRUMapSize">1500</property>  
  114.                 <property name="defaultPool">server1</property><!-- 默认数据库,即主数据库 -->  
  115.   
  116.                 <!--  
  117.                 <property name="writePool">server1</property>  
  118.                 <property name="readPool">server1</property>  
  119.                 -->  
  120.                 <property name="needParse">true</property>  
  121.         </queryRouter>  
  122. </amoeba:configuration>  
 

rule.xml

 

t_user 表 根据 user_id 字段的奇偶性 水平切分 , 偶数分到 server1 , 奇数分到 server2。其中 server1,server2 是在 dbServers.xml 中定义的。

 

t_attention 表垂直切分到 server2

 

主数据库是 server1 , 在 amoeba.xml 中定义

Xml代码   收藏代码
  1. <property name="defaultPool">server1</property>  

 

主数据库说明:连接到 Amoeba 代理的时候,主数据库的所有表均可访问。 但是其他的数据库的表,需要在分片规则中有涉及的表,才能访问,其他表不能访问。

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="gbk"?>  
  2. <!DOCTYPE amoeba:rule SYSTEM "rule.dtd">  
  3.   
  4. <amoeba:rule xmlns:amoeba="http://amoeba.meidusa.com/">  
  5.         <tableRule name="t_user" schema="test" defaultPools="server1,server2">  
  6.                 <rule name="rule1">  
  7.                         <parameters>user_id</parameters>  
  8.                         <expression><![CDATA[ user_id % 2 == 0 ]]></expression>  
  9.                         <defaultPools>server1</defaultPools>  
  10.                 </rule>  
  11.                 <rule name="rule2">  
  12.                         <parameters>user_id</parameters>  
  13.                         <expression><![CDATA[ user_id % 2 == 1 ]]></expression>  
  14.                         <defaultPools>server2</defaultPools>  
  15.                 </rule>  
  16.         </tableRule>  
  17.         <tableRule name="t_attention" schema="test" defaultPools="server2" />  
  18.         <tableRule name="t_blog_comment" schema="test" defaultPools="server2" />  
  19.  </amoeba:rule>  

 

启动 msyql1, mysql2, amoeba 切分完成。

 

4. 测试

 

访问的时候,需要通过 Amoeba 代理来访问。不能直接访问实体数据库,不然切分无效。

 

使用 Mysql GUI Tools 连接 Amoeba

 

用户名:root   密码: root  端口号:8066

 

以上信息在 amoeba.xml 中定义

 

Xml代码   收藏代码
  1. <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">  
  2.                         <!-- port 连接端口号-->  
  3.                         <property name="port">8066</property>  
  4.   
  5.                         <!-- bind ipAddress -->  
  6.                         <!-- 
  7.                         <property name="ipAddress">127.0.0.1</property> 
  8.                          -->  
  9.   
  10.                         <property name="manager">${clientConnectioneManager}</property>  
  11.   
  12.                         <property name="connectionFactory">  
  13.                                 <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">  
  14.                                         <property name="sendBufferSize">128</property>  
  15.                                         <property name="receiveBufferSize">64</property>  
  16.                                 </bean>  
  17.                         </property>  
  18.   
  19.                         <property name="authenticator">  
  20.                                 <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">  
  21.                                         <!--用户名,密码 -->  
  22.                                         <property name="user">root</property>  
  23.   
  24.                                         <property name="password">root</property>  
  25.   
  26.                                         <property name="filter">  
  27.                                                 <bean class="com.meidusa.amoeba.server.IPAccessController">  
  28.                                                         <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>  
  29.                                                 </bean>  
  30.                                         </property>  
  31.                                 </bean>  
  32.                         </property>  
  33.   
  34.                 </service>  

 



 

 

可以发现,在GUI 工具中,能看到的表 只有在 server1 中,创建的表。 上面已经说明了,因为 server1 中主数据库。

 

验证垂直切分的 t_attention 表

 

为了验证,垂直切分出去的 t_attention 表。  直接执行 select * from test.t_attention;


 

可以看到,查询成功。 说明的确可以访问,切分出去的 t_attention 表。

 

验证水平切分的 t_user 表

 

t_user 的水平切分规则是 根据 user_id 的奇偶性进行切分。

 


上图的左下角可以看到:2 rows affected by the last command , 说明刚才那个插入语句,作用到了两条记录

 

查询看一下插入结果:


发现有两条一样的数据。到网上找了一下原因。

 

后来知道 amoeba 是根据 sql 解析来进行 水平切分的, 需要把切分的关键字段(这里是user_id),加入到 sql 中。否则 切分规则无效。无效后,会在 server1, server2 均都插入数据。

 

即变为:insert into t_user(user_id, user_name, user_address) values(1, 'n1', 'a1')

 

分别检查一下,两个数据库


 

两个数据库均插入了一条相同的数据。  同时也了解到,amoeba 的查询会将水平切分的表在两个数据库的结果合并。

 

修改 sql 语句后再执行一次。


可以看到左下角:只作用到一条记录

 

看下查询结果



 
 

 

因为 user_id 为 偶数, 所有分派到 server 1 了。

 

水平切分的排序与分页


排序结果:分别查询的 server1 和 server2 然后简单合并。 所以 排序不正确。


分页结果:同样是分别执行的 server1 和 server2 然后合并。 每个 server 取两条记录,记录也变为了 4 条。分页不正确

 

 

验证连接查询

 

在 t_message , t_blog, t_attention 表分别插入两个数据

分别执行以上查询语句。

 

发现只有 第二 和 第四 可以执行成功。

 

结果:

1. 如果 表1 被水平分片,连接查询时,需要连接的表,在 表1 分片的所有数据库中均存在(即相同的分片规则),才可以连接查询。

2. 如果 表1 被垂直分片,连接查询时,需要连接的表也同样分片到同一个数据库中(即相同的分片规则)。

3. 综上,只有在连接的表,在同一个数据库中均存在时,才能连接操作。即 不支持跨数据库的连接。

这篇关于Mysql 基于 Amoeba 的 水平和垂直 分片(上)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/932989

相关文章

SQL中的外键约束

外键约束用于表示两张表中的指标连接关系。外键约束的作用主要有以下三点: 1.确保子表中的某个字段(外键)只能引用父表中的有效记录2.主表中的列被删除时,子表中的关联列也会被删除3.主表中的列更新时,子表中的关联元素也会被更新 子表中的元素指向主表 以下是一个外键约束的实例展示

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

如何去写一手好SQL

MySQL性能 最大数据量 抛开数据量和并发数,谈性能都是耍流氓。MySQL没有限制单表最大记录数,它取决于操作系统对文件大小的限制。 《阿里巴巴Java开发手册》提出单表行数超过500万行或者单表容量超过2GB,才推荐分库分表。性能由综合因素决定,抛开业务复杂度,影响程度依次是硬件配置、MySQL配置、数据表设计、索引优化。500万这个值仅供参考,并非铁律。 博主曾经操作过超过4亿行数据

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

MySQL数据库宕机,启动不起来,教你一招搞定!

作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG、Mongodb数据库运维(如安装迁移,性能优化、故障应急处理等)公众号:老苏畅谈运维欢迎关注本人公众号,更多精彩与您分享。 MySQL数据库宕机,数据页损坏问题,启动不起来,该如何排查和解决,本文将为你说明具体的排查过程。 查看MySQL error日志 查看 MySQL error日志,排查哪个表(表空间

MySQL高性能优化规范

前言:      笔者最近上班途中突然想丰富下自己的数据库优化技能。于是在查阅了多篇文章后,总结出了这篇! 数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意,并且最后不要超过32个字符 临时库表必须以tmp_为前缀并以日期为后缀,备份

[MySQL表的增删改查-进阶]

🌈个人主页:努力学编程’ ⛅个人推荐: c语言从初阶到进阶 JavaEE详解 数据结构 ⚡学好数据结构,刷题刻不容缓:点击一起刷题 🌙心灵鸡汤:总有人要赢,为什么不能是我呢 💻💻💻数据库约束 🔭🔭🔭约束类型 not null: 指示某列不能存储 NULL 值unique: 保证某列的每行必须有唯一的值default: 规定没有给列赋值时的默认值.primary key:

MySQL-CRUD入门1

文章目录 认识配置文件client节点mysql节点mysqld节点 数据的添加(Create)添加一行数据添加多行数据两种添加数据的效率对比 数据的查询(Retrieve)全列查询指定列查询查询中带有表达式关于字面量关于as重命名 临时表引入distinct去重order by 排序关于NULL 认识配置文件 在我们的MySQL服务安装好了之后, 会有一个配置文件, 也就

Java 连接Sql sever 2008

Java 连接Sql sever 2008 /Sql sever 2008 R2 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class TestJDBC

Mysql BLOB类型介绍

BLOB类型的字段用于存储二进制数据 在MySQL中,BLOB类型,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储的大小不同。 TinyBlob 最大 255 Blob 最大 65K MediumBlob 最大 16M LongBlob 最大 4G