mycat的安装和使用
程序员文章站
2022-03-20 13:29:14
...
Mycat下载
http://www.mycat.org.cn/ 选择下载-> Mycat-server-1.6.7.1-release-20200209222254-linux.tar.gz
Mycat安装和运行
mycat依赖jdk,首先安装jdk:yum install java-1.8.0-openjdk.x86_64 -y
安装Mycat:将下载下来的tar.gz包上传到服务上,运行tar -xvf Mycat-server-1.6.7.1-release-20200209222254-linux.tar.gz
运行Mycat:cd到mycat的bin目录运行:./mycat start
停止Mycat:./mycat stop
查看运行情况
mycat启动日志:
cat ./logs/wrapper.log
mycat 应用日志
cat ./logs/mycat.log
Mycat配置
server.xml
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">test</property>
</user>
root:对应逻辑数据库的用户名 123456:对应逻辑数据库的密码 test:对应逻辑数据库的数据库
schema.xml
<schema name="test" checkSQLschema="false" sqlMaxLimit="100">
<table name="t_user" dataNode="dn1,dn2" rule="mod-long" />
<table name="t_a" dataNode="dn1" />
<table name="t_record" subTables="test.t_record_$0-1" dataNode="dn1" rule="mod-long" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost2" database="db2" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="postgresql" dbDriver="jdbc" switchType="1" slaveThreshold="100">
<heartbeat>select now()</heartbeat>
<writeHost host="hostM1" url="jdbc:postgresql://domain1:1921/db1" user="xxxx" password="xxx">
<readHost host="hostS2" url="jdbc:postgresql://domain1:1921/db1" user="xxx" password="xxx" />
</writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="postgresql" dbDriver="jdbc" switchType="1" slaveThreshold="100">
<heartbeat>select now()</heartbeat>
<writeHost host="hostM1" url="jdbc:postgresql://domain2:1921/db2" user="xxx" password="xxx">
<readHost host="hostS2" url="jdbc:postgresql://domain2:1921/db2" user="xxx" password="xxx" />
</writeHost>
</dataHost>
单库分表通过subTables关键字实现
多库分表通过dataNode关键字实现
如果一个表只有一个库有,只配置都有的数据库节点即可
rule.xml
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property>
</function>
mod-long的数量对应当前表配置的分库分表的数量
补充说明
Mycat的逻辑表是不需要创建的,配置好了之后就自动有了
如果是postgresql,需要将本地的postgresql-42.2.5.jar上传到mycat的lib目录下,否则会找不到驱动包
java代码实现
spring.datasource.url=jdbc:mysql://domain:8066/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&allowPublicKeyRetrieval=true&serverTimezone=CTT
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver