Mycat : 数据库分库分表中间件
http://www.mycat.io/
mycat运行需要JVM,所以先安装java环境,JDK1.7以上。数据库采用mysql5.7,或者8.0
下载
下载地址:http://dl.mycat.io/1.6.6.1/
安装
下载的压缩包解压到安装目录,如:D:mycat
![](http://upload-images.jianshu.io/upload_images/7393203-a41f099242e18212.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/874/format/webp)
配置
在安装目录下的conf目录下,有几个最重要的配置文件:server.xml 、schema.xml 、rule.xml
修改server.xml :
user标签表示一个用户,mycat作为数据库中间件,可以通过下图的用户名root和密码123456去连接mycat,schemas是逻辑库,
可以用navicat创建mysql连接,这个用户下可以看到TESTDB的数据库。
![](http://upload-images.jianshu.io/upload_images/7393203-58ffe7d8200880e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/580/format/webp)
2.png
修改schema.xml :
![](http://upload-images.jianshu.io/upload_images/7393203-78956ca8e024b74b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
测试
在本地数据库建3个数据库,连接是上图的<writeHost>标签中的连接:
![](http://upload-images.jianshu.io/upload_images/7393203-9cea76bff45a81e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/606/format/webp)
4.png
在安装目录的bin目录下,打开startup_nowrap.bat,可以运行起来,运行成功或失败都有提示,
![](http://upload-images.jianshu.io/upload_images/7393203-932f6902063e7bc0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/905/format/webp)
5.png
- 还可以把bin目录加到环境变量,按如下方式,将mycat加到系统服务:
安装:mycat.bat install
启动:mycat.bat start
停止:mycat.bat stop
状态: mycat.bat status
连接mycat:
用navicat新建一个mysql连接,mycat默认端口是8066,用户名和密码就是server.xml中user标签中配置的
![](http://upload-images.jianshu.io/upload_images/7393203-aa93d0fb63d56447.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/486/format/webp)
6.png
连接成功后:
![](http://upload-images.jianshu.io/upload_images/7393203-fcd9750e5d0d40f1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/461/format/webp)
可以看到,这个逻辑库TESTDB下的customer表下的数据是来自不同的真实数据库db1和db2的数据。
那利用mycat进行分库分表,最主要的就是配置server.xml、schema.xml、rule.xml了,根据具体场景进行配置逻辑库和真实数据库的关系,
还可以配置读写分离、决高并发问题等等。。实现分布式存储。
转自:https://www.jianshu.com/p/adba8875d1c9