mycat 配置實現垂直分離和水平分離
1.垂直分離
環境根據上個實驗
在mysql-a 和 mysql-b 上建立相同的庫 但是存在著不同的表
mysql-a
[[email protected] ~]# mysql -u root -p"123"
mysql> create database test;
mysql> use test
mysql> create table emp(id int,name char(10));
mysql> insert into emp values(1,'robin');
mysql> select * from emp;
+------+-------+
| id | name |
+------+-------+
| 1 | robin |
+------+-------+
1 row in set (0.00 sec)
mysql-b
[
mysql> create database test;
mysql> use test
mysql> create table dpt(did int,d_name char(10));
mysql> insert into dpt values(100,'hr');
mysql> select * from dpt;
+------+--------+
| did | d_name |
+------+--------+
| 100 | hr |
+------+--------+
修改mycat配置檔案:
[
<user name="test">
<property name="password">test</property>
<property name="schemas">TESTDB</property>
</user>
[[email protected] ~]# vim /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
<table name="t2" primaryKey="id" type="global" dataNode="dn1" />
<table name="t1" primaryKey="name" type="global" dataNode="dn2" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database="db" />
<dataNode name="dn2" dataHost="localhost2" database="db" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="dn1" url="192.168.122.201:3306" user="My_cat" password="123">
</writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="dn2" url="192.168.122.202:3306" user="My_cat" password="123">
</writeHost>
</dataHost>
</mycat:schema>
mysql -A上建立表t2
mysql-B上建立表t1
測試:
[[email protected] ~]# mysql -u test -ptest -h 192.168.122.200 -P 8066
MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.01 sec)
MySQL [(none)]> use TESTDB
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 [TESTDB]>
MySQL [TESTDB]> show tables;
+--------------+
| Tables_in_db |
+--------------+
| t1 |
| t2 |
+--------------+
Database changed
MySQL [TESTDB]> show tables;
+--------------+
| Tables_in_db |
+--------------+
| t1 |
| t2 |
+--------------+
2 rows in set (0.02 sec)
MySQL [TESTDB]> select * from t2;
+------+
| name |
+------+
| tom |
+------+
1 row in set (0.15 sec)
MySQL [TESTDB]>
MySQL [TESTDB]> insert into t2 values('www');
Query OK, 1 row affected (0.01 sec)
MySQL [TESTDB]> select * from t2;
+------+
| name |
+------+
| tom |
| www |
+------+
2 rows in set (0.01 sec)
MySQL [TESTDB]>
MySQL [TESTDB]> insert into t1 values(1),(2);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
MySQL [TESTDB]> select * from t1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.04 sec)
MySQL [TESTDB]>
在mysql-A上MySQL庫中察看
MariaDB [db]> insert into t2 values("tom");
Query OK, 1 row affected (0.01 sec)
MariaDB [db]> select * from t2;
+------+
| name |
+------+
| tom |
| www |
+------+
2 rows in set (0.00 sec)
MariaDB [db]>
在mysql-B上檢視
MariaDB [db]> select * from t1;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
MariaDB [db]>
2.水平分離
環境根據上個實驗
在mysql-a 和 mysql-b 上建立相同的庫 但是存在著相同的表
MariaDB [db]> drop table t2;
Query OK, 0 rows affected (0.00 sec)
MariaDB [db]> create table t2(id int,name varchar(20));
Query OK, 0 rows affected (0.02 sec)
MariaDB [db]>
修改配置檔案
[[email protected] ~]# vim /usr/local/mycat/conf/server.xml
<user name="robin">
<property name="password">robin</property>
<property name="schemas">test</property>
</user>
在水平切分 加上type="global" ,就會在所有的表中都加插入資料
[[email protected] ~]# vim /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="">http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="t1" primaryKey="id" dataNode="dn1,dn2" rule="rule1" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database="db" />
<dataNode name="dn2" dataHost="localhost2" database="db" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="dn1" url="192.168.122.201:3306" user="My_cat" password="123">
</writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="dn2" url="192.168.122.202:3306" user="My_cat" password="123">
</writeHost>
</dataHost>
</mycat:schema>
[[email protected] ~]# vim /usr/local/mycat/conf/rule.xml
<tableRule name="rule1">
<rule>
<columns>id</columns>
<algorithm>func1</algorithm>
</rule>
</tableRule>
<!--安裝id規則,將id除於1024然後取餘,如果 餘數落在0~512就將資料寫到第一個資料庫,如果是在512~1024就放到第二個資料庫-->
<function name="func1" class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">2</property>
<property name="partitionLength">512</property>
</function>
<!--分成倆片,每片的 區間是512,必須要倆個相乘是1024-->
測試:
[[email protected] ~]# mysql -u test -ptest -h 192.168.122.200 -P 8066
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6.6.1-test-20180709095126 MyCat Server (OpenCloundDB)
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 |
+----------+
| TESTDB |
+----------+
1 row in set (0.03 sec)
MySQL [(none)]> use TESTDB
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 [TESTDB]>
MySQL [TESTDB]> show tables;
+------------------+
| Tables in TESTDB |
+------------------+
| t1 |
+------------------+
1 row in set (0.00 sec)
MySQL [TESTDB]> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
MySQL [TESTDB]> insert into t1(id,name) values(1,'tom');
Query OK, 1 row affected (0.20 sec)
MySQL [TESTDB]> insert into t1(id,name) values(512,'www');
Query OK, 1 row affected (0.01 sec)
MySQL [TESTDB]> insert into t1(id,name) values(514,'robin');
Query OK, 1 row affected (0.01 sec)
MySQL [TESTDB]>
MySQL [TESTDB]> insert into t1(id,name) values(514,'robin');
Query OK, 1 row affected (0.01 sec)
MySQL [TESTDB]> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 512 | www |
| 514 | robin |
| 1 | tom |
+------+-------+
3 rows in set (0.20 sec)
MySQL [TESTDB]>
在mysql-A上檢視
MariaDB [db]> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | tom |
+------+------+
1 row in set (0.00 sec)
MariaDB [db]>
在mysql-B上檢視
MariaDB [db]> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 512 | www |
| 514 | robin |
+------+-------+
2 rows in set (0.00 sec)
MariaDB [db]>
mysql> insert into t1 values(10,"aa");
ERROR 1064 (HY000): partition table, insert must provide ColumnList
mysql> insert into t1(id,name) values(10,"aa");
mysql> insert into t1(id,name) values(511,"aa");
mysql> insert into t1(id,name) values(512,"aa");
mysql> insert into t1(id,name) values(1024,"aa");
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 10 | aa |
| 511 | aa |
| 1024 | aa |
| 512 | aa |
+------+------+
4 rows in set (0.11 sec)
master-a
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 10 | aa |
| 511 | aa |
| 1024 | aa |
+------+------+
master-b
mysql> select * from t1;
+-----+------+
| id | name |
+-----+------+
| 512 | aa |
+-----+------+