mycat/conf
目录下,MyCat核心配置文件:schema.xmlxml version="1.0"?>
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"/>
<dataNode name="dn1" dataHost="localhost1" database="mldn" />
<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="hostM1" url="192.168.1.128:3306" user="root" password="123456">writeHost>
dataHost>
mycat:schema>
dataHost
标签上属性释义:
balance
:负载均衡类型
writeType
:负载均衡类型
dnindex.properties
中switchType
:切换方式
1 . 前提:多台MySQL服务器主机的时间是同步的,否则无法实现主从配置
2 . MASTER修改MySQL配置文件vim /usr/local/mysql/my.cnf
log-bin=mysql-bin-1 : 表示配置同步的bin的文件名称,不同的主从关系组,名称不同
server_id=196 : 表示MySQL服务的编号,这个编号一般取IP的最后一位,也可自定义
nohup mysqld_safe --user=root > /dev/null 2>&1 &
mysql -uroot -p123456
,查看master状态show master stats;
其中
File(tid_set) : 表示主机名称(mysql-bin-1.000001)
Position : 表示同步的节点位置
GRANT REPLICCATION SLAVE,REPLICATION CLIENT ON *.* TO 'sync_user'@'192.168.1.%' IDENTIFIED BY '123456';
flush privileges;
vim /usr/local/mysql/my.cnf
log-bin=mysql-bin-1 : 表示配置同步的bin的文件名称,不同的主从关系组,名称不同
server_id=168 : 表示MySQL服务的编号,这个编号一般取IP的最后一位,也可自定义
rm /usr/local/mysql/data/auto.cnf
nohup mysqld_safe --user=root > /dev/null 2>&1 &
mysql -uroot -p123456
- 若此时已经启动了从主机,必须先停止:
stop slave;
flush logs;
change master to master_host='192.168.1.128',master_user='sync_user',master_password='123456',master_log_file='mysql-bin-1.000001',master_log_pos=435 ;
master_host
表示mast的主机ip
master_user
表示可同步的账号
master_password
表示同步账号的密码
master_log_file
表示MASTERshow master stats;
查看时的File(tid_set)
master_log_pos
表示MASTERshow master stats;
查看时的Position
start slave;
show slave status\G;
Slave_IO_Running:YES
Slave_SQL_Running:Yes
schema.xml
文件vim /usr/local/mycat/conf/schema.xml
xml version="1.0"?>
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"/>
<dataNode name="dn1" dataHost="localhost1" database="test" />
<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="hostMaster" url="192.168.1.196:3306" user="root" password="123456">
<readHost host="hostSlave" url="192.168.1.168:3306" user="root" password="123456"/>
writeHost>
dataHost>
mycat:schema>
balance=3
,设置writeHost与ReadHost共同分担读取:balance=2
/usr/local/mycat/bin/mycat console
vim /usr/local/mycat/conf/schema.xml
xml version="1.0"?>
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB1" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"/>
<schema name="TESTDB2" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn2"/>
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost2" database="db2" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host1" url="192.168.1.199:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host2" url="192.168.1.166:3306" user="root" password="123456"/>
dataHost>
mycat:schema>
server.xml
中vim /usr/local/mycat/conf/server.xml
<property name="schemas">TESTDB1,TESTDB2property>
/usr/local/mycat/bin/mycat console
vim /usr/local/mycat/conf/schema.xml
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="dict" primaryKey="did" type="global" dataNode="dn1,dn2"/>
schema>
<schema name="TESTDB1" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"/>
<schema name="TESTDB2" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn2"/>
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost2" database="db2" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host1" url="192.168.1.199:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host2" url="192.168.1.166:3306" user="root" password="123456"/>
dataHost>
mycat:schema>
vim /usr/local/mycat/conf/server.xml
,将schema.xml
配置的所有逻辑数据库,配置到该配置文件中<property name="schemas">TESTDB1,TESTDB2,TESTDBproperty>
/usr/local/mycat/bin/mycat console
/usr/local/mycat/conf/rule.xml
中,目前常用的分片规则有:
- 求模分库:mod-long
vim /usr/local/mycat/conf/rule.xml
rule.xml
的配置<tableRule name="my-mod-long">
<rule>
<columns>idcolumns>
<algorithm>mod-longalgorithm>
rule>
tableRule>
<function name="my-mod-long" class="io.mycat.route.function.PartitionByMod">
<property name="count">3property>
function>
schema.xml
配置
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="data" primaryKey="id" dataNode="dn1,dn2,dn3" rule="my-mod-long"/>
schema>
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost2" database="db2" />
<dataNode name="dn3" dataHost="localhost3" database="db3" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host1" url="192.168.1.199:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host2" url="192.168.1.166:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost3" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host3" url="192.168.1.176:3306" user="root" password="123456"/>
dataHost>
mycat:schema>
server.xml
配置<property name="schemas">TESTDBproperty>
/usr/local/mycat/bin/mycat console
mysql -uroot -p123456 -h192.168.1.1 -P8066 -DTESTDB
- 序列的配置,在
usr/local/mycat/conf/sequence_conf.properties
文件中- 还需要在
server.xml
文件中配置
0 - 在SQL中使用
1187287110722392064
表示序列增长INSERT INTO data (id,title) VALUES ( 1187287110726586368,@@hostname)
;
rule.xml
的配置<tableRule name="my-auto-sharding-long">
<rule>
<columns>idcolumns>
<algorithm>my-auto-sharding-longalgorithm>
rule>
tableRule>
<function name="my-auto-sharding-long" class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">partition-long.txtproperty>
function>
- 此时需要定义一个范围分库的规则问津`partition-long.txt`,并存放在`conf`目录中`vim /usr/local/mycat/conf/partition-long.txt`
- partition-long.txt
根据数据编号划分
0-10000=0
10001-20000=1
20001-30000=2
- 修改`schema.xml`匹配新的分片规则
<table name="data" primaryKey="id" dataNode="dn1,dn2,dn3" rule="my-auto-sharding-long"/>
rule.xml
配置<tableRule name="my-by-intfile">
<rule>
<columns>titlecolumns>
<algorithm>my-hash-intalgorithm>
rule>
tableRule>
<function name="my-hash-int" class="io.mycat.route.function.PartitionByFileMap">
< property name="type">1property>
<property name="mapFile">partition-hash-int.txtproperty>
function>
- `schema.xml`配置
<table name="data" primaryKey="title" dataNode="dn1,dn2,dn3" rule="my-by-intfile"/>
rule.xml
配置<tableRule name="my-by-month">
<rule>
<columns>saledatecolumns>
<algorithm>my-partbymonthalgorithm>
rule>
tableRule>
<function name="my-partbymonth" class="io.mycat.route.function.PartitionByMonth">
< property name="dateFormat">yyyy-MM-ddproperty>
<property name="sBeginDate">2017-01-01property>
function>
- `schema.xml`配置
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="data" primaryKey="id" dataNode="dn$1-12" rule="my-by-month"/>
schema>
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost2" database="db2" />
<dataNode name="dn3" dataHost="localhost3" database="db3" />
<dataNode name="dn4" dataHost="localhost4" database="db4" />
<dataNode name="dn5" dataHost="localhost5" database="db5" />
<dataNode name="dn6" dataHost="localhost6" database="db6" />
<dataNode name="dn7" dataHost="localhost7" database="db7" />
<dataNode name="dn8" dataHost="localhost8" database="db8" />
<dataNode name="dn9" dataHost="localhost9" database="db9" />
<dataNode name="dn10" dataHost="localhost10" database="db10" />
<dataNode name="dn11" dataHost="localhost11" database="db11" />
<dataNode name="dn12" dataHost="localhost12" database="db12" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host1" url="192.168.1.166:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host2" url="192.168.1.199:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost3" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host3" url="192.168.1.116:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost4" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host4" url="192.168.1.150:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost5" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host5" url="192.168.1.152:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost6" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host6" url="192.168.1.177:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost7" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host7" url="192.168.1.136:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost8" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host8" url="192.168.1.153:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost9" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host9" url="192.168.1.159:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost10" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host10" url="192.168.1.176:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost11" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host11" url="192.168.1.156:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost12" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host12" url="192.168.1.136:3306" user="root" password="123456"/>
dataHost>
mycat:schema>
schema.xml
配置
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="data" primaryKey="id" dataNode="dn$1-3" rule="my-mod-long">
<childTable name="data_details" joinKey="id" primaryKey="ddid" parentKey="id"/>
table>
schema>
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost2" database="db2" />
<dataNode name="dn3" dataHost="localhost3" database="db3" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host1" url="192.168.122.166:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host2" url="192.168.122.199:3306" user="root" password="123456"/>
dataHost>
<dataHost name="localhost3" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="host3" url="192.168.122.198:3306" user="root" password="123456"/>
dataHost>
mycat:schema>
- 官方案例分析: