Mycat

安装mycat

下载mycat官方的安装包,最好用github上面的,避免采坑

https://github.com/MyCATApache/Mycat-Server/releases

mysql8 驱动Maven_数据库

在usr目录下面新建目录mycat

sudo mkdir mycat

解压压缩包到此目录

sudo unrar e mycat1.6.7.1.rar

如果没有安装rar, 可以安装一下,安装命令如下:

sudo apt get rar

解压之后得到以下文件Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz
需要继续解压

mysql8 驱动Maven_数据库_02

tar -zxvf Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz

兼容mysql8

由于mycat1.6仅支持mysql5,不支持mysql8, 首先需要替换bin目录下面的mysq驱动文件为对应版本的,查看mysq版本

mysql -V

mysql8 驱动Maven_数据库_03

下载对应的驱动包

https://mvnrepository.com/artifact/mysql/mysql-connector-java?cf_chl_captcha_tk=01fab9921304b803c833002e4bee58960f5609a0-1607528202-0-AT-mnJkNl0PIbV1dqtw2ZLZfpQA_eDjUcSKekQb2Xix4nLFZJMAvadJI1zpV5AkdqW3zoFUW901btxoUs_nPbKyrY_Ltxutrwpb6YGptzDU2WLYfsIdloJdBdsFGeV_VCXEUvK0LLhjC1e0nSF_h22gRgzbxXL9-5e4JvZt9syC0E7Xa-uf67XnArzLrSN-hHOjmtA2949QrXaA_SkwfzIhZI5mT0pvvgF3DyOAMpsqMy4qiVU_iHV7qPemt9N3Ae92eRQpOJzJ7T317_mQvQzAhzan9GcGN47UOQIIDbUgY1IDoGxh3mwlHLxyu0MONZ5QV5oDeCKVu3-1W4tR-1MCVwiKYDOwf2bKuDx6N5ijKAPiy5w6STo-kFUNGmWTfghBeZr0ETwKcoMr5D1DUMWMZ2JPtDOoTz4bhuqS_4LwcFYJdnfpgiO0b9kG2nb3ta9ptsUVmw1r4mo8KOs_nN6Zb6Gg39P3KrJVOTu5vmTRuLl501hzafVHR_tKC6WGYHRaKYJN6AMAl_joS8akLwLNiB9cjdHYeFp3dd9tYhljSSdBp5b6nOZVyeStHwhJS_s1wxhdzSRCmSpxrxmrJCuyXgQeYlnY3J_ZDVh7KEiU3TbAZM9KGNl2D23wFZheejg

mysql8 驱动Maven_xml_04

下载后

mysql8 驱动Maven_mysql8 驱动Maven_05

复制到 mycat的lib目录

cd /usr/mycat/mycat/lib

sudo cp mysql-connector-java-8.0.19.jar /usr/mycat/mycat/bin

修改文件为可执行

chmod 777 mysql-connector-java-8.0.19.jar

备份原来的mysql驱动文件

sudo mv mysql-connector-java-5.1.35.jar mysql-connector-java-5.1.35.jar.bak

其次,连接mysql时需要使用jdbc方式,schema.xml见下面的配置

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
		  writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostM1" url="jdbc:mysql://127.0.0.1:3306?useSSL=false&serverTimezone=UTC" 
               user="root" password="mysql密码" />
</dataHost>

开启mycat端口

一般需要使用windows中的navicat来连接mycat服务,但是需要提前开启linux中的9066和8066端口

先安装iptables工具

sudo apt-get install iptables

然后开启端口

sudo iptables -I INPUT -p tcp --dport 8066 -j ACCEPT

sudo iptables -I INPUT -p tcp --dport 9066 -j ACCEPT

开启之后使用navicat就可以连接mycat

mysql8 驱动Maven_mysql_06

设置mycat开机启动

每次启动虚拟机都要进入mycat安装目录重新开启mycat服务,不方便,可以这样开机自启mycat
参考:

遇到问题参考:

设置开机启动之后可以使用系统命令控制mycat启动和停止

启动:sudo systemctl start mysqld.service

查看状态:sudo systemctl status mysqld.service

停止 :sudo systemctl stop mysqld.service

重启:sudo systemctl restart mysqld.service

分库分表简单配置

进入mycat文件夹的配置文件夹

cd /usr/mycat/mycat/conf

设置server.xml

<mycat:server xmlns:mycat="http://io.mycat/">
<system>
        <property name="nonePasswordLogin">1</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0,设置为1则需要指定默认账户-->
		<property name="defaultSqlParser">druidparser</property>
		<property name="useHandshakeV10">1</property>  <!--(这里记得修改)-->
		<property name="mutiNodeLimitType">1</property>
		<property name="charset">utf8</property>
		<property name="serverPort">8066</property>
		<property name="managerPort">9066</property>
	</system>
	<user name="root" defaultAccount="true">
		<property name="password">root</property>
		<property name="schemas">TEST</property>
	</user>
</mycat:server>

设置schemal.xml

<mycat:schema xmlns:mycat="http://io.mycat/">
	<schema name="TEST" checkSQLschema="false" sqlMaxLimit="100">
		<table name="customer_info_t" primaryKey="id" type="global" dataNode="dn1,dn2,dn3" />
		<table name="order_head_t" primaryKey="order_head_id" dataNode="dn1,dn2,dn3" rule="auto-sharding-long">
			<childTable name="order_line_t" primaryKey="order_line_id" joinKey="order_head_id"	parentKey="order_head_id"></childTable>
		</table>
	</schema>
	<dataNode name="dn1" dataHost="localhost1" database="db1" />
	<dataNode name="dn2" dataHost="localhost1" database="db2" />
	<dataNode name="dn3" dataHost="localhost1" database="db3" />
	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
		  writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<writeHost host="hostM1" url="jdbc:mysql://127.0.0.1:3306?useSSL=false&serverTimezone=UTC" 
		user="root" password="mysql密码" />
    </dataHost>
</mycat:schema>

auto-sharding-long分片算法在rule.xml中,不用改动该文件,但是对应的映射文件autopartition-long.txt需要根据实际情况修改

<tableRule name="auto-sharding-long">
		<rule>
			<columns>order_date</columns>
			<algorithm>rang-long</algorithm>
		</rule>
</tableRule>
<function name="rang-long"
		class="io.mycat.route.function.AutoPartitionByLong">
		<property name="mapFile">autopartition-long.txt</property>
</function>

autopartition-long.txt中采用两个月一个库的方式

# range start-end ,data node index
202001-202002=0
202003-202004=1
202005-202006=2

启动mycat需要进入mycat的bin目录,常用的启动命令

启动 ./mycat start
停止 ./mycat stop
前台运行 ./mycat console
重启服务 ./mycat restart
暂停 ./mycat pause
查看启动状态 ./mycat status

mysql8 驱动Maven_数据库_07

命令行登陆:

mysql -uroot -proot -h192.168.26.128 -P8066 -DTEST

直接在navicat中建表

DROP TABLE IF EXISTS `order_head_t`;
CREATE TABLE `order_head_t` (
  `order_head_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID主键',
  `order_date` varchar(8) NOT NULL COMMENT '下单时间',
  `order_no` varchar(32) NOT NULL COMMENT '订单号',
  PRIMARY KEY (`order_head_id`),
  UNIQUE KEY `uk_order_no` (`order_no`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT '订单头表';

DROP TABLE IF EXISTS `order_line_t`;
CREATE TABLE `order_line_t` (
  `order_line_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID主键',
  `order_head_id` bigint(20) NOT NULL COMMENT '订单头关联ID',
  `order_date` varchar(8) NOT NULL COMMENT '下单时间',
	`commodity` varchar(128) NOT NULL COMMENT '商品',
	`customer` varchar(64) NOT NULL COMMENT '客户',
  PRIMARY KEY (`order_line_id`),
  KEY `idx_order_head` (`order_head_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT '订单商品表';

DROP TABLE IF EXISTS `customer_info_t`;
CREATE TABLE `customer_info_t` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID主键',
  `customer` varchar(32) NOT NULL COMMENT '客户名称',
  `telephone` varchar(32) NOT NULL COMMENT '电话',
	`address` varchar(256) NOT NULL COMMENT '地址',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_customer` (`customer`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT '客户表';