mycat安裝及與springboot整合
1. mycat下載
http://dl.mycat.io/1.6-RELEASE/
2.解壓,配置環境變數,path=D:\software\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\bin[mycat安裝地址]
3.修改配置檔案【目錄:D:\software\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\conf】
a)修改wrapper.conf
b)修改server.xml,配置使用者名稱和密碼[如果server.xml配置了多個schema,那麼在schema.xml要有對應的schema匹配。]
設定自增長的方式
c)設定sequence_conf.properties
設定sequence_db_conf.properties
d)修改schema.xml (定義邏輯庫,表、分片節點等內容),如果是應用於多個專案,可以設定多個schema
e)修改rule.xml
在schema.xml配置分庫的rule的型別的時候,要修改rule.xml中相應的方法中分庫的數量,如下圖路由方法murmur。
注:簡單介紹一下Mycat分庫規則
MYCAT常用的分片規則如下,另外還有一些其他分片方式這裡不全部列舉:
(1)分片列舉: sharding-by-intfile
(2)主鍵範圍約定: auto-sharding-long 此分片適用於,提前規劃好分片欄位某個範圍屬於哪個分片
(3)一致性hash: sharding-by-murmur
(4)字串hash解析: sharding-by-stringhash
(5)按日期(天)分片:sharding-by-date
(6)按單月小時拆分: sharding-by-hour
(7)自然月分片: sharding-by-month
(8)取模: mod-long 此規則為對分片欄位求摸運算
(9)取模範圍約束: sharding-by-pattern 此種規則是取模運算與範圍約束的結合,主要為了後續資料遷移做準備,即可以自主決定取模後資料的節點分佈
4. 啟動mycat
在mycat的bin路徑下【D:\software\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\bin】按住shift鍵的同時右鍵選擇【在此處開啟視窗】
安裝mycat服務 :mycate install
啟動mycat服務 :mycate start
停止mycat服務 :mycate stop
注意:當修改配置檔案後,需要重啟mycat服務 reload @@config;
安裝開啟mycat服務後,輸入命令進入mysql,如果是連線本機不需要-h[ip]
mysql -h[ip] -u[user] -p[password] -P8066
eg: mysql -uroot -p123456 -P8066
可輸入以下命令檢視資料庫資訊。
show databases;//分號不能少
5. 與navicat連線
新建連線,埠選擇8066
在navicat 埠為3306的連線中建立資料庫db1,db2,db3.然後在navicat的命令列介面建立表。
mysql> use TESTDB;
Query OK, 0 rows affected
mysql> create table userinfo(id int primary key not null auto_increment,username varchar(16) not null,password varchar(16) not null,phone varchar(16),name varchar(50));
Query OK, 0 rows affected
mysql> create table rankglobal(id int primary key not null auto_increment,rankname varchar(16) not null);
Query OK, 0 rows affected
mycat在springboot中的使用
1.在application.yml中寫資料庫相關資訊
spring:
datasource:
url: jdbc:mysql://127.0.0.1:8066/TESTDB
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
2.按照教程進行springboot的搭建,建立相關的dao,service,controller
----------------------------------------------------------------------------------------------------------------------------------
相關的報錯:
1.Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown character set: 'utf8mb4'
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown character set: 'utf8mb4'
解決方法:更換驅動版本
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
2.Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1
原因:userinfo 表中id欄位設定的是int,應該設為bigint
ALTER TABLE userinfo MODIFY COLUMN id BIGINT(64) auto_increment not null;
根本原因:mycat中server.xml中自增長的方式設定成時間戳,導致自增長獲得的id特別大,超出了int的範圍
解決方法:將自增長方式設定為自定義
3. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: columnValue:dcx Please eliminate any quote and non number within it.
原因:分片方式設定為mod-long,而分片的欄位是字串型別
解決方法: