mycat配置MySQL主从读写分离
1.安装java 1.8
mycat 1.6要求的Java需要Java 1.8或1.8以上,安装Java参考以下链接: https://blog.csdn.net/weixin_43893397/article/details/102636437
2.配置主从复制,主库创建远程用户用作数据连接
grant all on *.* to 'root'@'192.168.12.%' identified by 'mysql';
3.上传,解压mycat安装包
4.修改root,mysql用户环境变量
vim /etc/profile export JAVA_HOME=/stage/jdk1.8.0_144 export PATH=/stage/jdk1.8.0_144/bin:$PATH vim /home/mysql/.bash_profile export PATH export JAVA_HOME=/stage/jdk1.8.0_144 export PATH=/usr/local/mysql/bin:$PATH export PATH=/stage/mycat/bin:$PATH export PATH=/stage/jdk1.8.0_144/bin:/stage/mycat/bin:/usr/local/mysql/bin:$PATH
5.修改mycat配置文件
修改server.xml <user name="root" defaultAccount="true"> <property name="password">123456</property> <property name="schemas">csg</property> <property name="defaultSchema">csg</property> <!--No MyCAT Database selected 错误前会尝试使用该schema作为schema,不设置则为null,报错 --> <!-- 表级 DML 权限设置 --> <!-- <privileges check="false"> <schema name="TESTDB" 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">csg</property> <property name="readOnly">true</property> <property name="defaultSchema">csg</property> </user> </mycat:server> 修改schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="csg" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"> </schema> <dataNode name="dn1" dataHost="localhost1" database="csg" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="192.168.12.60:3306" user="root" password="mysql"> <readHost host="readS2" url="192.168.12.3:3306" user="root" password="mysql" /> </writeHost> </dataHost> </mycat:schema>
多个只读库,直接加一列,如: <readHost host="readS3" url="192.168.12.4:3306" user="root" password="mysql" /> <readHost host="readS4" url="192.168.12.5:3306" user="root" password="mysql" />