大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

MyCat分库分表中怎么实现ER分片

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

成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的阳城网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

项目环境: 

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) http://wrapper.tanukisoftware.org
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://dzwzjz.com/article/pieijg.html
在线咨询
服务热线
服务热线:028-86922220
TOP