网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

MyCat分库分表中怎么实现ER分片-创新互联

这篇文章主要介绍“MyCat分库分表中怎么实现ER分片”,在日常操作中,相信很多人在MyCat分库分表中怎么实现ER分片问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MyCat分库分表中怎么实现ER分片”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

目前成都创新互联公司已为近1000家的企业提供了网站建设、域名、虚拟主机成都网站托管、企业网站设计、徐闻网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

项目环境:

192.168.8.30  mycat

192.168.8.31  node1

192.168.8.32  node2

192.168.8.33  node3

三个节点MySQL均为单实例

一、配置schema.xml




         
           
           
        
	
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
	
                select user()
                
                
        
        
                select user()
                
                
        
        
                select user()
                
                
        

二、配置rule.xml

     
        
                
                        order_id
                        mod-long
                
        
       
        
                9
        
 

三、配置server.xml


		mysql
		mycatdb

四、创建测试库

node1

create database testdb01;
create database testdb02;
create database testdb03;

node2

create database testdb13;
create database testdb14;
create database testdb15;

node3 

create database testdb25;
create database testdb26;
create database testdb27;

五、启动mycat

/usr/local/mycat/bin/mycat start

查看mycat日志

STATUS | wrapper  | 2018/11/22 14:12:55 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2018/11/22 14:12:55 | Launching a JVM...
INFO   | jvm 1    | 2018/11/22 14:12:55 | OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO   | jvm 1    | 2018/11/22 14:12:58 | Wrapper (Version 3.2.3) /tupian/20230522/home.html
INFO   | jvm 1    | 2018/11/22 14:12:58 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2018/11/22 14:12:58 | 
INFO   | jvm 1    | 2018/11/22 14:13:04 | MyCAT Server startup successfully. see logs in logs/mycat.log

六、登录MySQL,查看逻辑表

mysql -uroot -pmysql -P8066 -h292.168.8.30
mysql> show databases;
+----------+
| DATABASE |
+----------+
| mycatdb  |
+----------+
1 row in set (0.02 sec)
mysql> use mycatdb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables in mycatdb |
+-------------------+
| order01           |
| orderdetail01     |
| user01            |
| user02            |
+-------------------+
4 rows in set (0.00 sec)
mysql> select * from order01;
ERROR 1105 (HY000): Table 'testdb03.order01' doesn't exist
mysql> 
mysql> drop table if exists order01;
Query OK, 0 rows affected, 1 warning (0.46 sec)
mysql> drop table if exists orderdetail01;
Query OK, 0 rows affected, 1 warning (0.16 sec)
mysql> CREATE TABLE order01(
    -> order_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> number VARCHAR(64),
    -> createtime DATETIME);
Query OK, 0 rows affected (0.58 sec)
mysql> CREATE TABLE orderdetail01(
    -> orderdetail_id INT AUTO_INCREMENT PRIMARY KEY,
    -> order_id INT,
    -> order_status CHAR(1),
    -> addresss VARCHAR(128),
    -> createtime DATETIME,
    -> CONSTRAINT fk_iorder01 FOREIGN KEY (order_id) REFERENCES order01
    -> (order_id));
Query OK, 0 rows affected (0.63 sec)

七、插入测试数据

父表和子表各插入9条记录

INSERT INTO order01(order_id,number,createtime) VALUES(1,'steven101',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (1,1,'1','steven101(ID=1,steven101)',NOW());
INSERT INTO order01(order_id,number,createtime) VALUES(2,'steven201',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (2,2,'1','steven201(ID=2,steven201)',NOW());
INSERT INTO order01(order_id,number,createtime) VALUES(3,'steven301',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (3,3,'1','steven301(ID=3,steven301)',NOW());
INSERT INTO order01(order_id,number,createtime) VALUES(4,'steven401',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (4,4,'1','steven401(ID=4,steven401)',NOW());
INSERT INTO order01(order_id,number,createtime) VALUES(5,'steven501',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (5,5,'1','steven501(ID=5,steven501)',NOW());
INSERT INTO order01(order_id,number,createtime) VALUES(6,'steven601',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (6,6,'1','steven601(ID=6,steven601)',NOW());
INSERT INTO order01(order_id,number,createtime) VALUES(7,'steven701',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (7,7,'1','steven701(ID=7,steven701)',NOW());
INSERT INTO order01(order_id,number,createtime) VALUES(8,'steven801',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (8,8,'1','steven801(ID=8,steven801)',NOW());
INSERT INTO order01(order_id,number,createtime) VALUES(9,'steven901',NOW());
INSERT INTO orderdetail01(orderdetail_id,order_id,order_status,addresss,createtime) VALUES (9,9,'1','steven901(ID=9,steven901)',NOW());

八、验证数据

schema.xml中,order01和orderdetail01分片只配置了testdb01-03,testdb13-15,testdb25-27,所以除了这9个物理库之外,其他库查不到分片。

下面在三个node分别验证出分片信息:

node1

mysql> select * from testdb01.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        9 | steven901 | 2018-11-22 14:21:20 |
+----------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb01.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              9 |        9 | 1            | steven901(ID=9,steven901) | 2018-11-22 14:21:23 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.01 sec)
mysql> select * from testdb02.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        1 | steven101 | 2018-11-22 14:21:18 |
+----------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb02.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              1 |        1 | 1            | steven101(ID=1,steven101) | 2018-11-22 14:21:18 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.01 sec)
mysql> select * from testdb03.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        2 | steven201 | 2018-11-22 14:21:18 |
+----------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb03.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              2 |        2 | 1            | steven201(ID=2,steven201) | 2018-11-22 14:21:18 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.00 sec)

node2

mysql> select * from testdb13.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        3 | steven301 | 2018-11-22 14:21:17 |
+----------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb13.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              3 |        3 | 1            | steven301(ID=3,steven301) | 2018-11-22 14:21:17 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.01 sec)
mysql> select * from testdb14.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        4 | steven401 | 2018-11-22 14:21:17 |
+----------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb14.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              4 |        4 | 1            | steven401(ID=4,steven401) | 2018-11-22 14:21:17 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb15.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        5 | steven501 | 2018-11-22 14:21:18 |
+----------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb15.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              5 |        5 | 1            | steven501(ID=5,steven501) | 2018-11-22 14:21:18 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.00 sec)

node3

mysql> select * from testdb25.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        6 | steven601 | 2018-11-22 14:21:20 |
+----------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb25.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              6 |        6 | 1            | steven601(ID=6,steven601) | 2018-11-22 14:21:20 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb26.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        7 | steven701 | 2018-11-22 14:21:20 |
+----------+-----------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb26.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              7 |        7 | 1            | steven701(ID=7,steven701) | 2018-11-22 14:21:20 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb27.order01;
+----------+-----------+---------------------+
| order_id | number    | createtime          |
+----------+-----------+---------------------+
|        8 | steven801 | 2018-11-22 14:21:20 |
+----------+-----------+---------------------+
1 row in set (0.01 sec)
mysql> select * from testdb27.orderdetail01;
+----------------+----------+--------------+---------------------------+---------------------+
| orderdetail_id | order_id | order_status | addresss                  | createtime          |
+----------------+----------+--------------+---------------------------+---------------------+
|              8 |        8 | 1            | steven801(ID=8,steven801) | 2018-11-22 14:21:20 |
+----------------+----------+--------------+---------------------------+---------------------+
1 row in set (0.00 sec)

到此,关于“MyCat分库分表中怎么实现ER分片”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联-成都网站建设公司网站,小编会继续努力为大家带来更多实用的文章!


当前文章:MyCat分库分表中怎么实现ER分片-创新互联
转载源于:http://bjjierui.cn/article/csdecd.html

其他资讯