前提:mycat的读写分离需要mysql的主从配置,否则数据不全有问题
一、首先保证mysql数据库能用navicat连接(开启用户的远程连接)
如果没有开启远程访问需要开启:
步骤如下
1:管理员角色打开cmd
2:定位到解压的mysql的bin文件夹下
3:mysql登录mysql -uroot -p123456
4:use mysql
5:grant all privileges on *.* to root@'%' identified by "password";
6:flush privileges;
7:select host,user from user;
二、下载mycat解压安装。
1:管理员角色打开cmd
2:定位到解压的mycat的bin文件夹下
3:mycat.bat install安装
4:mycat.bat start 启动
5:mycat.bat stop 停止
6:mycat.bat status 查看状态
三、mycat读写分离配置
1:schema.xml文件配置
<!-- 配置一个逻辑库erp-->
<schema name="erp" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
</schema>
<dataNode name="dn1" dataHost="localhost1" database="erp" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.1.149:3306" user="root" password="123456">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.1.158:3306" user="root" password="123456" />
</writeHost>
</dataHost>
2:如果表要进行分片需要引入不同的规则 rule.xml
3:server.xml定义用户配置片段
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">erp</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="erp" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">erp</property>
<property name="readOnly">true</property>
</user>