linux下mycat快速安装与简单使用

建议学习mycat之前先下载mycat权威指南一书,先整体看一遍。

先去mycat官网:http://dl.mycat.io/下载对应版本的mycat-server,我这里使用的是Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

上传到linux服务器。

解压:tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz 会生成一个mycat文件夹。

这里简单演示一下mycat使用id 取模 的单库 水平分表实例,我们线上大部分是这种分表策列。

修改conf目录下的server.xml, 内容如下:

<?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>
    </system>
	
    <user name="root">
        <property name="password">root</property>
        <property name="schemas">tanlei</property>
    </user>

</mycat:server>
其中name="root" 和password="root"对应mysql实例用户名和密码,schemas对应schema.xml下<schema>的name标签。

修改schema.xml,内容如下:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

	<schema name="tanlei" checkSQLschema="false" sqlMaxLimit="100"> 
		<table name="t_user" subTables="t_user$1-3" dataNode="dn1" rule="mod-long" /> 
	</schema> 
	<dataNode name="dn1" dataHost="localhost1" database="tanleidb" /> 
	<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="localhost:3306" user="root" password="root" /> 
	</dataHost>

</mycat:schema>
修改rule.xml,内容如下
<?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:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
	<tableRule name="mod-long">
		<rule>
			<columns>id</columns>
			<algorithm>mod-long</algorithm>
		</rule>
	</tableRule>
	<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
		<!-- how many data nodes -->
		<property name="count">3</property>
	</function>
</mycat:rule>

进入bin目录下执行: ./mycat start 启动mycat server。

观察logs 目录下 mycat.log和wrapper.log, 没有报错则mycat server启动成功。

我这里在实例下建了一个tanleidb库,分别创建了三张表:

t_user1,t_user2,t_user3,主键为id。

(用navicat链接root,root,hostip,8066端口,然后插入数据:)

执行命令行: mysql -uroot -proot -h127.0.0.1 -P8066 -Dtanlei; 进入mycat客户端。

insert into t_user(id,userName,age,createTime,updateTime) values(5,'tanlei',23,now(),now())

explain
select * from t_user where id = 9

发现id=9(mod=0,进入表1)的数据落入到了t_user1表中。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值