mycat垂直分表:
[root@db01 ~]# vim /application/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" dataNode="sh1"> </schema> <dataNode name="sh1" dataHost="oldguo1" database= "world" /> <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3307" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3309" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3307" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3309" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema>
| |
blance属性 | blance="0"不开启读写分离,所有操作都发送到当前可用的writehost上 |
blance="1"全部的readhost与standby writehost都参读操作 | |
blance="2"所有读的操作都随机在writehost,readhost上分发 | |
writeType属性 | writeType="0"所有写操作发送到配置的第一个writeHost,当第一个挂掉后自动切换到第二个writehost,重启启动第一个挂掉的writehost后自动变为备用 |
writeType="1" 所有写操作都随机的发送到配置的writeHost,不推荐使用 | |
switchType属性 | switchType="-1"表示不自动切换 |
swtichType="1"表示自动切换 | |
switchType="2"基于mysql主从同步的状态决定是否切换,心跳语句为show slave status | |
datahost其它配置 | maxCon="1000"最大的并发连接数 |
minCon="10"mycat启动后会在后端节点上自动开启的连接线程数 | |
<hertbest>select user()</heartbest>检测心跳 |
mycat垂直分库:
1准备工作
在3308这个节点上创建一个库,准备数据库和表的目的是为了看测试效果
[root@db01 ~]# mysql -S /data/3308/mysql.sock -e "create database oldboy charset utf8mb4;use oldboy;create table t1(id int);" [root@db01 ~]# mysql -S /data/3308/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 5.7.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use oldboy; 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_oldboy | +------------------+ | t1 | +------------------+ 1 row in set (0.03 sec)
mysql>
|
2编写mycat配置文件
启用两台机器中的3308主节点和3310从节点
[root@db01 ~]# cp /application/mycat/conf/schema.xml schema.xml2.bak [root@db01 ~]# vim /application/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" dataNode="sh1"> </schema> <dataNode name="sh1" dataHost="oldguo1" database= "world" /> <dataNode name="sh2" dataHost="oldguo2" database= "oldboy" /> <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3307" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3309" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3307" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3309" user="root" password="123" /> </writeHost> </dataHost> <dataHost name="oldguo2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3308" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3310" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3308" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3310" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema>
|
|
3重启mycat进行测试
[root@db01 ~]# mycat restart Stopping Mycat-server... Waiting for Mycat-server to exit... Stopped Mycat-server. Starting Mycat-server... [root@db01 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111) [root@db01 ~]# vim /application/mycat/logs/mycat.log #查看日志没发现问题再次尝试后成功 [root@db01 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
|
mycat垂直分表:
1创建测试库和表
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "create database taobao charset utf8;" [root@db01 ~]# mysql -S /data/3307/mysql.sock -e "use taobao;create table user(id int,name varchar(20))"; [root@db01 ~]# mysql -S /data/3308/mysql.sock -e "create database taobao charset utf8;" [root@db01 ~]# mysql -S /data/3308/mysql.sock -e "use taobao;create table order_t(id int,name varchar(20))"
|
2编写配置文件并测试:
(1)配置文件
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> <table name="user" dataNode="sh1"/> <table name="order_t" dataNode="sh2"/> </schema> <dataNode name="sh1" dataHost="oldguo1" database= "taobao" /> <dataNode name="sh2" dataHost="oldguo2" database= "taobao" /> <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3307" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3309" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3307" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3309" user="root" password="123" /> </writeHost> </dataHost> <dataHost name="oldguo2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3308" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3310" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3308" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3310" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema> ~ |
(2)执行过程
[root@db01 ~]# vim /application/mycat/conf/schema.xml [root@db01 ~]# mycat restart Stopping Mycat-server... Stopped Mycat-server. Starting Mycat-server... [root@db01 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
|
[root@db01 ~]# cp /application/mycat/conf/schema.xml schema.xml3.bak [root@db01 ~]# vim /application/mycat/conf/schema.xml [root@db01 ~]# mycat restart Stopping Mycat-server... Stopped Mycat-server. Starting Mycat-server... [root@db01 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; +----------+ | DATABASE | +----------+ | TESTDB | +----------+ 1 row in set (0.03 sec)
mysql> 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> show tables; +------------------+ | Tables_in_taobao | +------------------+ | order_t | | user | +------------------+ 2 rows in set (0.02 sec)
mysql>
|