06-MySQL基於MyCat的分散式資料庫的搭建
一、MyCat簡介
1、什麼是MyCat?
MyCAT是:
* 一個徹底開源的,面向企業應用開發的“大資料庫叢集”;
* 支援事務、ACID、可以替代MySQL的加強版資料庫;
* 一個可以視為“MySQL”叢集的企業級資料庫,用來替代昂貴的Oracle叢集;
* 一個融合記憶體快取技術、Nosql技術、HDFS大資料的新型SQL;
* 一個新穎的資料庫中介軟體產品;
2、MyCat的特性
* 支援 SQL92標準(資料庫的一個ANSI/ISO標準);
* 支援Mysql叢集,可以作為Proxy使用;
* 支援JDBC連線ORACLE、DB2、SQL Server、PostgreSQL、MongoDB,將其模擬為MySQL Server使用;
* 自動故障切換,高可用性;
* 支援讀寫分離,支援Mysql雙主多從,以及一主多從的模式;
* 支援全域性表,資料自動分片到多個節點,用於高效表關聯查詢;
* 支援獨有的基於E-R 關係的分片策略,實現了高效的表關聯查詢;
3、MyCat架構圖
4、MyCat的分片策略
* MyCAT支援水平分片與垂直分片:
水平分片:一個表格的資料分割到多個節點上,按照行分隔。
垂直分片:一個數據庫中多個表格A,B,C,A儲存到節點1上,B儲存到節點2上,C儲存到節點3上。
* MyCat通過定義表的分片規則來實現分片,每個表格可以繫結一個分片規則,每個分片規則指定一個分片欄位並繫結一個函式,來實現動態分配演算法。
5、MyCat的組成部分
* Schema:邏輯庫,與MySQL中的Database對應,一個邏輯庫中定義了所包括的Table;
* Table:表,即物理資料庫中儲存的某一張表,與傳統資料庫不同,這裡的表格需要宣告其所儲存的邏輯資料節點(DataNode),在此可以指定表的分片規則;
* DataNode:MyCat的邏輯資料節點,是存放Table的具體物理節點,也稱之為分片節點,通過DataSource來關聯到後端某個具體的資料庫上;
* DataSource:定義某個物理庫的訪問地址,用於捆綁到DataNode上;
6、MyCat百度雲盤地址(Mycat-server-1.6.7.4-release-20200105164103-linux.tar),有需要的可以下載,也可以去官網下載
連結:https://pan.baidu.com/s/1a6Y_YL2DHUzz4zyvYHRkKQ 提取碼:z9os
官網:http://www.mycat.org.cn/
二、MyCat架構的搭建
1、測試環境IP配置
機器名稱 | IP | 伺服器角色 | 備註 |
Manager | 192.168.232.45 | MyCat伺服器 | 作業系統redhat7.5 |
node1 | 192.168.232.42 | MySQL主節點 | 作業系統redhat7.5;MySQL5.7.22 |
node2 | 192.168.232.43 | MySQL從節點1 | 作業系統redhat7.5;MySQL5.7.22 |
node3 | 192.168.232.44 | MySQL從節點2 | 作業系統redhat7.5;MySQL5.7.22 |
2、配置IP對映
在各節點的/etc/hosts檔案中配置如下內容:
192.168.232.42 node1 192.168.232.43 node2 192.168.232.44 node3 192.168.232.45 manager
3、3個節點先安裝好資料庫
注:
* 這裡不做說明,參考安裝文件,資料版本最好一致;
* 3個數據庫節點上都要新增忽略大小寫的配置,在/etc/my.cnf下新增lower_case_table_names=1即可;
4、manage節點上建立mycat使用者和組
groupadd mycat useradd mycat -g mycat passwd mycat cat /etc/group --檢視使用者組
5、解壓mycat安裝包
tar -zxvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
注:mycat是依賴jdk的,沒有安裝jdk的要先安裝好jdk(jdk7以上)
6、配置環境變數
7、配置schema.xml檔案
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="customer" checkSQLschema="false" sqlMaxLimit="100"> <!-- auto sharding by id (long) --> <table name="person" dataNode="dn1,dn2,dn3" rule="mod-long" /> <table name="TB_ITEM" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" /> <table name="TB_USER" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" rule="mod-long" /> <!-- global table is auto cloned to all defined data nodes ,so can join with any table whose sharding node is in the same data node --> <table name="student" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" /> </schema> <dataNode name="dn1" dataHost="node1" database="customer1" /> <dataNode name="dn2" dataHost="node2" database="customer2" /> <dataNode name="dn3" dataHost="node3" database="customer3" /> <dataHost name="node1" 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="node1" url="192.168.232.42:3308" user="root" password="123456"> <!-- can have multi read hosts --> </writeHost> </dataHost> <dataHost name="node2" 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="node2" url="192.168.232.43:3308" user="root" password="123456"> <!-- can have multi read hosts --> </writeHost> </dataHost> <dataHost name="node3" 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="node3" url="192.168.232.44:3308" user="root" password="123456"> <!-- can have multi read hosts --> </writeHost> </dataHost> </mycat:schema>
注:
* customer是邏輯庫,應用訪問的就是這個庫,下面dataNode裡配置的customer1、customer2、customer3是3個節點上的物理資料庫,真正儲存資料的地方;
* table標籤中的type=“global”表示的是全域性表,在所指定的dataNode節點上都儲存資料,意思就是儲存3份資料,沒有做切片;
* balance引數設定
balance=“0”, 所有讀操作都發送到當前可用的writeHost上。 balance=“1”,所有讀操作都隨機的傳送到readHost。 balance=“2”,所有讀操作都隨機的在writeHost、readhost上分發。
* WriteType引數設定
writeType=“0”, 所有寫操作都發送到可用的writeHost上。 writeType=“1”,所有寫操作都隨機的傳送到readHost。 writeType=“2”,所有寫操作都隨機的在writeHost、readhost分上發。
* swatchType引數設定
-1:表示不自動切換 1 :預設值,自動切換 2 :基於MySQL主從同步的狀態決定是否切換
注:Mycat心跳檢查語句配置為 show slave status ,dataHost 上定義兩個新屬性: switchType="2" 與slaveThreshold="100",此時意味著開啟MySQL主從複製狀態繫結的讀寫分離與切換機制。Mycat心跳機制通過檢測 show slave status 中的 "Seconds_Behind_Master", "Slave_IO_Running", "Slave_SQL_Running" 三個欄位來確定當前主從同步的狀態以及Seconds_Behind_Master主從複製時延。
8、配置server.xml檔案
注:server.xml幾乎儲存了mycat需要的所有配置資訊,最常用的是在此配置邏輯資料庫、使用者、密碼、許可權等。
<?xml version="1.0" encoding="UTF-8"?> <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <!DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <system> <property name="defaultSqlParser">druidparser</property> <!-- <property name="useCompression">1</property>--> <!--1為開啟mysql壓縮協議--> <!-- <property name="processorBufferChunk">40960</property> --> <!-- <property name="processors">1</property> <property name="processorExecutor">32</property> --> <!--預設是65535 64K 用於sql解析時最大文字長度 --> <!--<property name="maxStringLiteralLength">65535</property>--> <!--<property name="sequnceHandlerType">0</property>--> <!--<property name="backSocketNoDelay">1</property>--> <!--<property name="frontSocketNoDelay">1</property>--> <!--<property name="processorExecutor">16</property>--> <!-- <property name="mutiNodeLimitType">1</property> 0:開啟小數量級(預設) ;1:開啟億級資料排序 <property name="mutiNodePatchSize">100</property> 億級數量排序批量 <property name="processors">32</property> <property name="processorExecutor">32</property> <property name="serverPort">8066</property> <property name="managerPort">9066</property> <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> --> </system> <user name="test"> <property name="password">test</property> <property name="schemas">customer</property> </user> <user name="user"> <property name="password">user</property> <property name="schemas">customer</property> <property name="readOnly">true</property> </user> <!-- <cluster> <node name="cobar1"> <property name="host">127.0.0.1</property> <property name="weight">1</property> </node> </cluster> --> <!-- <quarantine> <host name="1.2.3.4"> <property name="user">test</property> </host> </quarantine> --> </mycat:server>
注:test是讀寫使用者,user是隻讀使用者
9、配置rule.xml檔案
注:rule.xml裡面就定義了我們對錶進行拆分所涉及到的規則定義。我們可以靈活的對錶使用不同的分片演算法,或者對錶使用相同的演算法但具體的引數不同。這個檔案裡面主要有tableRule和function這兩個標籤。在具體使用過程中可以按照需求新增tableRule和function。
此配置檔案可以不用修改,使用預設即可。
10、在三個節點先建立好資料庫(customer1、customer2、customer3)
--node1 mysql> create database customer1 default character set utf8; Query OK, 1 row affected (0.06 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | customer1 | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) --node2 mysql> create database customer2 default character set utf8; Query OK, 1 row affected (0.06 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | customer2 | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.01 sec) --node3 mysql> create database customer3 default character set utf8; Query OK, 1 row affected (0.34 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | customer3 | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
11、啟動mycat
[root@manager ~]# su - mycat Last login: Tue Dec 22 10:54:32 CST 2020 on pts/0 [mycat@manager ~]$ mycat start Starting Mycat-server... [mycat@manager ~]$ mycat status Mycat-server is running (4396). [mycat@manager ~]$ ps -ef | grep mycat root 4284 4074 0 14:23 pts/1 00:00:00 su - mycat mycat 4285 4284 0 14:23 pts/1 00:00:00 -bash mycat 4396 1 0 14:24 ? 00:00:00 /mysql/mycat/mycat/bin/./wrapper-linux-x86-64 /mysqlmycat/mycat/conf/wrapper.conf wrapper.syslog.ident=mycat wrapper.pidfile=/mysql/mycat/mycat/logs/mycat.pid wrapper.daemonize=TRUE mycat 4398 4396 33 14:24 ? 00:00:06 java -DMYCAT_HOME=. -server -XX:+AggressiveOpts -XX:MaxDirectMemorySize=2G -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1984 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xmx4G -Xms1G -Djava.library.path=lib -classpath lib/wrapper.jar:conf:lib/mysql-binlog-connector-java-0.16.1.jar:lib/reflectasm-1.03.jar:lib/annotations-13.0.jar:lib/kotlin-stdlib-1.3.50.jar:lib/kotlin-stdlib-common-1.3.50.jar:lib/curator-framework-2.11.0.jar:lib/log4j-core-2.5.jar:lib/commons-collections-3.2.1.jar:lib/mongo-java-driver-3.11.0.jar:lib/fastjson-1.2.58.jar:lib/commons-lang-2.6.jar:lib/log4j-1.2-api-2.5.jar:lib/Mycat-server-1.6.7.4-release.jar:lib/mysql-connector-java-5.1.35.jar:lib/netty-buffer-4.1.9.Final.jar:lib/hamcrest-core-1.3.jar:lib/hamcrest-library-1.3.jar:lib/libwrapper-linux-x86-64.so:lib/log4j-slf4j-impl-2.5.jar:lib/ehcache-core-2.6.11.jar:lib/asm-4.0.jar:lib/leveldb-api-0.7.jar:lib/minlog-1.2.jar:lib/jsr305-2.0.3.jar:lib/log4j-api-2.5.jar:lib/jline-0.9.94.jar:lib/libwrapper-linux-ppc-64.so:lib/dom4j-1.6.1.jar:lib/kryo-2.10.jar:lib/okio-2.2.2.jar:lib/libwrapper-linux-x86-32.so:lib/objenesis-1.2.jar:lib/druid-1.0.26.jar:lib/wrapper.jar:lib/leveldb-0.7.jar:lib/mapdb-1.0.7.jar:lib/univocity-parsers-2.2.1.jar:lib/guava-19.0.jar:lib/joda-time-2.9.3.jar:lib/curator-recipes-2.11.0.jar:lib/zookeeper-3.4.6.jar:lib/okhttp-4.2.2.jar:lib/sequoiadb-driver-1.12.jar:lib/slf4j-api-1.6.1.jar:lib/velocity-1.7.jar:lib/log4j-1.2.17.jar:lib/netty-common-4.1.9.Final.jar:lib/netty-3.7.0.Final.jar:lib/disruptor-3.3.4.jar:lib/curator-client-2.11.0.jar -Dwrapper.key=8mVcPdJniednz5DH -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.max=31999 -Dwrapper.pid=4396 -Dwrapper.version=3.2.3 -Dwrapper.native_library=wrapper -Dwrapper.service=TRUE -Dwrapper.cpu.timeout=10 -Dwrapper.jvmid=1 org.tanukisoftware.wrapper.WrapperSimpleApp io.mycat.MycatStartup start mycat 4485 4285 0 14:25 pts/1 00:00:00 ps -ef mycat 4486 4285 0 14:25 pts/1 00:00:00 grep --color=auto mycat [mycat@manager ~]$
12、連線mycat
注:mycat的預設埠是8066
[root@manager ~]# mysql -utest -ptest -P8066 -h192.168.232.45 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB) Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +----------+ | DATABASE | +----------+ | customer | +----------+ 1 row in set (0.00 sec) MySQL [(none)]> use customer; 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 [customer]> show tables; +--------------------+ | Tables in customer | +--------------------+ | person | | student | | tb_item | | tb_user | +--------------------+ 4 rows in set (0.00 sec) MySQL [customer]> select * from tb_item; ERROR 1105 (HY000): Table 'customer1.tb_item' doesn't exist MySQL [customer]>
注:連線成功後,在customer邏輯庫下是有這幾個表名的,但是這些表使用的時候還是需要建立,這裡這是顯示在配置檔案配置的table標籤裡有哪些表名而已,是邏輯表,真正的資料節點上還看不到表。
13、連線mycat的管理端
注:mycat的管理埠是9066
[root@manager ~]# mysql -utest -ptest -P9066 -h192.168.232.45 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (monitor) Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show @@database; +----------+ | DATABASE | +----------+ | customer | +----------+ 1 row in set (0.01 sec) MySQL [(none)]> show @@dataNode; +------+-----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+ | NAME | DATHOST | INDEX | TYPE | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME | +------+-----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+ | dn1 | node1/customer1 | 0 | mysql | 0 | 10 | 1000 | 77 | 0 | 0 | 0 | -1 | | dn2 | node2/customer2 | 0 | mysql | 0 | 10 | 1000 | 74 | 0 | 0 | 0 | -1 | | dn3 | node3/customer3 | 0 | mysql | 0 | 10 | 1000 | 75 | 0 | 0 | 0 | -1 | +------+-----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+ 3 rows in set (0.02 sec) MySQL [(none)]> show @@heartbeat; +-------+-------+----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ | NAME | TYPE | HOST | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME | STOP | +-------+-------+----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ | node1 | mysql | 192.168.232.42 | 3308 | 1 | 0 | idle | 30000 | 4,3,3 | 2020-12-22 14:35:42 | false | | node2 | mysql | 192.168.232.43 | 3308 | 1 | 0 | idle | 30000 | 3,2,2 | 2020-12-22 14:35:42 | false | | node3 | mysql | 192.168.232.44 | 3308 | 1 | 0 | idle | 30000 | 5,2,2 | 2020-12-22 14:35:42 | false | +-------+-------+----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ 3 rows in set (0.01 sec) MySQL [(none)]> show @@cache; +-------------------------------------+-------+------+--------+------+------+---------------+---------------+ | CACHE | MAX | CUR | ACCESS | HIT | PUT | LAST_ACCESS | LAST_PUT | +-------------------------------------+-------+------+--------+------+------+---------------+---------------+ | ER_SQL2PARENTID | 1000 | 0 | 0 | 0 | 0 | 0 | 0 | | SQLRouteCache | 10000 | 1 | 1 | 0 | 1 | 1608618743582 | 1608618743689 | | TableID2DataNodeCache.TESTDB_ORDERS | 50000 | 0 | 0 | 0 | 0 | 0 | 0 | +-------------------------------------+-------+------+--------+------+------+---------------+---------------+ 3 rows in set (0.02 sec) MySQL [(none)]> show @@connection; +------------+------+----------------+------+------------+------+----------+---------+--------+---------+---------------+-------------+------------+---------+------------+ | PROCESSOR | ID | HOST | PORT | LOCAL_PORT | USER | SCHEMA | CHARSET | NET_IN | NET_OUT | ALIVE_TIME(S) | RECV_BUFFER | SEND_QUEUE | txlevel | autocommit | +------------+------+----------------+------+------------+------+----------+---------+--------+---------+---------------+-------------+------------+---------+------------+ | Processor0 | 1 | 192.168.232.45 | 8066 | 45614 | test | customer | utf8:33 | 281 | 903 | 309 | 4096 | 0 | 3 | true | | Processor0 | 2 | 192.168.232.45 | 9066 | 33918 | test | NULL | utf8:33 | 199 | 1902 | 99 | 4096 | 0 | | | +------------+------+----------------+------+------------+------+----------+---------+--------+---------+---------------+-------------+------------+---------+------------+ 2 rows in set (0.00 sec) MySQL [(none)]>
管理端相關命令:
* reload命令
* reload @@config; 重新載入配置檔案,在修改完配置檔案後,不用重啟mycat。 * reload @@sqlstat=open; 開啟SQL監控分析功能。 * reload @@sqlstat=close; 關閉SQL監控分析功能。 * reload @@sqlshow=10; 設定慢SQL時間閾值。 * reload @@user_stat; 重置SQL監控分析的資料,這個命令用於清除快取。該命令工作在9066埠,用來將客戶端執行show @@sql; show @@sql.sum;show@@slow.success;命令之後的快取資訊清除。
* show命令
* show @@database; 檢視所有資料庫。 * show @@dataNode; 檢視所有的資料節點(ACTIVE:表示活躍的連線數量,IDLE:表示空閒的連線數量,SIZE:表示對應的總連線數量)。 * show @@dataNode where schema=customer; 檢視某個邏輯庫的所有資料節點。 * show @@heartbeat; 檢查心跳狀態(1:正常狀態,-1:連線錯誤,-2:連線超時,0:初始化狀態)。 注:若節點發生故障,則會連續進行預設的5個週期檢測,心跳連線失敗後就會變成-1,節點故障確認,然後可能發生切換。 * show @@connection; 獲取mycat當前的連線狀態,即現在有幾個客戶端連線mycat。 * kill @@connection 1; 強制關閉連線,id的值是show @@connection結果的id列。 * show @@backend; 檢視後端連線狀態。 * show @@cache ; 檢視mycat快取。 * show @@datasource; 檢視資料來源的狀態,如果配置了主從或者多主,則可以切換。 * show @@syslog limit=3; 顯示系統日誌,limit=3表示顯示行數。
三、測試分片
1、使用“auto-sharding-long”分片規則的表TB_ITEM
注:
* 從schema.xml裡可以看出TB_ITEM表使用的分片規則是auto-sharding-long
* 從rule.xml看auto-sharding-long的具體分片函式,是按照id列來具體分片的
(1)連線mycat,建立TB_ITEM表
MySQL [(none)]> use customer; 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 [customer]> show tables; +--------------------+ | Tables in customer | +--------------------+ | person | | student | | tb_item | | tb_user | +--------------------+ 4 rows in set (0.00 sec) MySQL [customer]> create table tb_item (id int(10),name varchar(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.12 sec) MySQL [customer]>
(2)插入資料
注:使用mycat插入資料的時候,要補全欄位值
MySQL [customer]> insert into tb_item(id,name) values(1,'jack'); Query OK, 1 row affected (0.07 sec) MySQL [customer]> insert into tb_item(id,name) values(5000000,'jack'); Query OK, 1 row affected (0.00 sec) MySQL [customer]> insert into tb_item(id,name) values(5000001,'jack'); Query OK, 1 row affected (0.02 sec) MySQL [customer]> insert into tb_item(id,name) values(10000000,'rose'); Query OK, 1 row affected (0.02 sec) MySQL [customer]> insert into tb_item(id,name) values(10000001,'wade'); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into tb_item(id,name) values(15000000,'wade'); Query OK, 1 row affected (0.00 sec) MySQL [customer]> insert into tb_item(id,name) values(15000001,'wade'); ERROR 1064 (HY000): can't find any valid datanode :TB_ITEM -> ID -> 15000001 MySQL [customer]>
(3)檢視3個節點的資料分佈情況
--node1 mysql> use customer1; Database changed mysql> show tables; +---------------------+ | Tables_in_customer1 | +---------------------+ | tb_item | +---------------------+ 1 row in set (0.00 sec) mysql> select * from tb_item; +---------+------+ | ID | NAME | +---------+------+ | 1 | jack | | 5000000 | jack | +---------+------+ 2 rows in set (0.00 sec) --node2 mysql> use customer2; 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_customer2 | +---------------------+ | tb_item | +---------------------+ 1 row in set (0.00 sec) mysql> select * from tb_item; +----------+------+ | ID | NAME | +----------+------+ | 5000001 | jack | | 10000000 | rose | +----------+------+ 2 rows in set (0.00 sec) --node3 mysql> use customer3; 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_customer3 | +---------------------+ | tb_item | +---------------------+ 1 row in set (0.00 sec) mysql> select * from tb_item; +----------+------+ | ID | NAME | +----------+------+ | 10000001 | wade | | 15000000 | wade | +----------+------+ 2 rows in set (0.00 sec)
注:從資料的分佈情況和最後insert資料來看,發現使用auto-sharding-long分片規則的資料分佈如下:
node1:1~5000000
node2:5000001~10000000
node3:10000001~15000000
如果資料超過15000000,就需要新增節點了。
2、使用“mod-long”分片規則的表person
(1)檢視server.xml中mod-long的分片函式
注:也是通過對id欄位求模,來對資料進行分片的。
(2)連線mycat,建立person表
MySQL [customer]> create table person(id int(10),age int(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.02 sec)
(3)插入資料
MySQL [customer]> insert into person(id,age) values(1,20); Query OK, 1 row affected (0.22 sec) MySQL [customer]> insert into person(id,age) values(2,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(3,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(4,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(5,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(6,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(5000000,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(5000001,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(10000000,25); Query OK, 1 row affected (0.00 sec) MySQL [customer]> insert into person(id,age) values(10000001,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(15000000,25); Query OK, 1 row affected (0.01 sec) MySQL [customer]> insert into person(id,age) values(15000001,25); Query OK, 1 row affected (0.00 sec)
(4)檢視3個節點的資料分佈情況
--node1 mysql> show tables; +---------------------+ | Tables_in_customer1 | +---------------------+ | person | | tb_item | +---------------------+ 2 rows in set (0.00 sec) mysql> select * from person; +-----------+------+ | ID | AGE | +-----------+------+ | 3 | 25 | | 6 | 25 | | 5000001 | 25 | | 15000000 | 25 | | 950000010 | 25 | +-----------+------+ 5 rows in set (0.00 sec) --node2 mysql> show tables; +---------------------+ | Tables_in_customer2 | +---------------------+ | person | | tb_item | +---------------------+ 2 rows in set (0.00 sec) mysql> select * from person; +----------+------+ | ID | AGE | +----------+------+ | 1 | 20 | | 4 | 25 | | 10000000 | 25 | | 15000001 | 25 | +----------+------+ 4 rows in set (0.00 sec) --node3 mysql> show tables; +---------------------+ | Tables_in_customer3 | +---------------------+ | person | | tb_item | +---------------------+ 2 rows in set (0.00 sec) mysql> select * from person; +----------+------+ | ID | AGE | +----------+------+ | 2 | 25 | | 5 | 25 | | 5000000 | 25 | | 10000001 | 25 | +----------+------+ 4 rows in set (0.00 sec)
注:從上面的資料分佈可以看出規律如下:
一共三個分片資料庫,所以count=3,分片的時候id的值要對3取模,
取模=0時,存放在第一個分片資料庫節點(3對3取模是0)
取模=1時,存放在第二個分片資料庫節點(1對3取模是1)
取模=2時,存放在第三個分片資料庫節點(2對3取模是2)