--mycat1.6.6.1读写分离
--主备库赋予权限
grant all privileges on *.* to root@'%' identified by 'oracle';
flush privileges;
vi /etc/profile
export JAVA_HOME=/usr/java/latest
#export CLASSPATH=.:$JAVA_HOME/lib
#export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export MYCAT_HOME=/opt/mycat
export PATH=$PATH:$MYCAT_HOME/bin
--修改日志为debug:
vim /usr/local/mycat/conf/log4j.xml
<root>
<level value="debug" />
<appender-ref ref="FILE" />
<!--<appender-ref ref="FILE" />-->
</root>
--我们可以使用mysql客户端连接或者navicat来连接mycat
--mysql -uroot -h192.168.92.14 -P8066 -pmysql不能登陆
--修改conf下的配置后可能导致mycat服务起不来
mysql -utest -h192.168.92.14 -P8066 -pmysql
--如果配置文件不正确,则导致:JVM did not exit on request, terminated
--如下配置company表只允许在主库写而不允许读
vi schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="mydb1" checkSQLschema="true" sqlMaxLimit="100">
<table name="t_order" primaryKey="uuid" autoIncrement="true" dataNode="dn1"/>
<table name="t_user" primaryKey="uuid" autoIncrement="true" dataNode="dn2"/>
<table name="company" primaryKey="ID" type="global" dataNode="dn3" />
</schema>
<dataNode name="dn1" dataHost="dhost1" database="orderdb" />
<dataNode name="dn2" dataHost="dhost1" database="userdb" />
<dataNode name="dn3" dataHost="dhost2" database="userdb" />
<dataHost name="dhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="myhostM1" url="192.168.92.14:3306" user="root" password="oracle">
<readHost host="myhostS1" url="192.168.92.16:3306"
user="root" password="oracle" weight="1" />
</writeHost>
</dataHost>
<dataHost name="dhost2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="myhostM2" url="192.168.92.14:3306" user="root" password="oracle"></writeHost>
</dataHost>
</mycat:schema>
vi server.xml 最后的user配置如下,注释掉最后的多余的mycat的schema:TESTDB
<user name="root" defaultAccount="true">
<property name="password">oracle</property>
<property name="schemas">mydb1</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<!-- schema.xml 中已没有TESTDB了,所以注释掉下面的用户 -->
<!--
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
-->
--mycat启动错误
Startup failed: Timed out waiting for a signal from the JVM.
JVM did not exit on request, terminated
解决办法:在wrapper.conf中添加,本例中少wrapper.startup.timeout
wrapper.startup.timeout=300 //超时时间300秒
wrapper.ping.timeout=120
--修改日志模式为debug
vi /opt/mycat/conf/log4j2.xml info修改为debug
<asyncRoot level="debug" includeLocation="true">
<!--<AppenderRef ref="Console" />-->
<AppenderRef ref="RollingFile"/>
</asyncRoot>
--以前的版本参考:
<root>
<level value="debug" />
<appender-ref ref="FILE" />
<!--<appender-ref ref="FILE" />-->
</root>
--运行启动命令 启动mycat参考 startup_nowrap.sh
mycat start &
--navicat通过8066端口连接mycat,如下为命令行方式连接mycat
mysql -uroot -h192.168.92.14 -P8066 -poracle
--查询debug日志,如下为日志中对应的执行的sql和对应的执行节点,也可以根据attachment关键字确定
tail -f /opt/mycat/logs/mycat.log
to send query cmd:
select user()
in pool
DBHostConfig [hostName=myhostM1, url=192.168.92.14:3306]
--navicat测试读写分离
select * from orderdb.t_order;
insert into orderdb.t_order select 16,'user16';
--只能写,读报错
select * from orderdb.company ;
insert into orderdb.company select 6,'company6';
来源:https://www.cnblogs.com/buffercache/p/12446299.html