Mysql(集群)业务水平切割 垂直切割(Amoeba)
Amoeba原理戳这里:Amoeba详细介绍
需要根据企业 数据业务进行切割,垂直切割又称为纵向切割。
垂直切割通说的说就是有多个表,对表进行分离(用户数据、博客文章数据、照片数据、标签数据类型、群组数据)
水平切割通俗的说就是一张表的数据太多,对表内数据进行分离
以下是一个水平切割的实例
安装JDK、Amoeba和配置文件参考前面写过的基于Amoeba的读写分离 http://www.cnblogs.com/losbyday/p/5818994.html
注释或者删除两行就OK
<!-- 读写分离在这里 --> <!-- <property name="writePool">master</property> <property name="readPool">slave</property> -->
配置水平分割规则
# vim /usr/local/amoeba/conf/rule.xml
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:rule SYSTEM "rule.dtd"> <amoeba:rule xmlns:amoeba="http://amoeba.meidusa.com/"> <!-- 表名 操作的那个数据库 那个数据库池 --> <tableRule name="los" schema="test" defaultPools="server1,server2">
<rule name="rule1"> <parameters>ID</parameters> <!-- 写入数据库的规则 --> <expression><![CDATA[ user_id between 1 and 50000 and user_id % 2 =0 ]]></expression> <defaultPools>server1</defaultPools> <readPools>server1</readPools> <writePools>server1</writePools> </rule> <rule name="rule2"> <parameters>ID</parameters> <expression><![CDATA[ user_id between 1 and 50000 and user_id % 2 =0 ]]></expression> <defaultPools>server2</defaultPools> <writePools>server2</writePools> <readPools>server2</readPools> </rule> <!-- <rule name="rule4"> <parameters>ID</parameters> <expression><![CDATA[ ID > 20000000 ]]></expression> <defaultPools>server3</defaultPools> </rule> --> <!-- <rule name="rule3"> <parameters>ID,CREATE_TIME</parameters> <expression><![CDATA[ID>4 or CREATE_TIME between to_date('2008-11-12 00:00:00.0000') and to_date('2008-12-10 00:00:00.0000') ]]></expression> <defaultPools>server3</defaultPools> </rule> --> </tableRule> </amoeba:rule>
插入测试数据
在Amoeba Server登录:
# mysql -uroot -p123456 -h 192.168.100.214 -P 9006
mysql->use test;
mysql->insert into los(user_id,user_name,user_address)values('1','user1','China');
mysql->insert into los(user_id,user_name,user_address)values('2','user1','China');
mysql->insert into los(user_id,user_name,user_address)values('3','user1','China');
mysql->insert into los(user_id,user_name,user_address)values('4','user1','China');
mysql->insert into los(user_id,user_name,user_address)values('5','user1','China');
mysql->insert into los(user_id,user_name,user_address)values('6','user1','China');
mysql->insert into los(user_id,user_name,user_address)values('7','user1','China');
mysql->insert into los( user_id,user_name,user_address)values('8','user1','China');
mysql->insert into los(user_id,user_name,user_address)values('9','user1','China');
mysql->insert into los(user_id,user_name,user_address)values('10','user1','China');
分别登录master和slave数据库进行查看
总结:
(1)、amoeba 是根据 sql 解析来进行 水平切分的, 需要把切分的关键字段(这里是user_id),加入到insert sql 语句 中。否则 切分规则无效。无效后,会在 server1, server2 均都插入数据。
(2)、amoeba插入数据的时候必须先use dbname(比如要先use test库名,或者查询带上库名和表名,test.t_user) 否则插入数据会默认插到server1上面
(3)、在rule.xml 指定的ID范围,在插入数据user_id的时候。不能超过这个范围,否则分片无效,
比如定义的ID范围为:
1-500000号
insert into zyalvin(user_id,user_name,user_address)values('5000001','user1','China'); 如果插入ID超过5000000,变成了5000001的话那么这个数据将同时写到server1和server2 导致分片无效。
有点儿事,回头修正