文章目录

  • MyCAT分库分表
    • 一、分库分表简介
    • 二、垂直切分---分库
      • 1、思想
      • 2、配置实现
    • 三、水平切分

MyCAT分库分表

一、分库分表简介

  • 在业务数据量过多的时候,而且数据不断持续增长的情况下,为了缓解单台服务器的压力,可以考虑使用分库或者分表方案

二、垂直切分—分库

1、思想

  • 一个数据库由很多的表构成,每个表对应这不同的业务,垂直切分是按照业务将表进行手动分类,利用MyCAT中间件进行管理,将同一个数据库中的多个表分开放在多个数据库中

2、配置实现

  • 环境描述

192.168.152.10 MySQL服务器

192.168.152.11 MySQL服务器

192.168.152.12 MyCAT中间键

192.168.152.13 测试客户端

  • 分别在两台MySQL上创建测试的库、表

    • 相同的库,不同的表
  • 在数据库db01上安装mariadb-server,创建数据库,account表,为mycat创建连接用户

#第一个数据库
[root@db01 ~]# yum -y install mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@db01 ~]# mysql -urootMariaDB [(none)]> create database game charset utf8;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> use game;
Database changed
MariaDB [game]> create table Account(-> id int primary key not null auto_increment,-> name char(30));
Query OK, 0 rows affected (0.01 sec)MariaDB [game]> insert into Account(name) values("ljh"),("yxj");
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0MariaDB [game]> select * from Account;
+----+------+
| id | name |
+----+------+
|  1 | ljh  |
|  2 | yxj  |MariaDB [game]> grant all on game.*  to 'mycatuser'@'192.168.152.12' identified by "redhat";
Query OK, 0 rows affected (0.00 sec)MariaDB [game]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • 在数据库db02上安装mariadb-server,创建数据库,area表,为mycat创建连接用户
#第二个数据库
[root@db02 ~]# yum -y install mariadb-server
[root@db02 ~]# systemctl start mariadb
[root@db02 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@db02 ~]# mysql -uroot
MariaDB [(none)]> create database game charset utf8-> ;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> use game;
Database changed
MariaDB [game]> create table area(-> aid int not null primary key auto_increment,-> aname char(40));
Query OK, 0 rows affected (0.00 sec)MariaDB [game]> insert into area(aname)values("北方大区"),("南方大区");
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0MariaDB [game]> select * from area;
+-----+--------------+
| aid | aname        |
+-----+--------------+
|   1 | 北方大区     |
|   2 | 南方大区     |
+-----+--------------+MariaDB [game]> grant all on game.*  to 'mycatuser'@'192.168.152.12' identified by "redhat";
Query OK, 0 rows affected (0.00 sec)MariaDB [game]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • 安装MyCAT,配置java环境变量,修改配置文件
[root@mycat ~]# tar -xf jdk-8u91-linux-x64.tar.gz -C /usr/local/^C
[root@mycat ~]# tar -xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat ~]# tail -2 /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export PATH=$PATH:$JAVA_HOME/bin[root@mycat ~]# vim /usr/local/mycat/conf/schema.xml<schema name="game" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn01"><table name="Account" primaryKey="id" type="global" dataNode="dn01" /><table name="area" primaryKey="aid" type="global" dataNode="dn02" /></schema><dataNode name="dn01" dataHost="localhost1" database="game" /><dataNode name="dn02" dataHost="localhost2" database="game" /><dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100"><heartbeat>select user()</heartbeat><!-- can have multi write hosts --><writeHost host="hostM1" url="192.168.152.10:3306" user="mycatuser"password="redhat"><!-- can have multi read hosts --></writeHost></dataHost><dataHost name="localhost2" maxCon="1000" minCon="10" balance="0"writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100"><heartbeat>select user()</heartbeat><!-- can have multi write hosts --><writeHost host="hostM1" url="192.168.152.11:3306" user="mycatuser"password="redhat"><!-- can have multi read hosts --></writeHost></dataHost>[root@mycat ~]# tail -6 /usr/local/mycat/conf/server.xml<user name="ljh"><property name="password">redhat</property><property name="schemas">game</property></user></mycat:server>
[root@mycat ~]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
Mycat-server is already running.
[root@mycat ~]# netstat -tunlp | grep java
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      12735/java
tcp6       0      0 :::1984                 :::*                    LISTEN      12735/java
tcp6       0      0 :::8066                 :::*                    LISTEN      12735/java
tcp6       0      0 :::9066                 :::*                    LISTEN      12735/java
tcp6       0      0 :::45934                :::*                    LISTEN      12735/java
tcp6       0      0 :::45428                :::*                    LISTEN      12735/java
[root@mycat ~]# 
  • 客户端连接MyCAT进行测试
MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| game     |
+----------+
1 row in set (0.01 sec)MySQL [(none)]> use game;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MySQL [game]> show tables;
+----------------+
| Tables_in_game |
+----------------+
| account        |
| area           |
+----------------+
2 rows in set (0.00 sec)MySQL [game]> insert into account(name)values("jjj"),("ddd");
ERROR 1146 (42S02): Table 'game.account' doesn't exist
MySQL [game]> insert into Account(name)values("jjj"),("ddd");
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0MySQL [game]> select * from Account;
+----+------+
| id | name |
+----+------+
|  1 | ljh  |
|  2 | yxj  |
|  3 | jjj  |
|  4 | ddd  |
+----+------+
4 rows in set (0.00 sec)MySQL [game]> select * from area;
+-----+--------------+
| aid | aname        |
+-----+--------------+
|   1 | 北方大区     |
|   2 | 南方大区     |
|   3 | jjj          |
|   4 | ddd          |
+-----+--------------+
4 rows in set (0.01 sec)MySQL [game]> 

三、水平切分

  • 将上午写的schema.xml server.xml还原
[root@mycat conf]# cp schema.xml.bak schema.xml
cp: overwrite ‘schema.xml’? yes
[root@mycat conf]# cp server.xml.bak server.xml
cp: overwrite ‘server.xml’? yes
  • 先选择对应的规则和函数
<mycat:rule xmlns:mycat="http://io.mycat/"><tableRule name="splitHWrule"><rule><columns>id</columns><algorithm>func1</algorithm></rule></tableRule><function name="func1" class="io.mycat.route.function.PartitionByLong"><property name="partitionCount">2</property><property name="partitionLength">512</property></function>
</mycat:rule>
  • 最后修改这两个配置文件
[root@mycat conf]# vim schema.xml<schema name="phone" checkSQLschema="false" sqlMaxLimit="100"><!-- auto sharding by id (long) --><table name="hw" dataNode="dn01,dn02" rule="splitHWrule" /></schema><dataNode name="dn01" dataHost="localhost1" database="phone" /><dataNode name="dn02" dataHost="localhost2" database="phone" /><dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100"><heartbeat>select user()</heartbeat><!-- can have multi write hosts --><writeHost host="hostM1" url="192.168.152.10:3306" user="mycatuser"password="redhat"><!-- can have multi read hosts --></writeHost></dataHost><dataHost name="localhost2" maxCon="1000" minCon="10" balance="0"writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100"><heartbeat>select user()</heartbeat><!-- can have multi write hosts --><writeHost host="hostM1" url="192.168.152.11:3306" user="mycatuser"password="redhat"><!-- can have multi read hosts --></writeHost></dataHost>
[root@mycat conf]# cat server.xml<user name="ljh"><property name="password">redhat</property><property name="schemas">phone</property></user>
  • 在db01上创建相同数据库,相同的表,为mycat授权
MariaDB [(none)]> create database phone charset utf8;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> use phone;
Database changed
MariaDB [phone]> create table hw(-> id int primary key not null auto_increment,-> name char(30));
Query OK, 0 rows affected (0.00 sec)MariaDB [phone]> grant all on phone.* to 'mycatuser'@'192.168.152.12' identified by "redhat";
Query OK, 0 rows affected (0.00 sec)MariaDB [phone]> flush privileges;
Query OK, 0 rows affected (0.00 sec)MariaDB [phone]> show tables;
+-----------------+
| Tables_in_phone |
+-----------------+
| hw              |
+-----------------+
  • 在db02上创建相同数据库,相同的表,为mycat授权
MariaDB [(none)]> create database phone charset utf8;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> use database phone;
ERROR 1049 (42000): Unknown database 'database'
MariaDB [(none)]> use phone;
Database changed
MariaDB [phone]> create table hw(-> id int primary key not null auto_increment,-> name char(30));
Query OK, 0 rows affected (0.00 sec)MariaDB [phone]> grant all on phone.* to 'mycatuser'@'192.168.152.12' identified by "redhat";
Query OK, 0 rows affected (0.00 sec)MariaDB [phone]> flush privileges;
Query OK, 0 rows affected (0.00 sec)MariaDB [phone]> select * from hw;
+------+------+
| id   | name |
+------+------+
|  513 | hzy  |
| 1021 | zyz  |
+------+------+
  • 用测试机连接创建不同的ID,来测试规则
[root@localhost ~]# mysql -uljh -predhat -h 192.168.152.12 -P8066
MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| phone    |
+----------+
MySQL [phone]> insert into hw(id,name)values(10,"ljh"),(511,"yxj"),(513,"hzy"),(1021,"zyz");
Query OK, 4 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
MySQL [phone]> select * from hw;
+------+------+
| id   | name |
+------+------+
|   10 | ljh  |
|  511 | yxj  |
|  513 | hzy  |
| 1021 | zyz  |
+------+------+#在db01上查看
MariaDB [phone]> select * from hw;
+-----+------+
| id  | name |
+-----+------+
|  10 | ljh  |
| 511 | yxj  |
+-----+------+
2 rows in set (0.00 sec)
#在db02上查看
MariaDB [phone]> select * from hw;
+------+------+
| id   | name |
+------+------+
|  513 | hzy  |
| 1021 | zyz  |
+------+------+

利用MyCAT分库分表相关推荐

  1. 利用Mycat分库分表操作

    为什么分库分表 1 什么是分库分表? 其实就是字面意思,很好理解: 分库:从单个数据库拆分成多个数据库的过程,将数据散落在多个数据库中. 分表:从单张表拆分成多张表的过程,将数据散落在多张表内. 2 ...

  2. mysql 配置文件在哪_MySQL+MyCat分库分表 读写分离配置

    一. MySQL+MyCat分库分表 1 MyCat简介 java编写的数据库中间件 Mycat运行环境需要JDK. Mycat是中间件,运行在代码应用和MySQL数据库之间的应用. 前身: corb ...

  3. docker二进制安装mysql_Docker搭建MySQL读写分离主从模式 分布式数据库中间件Mycat分库分表应用...

    一.MySQL读写分离主从模式 1. 下载镜像 docker pull mysql 当前最新版本:mysql Ver 8.0.19 for Linux on x86_64 (MySQL Communi ...

  4. MySQL单表膨胀优化之MyCat分库分表

    MySQL的单表达到多少量级时性能会下降?宽表在千万量级,窄表要好一点在1200W左右.但是MySQL单表达到1500W时性能开始急剧下降! 事实上MySQL单表可以存储10亿级数据,只是这时候性能比 ...

  5. MyCat分库分表和读写分离

    文章目录 1.MyCat 1.1.MyCat简介 1.2.Mycat对多数据库的支持 1.3.MyCAT架构 1.4.MyCat分库分表 1.5.MyCat下载与安装 1.5.1.下载mycat 1. ...

  6. mycat分库分表与读写分离

    mycat分库分表与读写分离 Dockerfile搭建mycat 1.创建mycat的配置文件 #新建目录 mkdir /docker/mycat#切换目录 cd /docker/mycat#下载my ...

  7. mycat分库分表demo

    关于Mycat,它是一个阿里的开源项目,用来解决分库分表的海量数据存储和查询优化,关于它的简介,可以直接参考介绍:Mycat简介. 下面对自己的demo做个记录: 我之前从192.168.68.3克隆 ...

  8. 细讲MyCat分库分表策略

    作为服务端模式的典型代表,MyCat不仅提供了丰富的分库分表策略,也提供了非常灵活的读写分离策略,并且其对客户端的侵入性是非常小的.本文主要讲解MyCat主要提供的分库分表策略,并且还会讲解MyCat ...

  9. 【mycat】mycat分库分表

    mycat分库分表 Mycat2 一大优势就是可以在终端直接创建数据源.集群.库表,并在创建时指定 分库.分表. 操作之前,请先启动一主一从的mysql服务,启动mycat服务 以下步骤不是必须,看自 ...

最新文章

  1. linux里那些依赖包
  2. opencv配置_Opencv在vs2012下的配置
  3. addr2line 和 tombstone问题分析
  4. BZOJ.2741.[FOTILE模拟赛]L(分块 可持久化Trie)
  5. kafka图形化管理工具kafka-manager
  6. Java是有法_Java基础语法
  7. 联发科发布天玑9000移动平台 4nm制程 Armv9架构
  8. Go语言潜力有目共睹,但它的Goroutine机制底层原理你了解吗?
  9. python数据处理源代码_python数据分析与应用源数据和代码
  10. 解决拼音汉字混合搜索,由于同音字导致搜出不相干的内容
  11. 产品升级|​9月产品升级,精彩不间断!
  12. 软著申请合作开发协议模板
  13. ros建图过程中给上位机发布地图信息
  14. 数字信号处理2-截止频率
  15. php generator 风雪,PHP 生成器Generator理解
  16. Java工程师学快速Python(4)----- I/O与异常处理
  17. Android开发之获取网络类型(WIFI、2G、3G、4G)和运营商名称
  18. 2020年中国汽车电子软件行业发展现状、竞争格局及未来发展趋势分析,“软件定义汽车”重构汽车产业格局「图」
  19. socket请求ip遇到的问题
  20. 从IBN-Net到Switchable Whitening:在不变性与判别力之间权衡

热门文章

  1. 免费的云端软件测试平台-快意测试云V1R1.B21更新发布
  2. 推荐8个免费建站、域名或虚拟主机
  3. ACTIVEPERL 5.10无法ppm安装
  4. c++中gets用法总结
  5. 计算机算法设计与分析教学大纲,算法设计与分析的教与学(教学大纲)
  6. (sdau) Summary of the third week.
  7. 跨界营销新套路:立白和杜蕾斯这样“去污”
  8. centos 下查看本机公网IP
  9. 谈JS 异步任务,微任务,宏任务
  10. JavaScript一行代码