mycat實踐

本次實踐基於ubuntu系統;

mycat:1.6.5;

採用 docker拉起3個 mysql容器,端口分別位於33061,33062,33063。

安裝java

sudo apt-get install openjdk-8-jdk-headless

安裝mycat

mycat安裝包解壓到/usr/local下:java

sudo chown -R $USER /usr/local/mycat

啓動mycat

/usr/local/mycat/bin/mycat start

查看logs/wrapper.log監控啓動狀態node

使用mycat

1.配置mycat
  • server.xml中配置可訪問用戶:
<user name="mycat">
    <property name="password">mycat</property>
    <property name="schemas">db1</property>
</user>

這裏的db1必須是schema.xml中配置的,不然報錯。mysql

  • schema.xml中配置參數:
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    // schema標籤的name對應server.xml中的schema
    <schema name="db1" checkSQLschema="true" sqlMaxLimit="1000000">
        <table name="user" primaryKey="id" autoIncrement="true" dataNode="dn$1-3" rule="mod-long" />
    </schema>
    <dataNode name="dn1" dataHost="node1" database="node" />
    <dataNode name="dn4" dataHost="node1" database="node" />
    <dataNode name="dn7" dataHost="node1" database="node" />
    <dataNode name="dn11" dataHost="node1" database="node" />
    <dataNode name="dn2" dataHost="node2" database="node" />
    <dataNode name="dn5" dataHost="node2" database="node" />
    <dataNode name="dn8" dataHost="node2" database="node" />
    <dataNode name="dn11" dataHost="node2" database="node" />
    <dataNode name="dn3" dataHost="node3" database="node" />
    <dataNode name="dn6" dataHost="node3" database="node" />
    <dataNode name="dn9" dataHost="node3" database="node" />
    <dataNode name="dn12" dataHost="node3" database="node" />
    // dataHost的name對應dataNode中的dataHost
    <dataHost name="node1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        // 在這裏配置docker拉起來的3個容器
        <writeHost host="hostM1" url="192.168.1.5:33061" user="root" password="root">
        </writeHost>
    </dataHost>
    <dataHost name="node2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="hostM2" url="192.168.1.5:33062" user="root" password="root">
        </writeHost>
    </dataHost>
    <dataHost name="node3" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="hostM3" url="192.168.1.5:33063" user="root" password="root">
        </writeHost>
    </dataHost>
</mycat:schema>
  • 進入mycat數據庫層:
root@063b64a0619f:/# mysql -u mycat -p -P 8066 -h HOST

此處HOSTmycat安裝所在的ip。sql

mysql> show databases;
+----------+
| DATABASE |
+----------+
| db1      |
+----------+
1 row in set (0.01 sec)

這裏的db1是schema標籤中對應的名稱,這是一個虛擬庫。docker

mysql> use db1;
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 datacache        |
+----------------------------+
| user                       |
+----------------------------+
5 rows in set (0.01 sec)

這裏的user表實際上目前也是個虛擬表,只有當在子節點中建立表以後,這個表纔有意義。數據庫

mysql> CREATE TABLE IF NOT EXISTS `user` (
    ->  `id` varchar(20) NOT NULL DEFAULT '0' COMMENT 'ID',
    ->  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '',
    ->  `created_at` datetime NOT NULL DEFAULT now() COMMENT '',
    ->  PRIMARY KEY (`id`)
    -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 1 warning (0.01 sec)

1.這裏的id不採用int類型,是由於我採用的全局序列是默認的本地時間戳方式,int長度不夠;

2.這裏建立表結構,必須在對用的全部dataNode上建立相同的表,若是隻是在mycat庫中建立,只會在第一個dataNode中建立成功,並不能在全部dataNode中一塊兒建立,我想這是mycat仍然會進行優化的地方吧。ubuntu

  • 插入數據:
mysql> INSERT INTO `user` (`name`,`date`) VALUES ('mycat','2017-10-10');
Query OK, 1 row affected (0.01 sec)
mysql> select * from user;
+----+-------+---------------------+
| id | name  | created_at          |
+----+-------+---------------------+
|  1 | mycat | 2018-02-01 07:12:26 |
+----+-------+---------------------+
1 row in set (0.01 sec)

1.當插入多條數據時,數據會根據mod-long的分片方式分散到不一樣節點上;

2.若是以時間維度做爲篩選條件,會遍歷全部節點,因此,根據個人業務需求,我將分片方式改成sharding-by-monthbash

本來是分佈在3個節點上的相同database上,可是發現當我插入一條數據,就會產生3條數據;

解決方式是分佈到不一樣的database上。
<schema name="db1" checkSQLschema="true" sqlMaxLimit="1000000">
    <table name="user" primaryKey="id" autoIncrement="true" dataNode="dn$1-12" rule="sharding-by-month" />
</schema>
<dataNode name="dn1" dataHost="node1" database="node1" />
<dataNode name="dn4" dataHost="node1" database="node2" />
<dataNode name="dn7" dataHost="node1" database="node3" />
<dataNode name="dn11" dataHost="node1" database="node4" />
<dataNode name="dn2" dataHost="node2" database="node1" />
<dataNode name="dn5" dataHost="node2" database="node2" />
<dataNode name="dn8" dataHost="node2" database="node3" />
<dataNode name="dn11" dataHost="node2" database="node4" />
<dataNode name="dn3" dataHost="node3" database="node1" />
<dataNode name="dn6" dataHost="node3" database="node2" />
<dataNode name="dn9" dataHost="node3" database="node3" />
<dataNode name="dn12" dataHost="node3" database="node4" />

這樣雖然須要建立12個database,數據準確性問題獲得解決。app

以上

相關文章
相關標籤/搜索