Written at the very beginning: The experiment requires a mycat server, a master server, and a slave server, and the separation of master and slave is required. (If you don't use master-slave replication, you can check the previously published blog by yourself)
1. Read-write separation is based on master-slave replication, so you need to perform read-write separation after master-slave replication.
2. Install the java environment:
[root@mycat tools]# ls
jdk-8u60-linux-x64.tar.gz
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@mycat tools]# tar xf jdk-8u60-linux-x64.tar.gz
[root@mycat tools]# ls
jdk1.8.0_60
jdk-8u60-linux-x64.tar.gz
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@mycat tools]# mv jdk1.8.0_60/ /usr/local/java8
[root@mycat tools]# cd /usr/local/java8/
[root@mycat ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java8
export CLASSPATH=$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin
[root@mycat ~]# source /etc/profile
##[root@mycat ~]# source /etc/profile.d/java.sh
[root@mycat ~]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
3. Install mycat
[root@mycat tools]# ls
jdk-8u60-linux-x64.tar.gz
Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@mycat tools]# ls
jdk-8u60-linux-x64.tar.gz
mycat
Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@mycat tools]# mv mycat/ /usr/local/
[root@mycat tools]# useradd -M -s /sbin/nologin mycat
[root@mycat tools]# chown -R mycat:mycat /usr/local/mycat/
[root@mycat tools]# vim /etc/profile.d/mycat.sh
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin
[root@mycat tools]# source /etc/profile.d/mycat.sh
[root@mycat ~]# cd /usr/local/mycat/
[root@mycat mycat]# ls
bin catlet conf lib logs version.txt
[root@mycat mycat]# cd conf/
[root@mycat conf]# ls
[root@mycat conf]# vim ./schema.xml
[root@mycat conf]# vim server.xml
<property name="useSqlStat">0</property>
<property name="useGlobleTableCheck">0</property>
<property name="useHandshakeV10">1</property>
<property name="processorBufferPoolType">0</property>
<property name="serverPort">3310</property>
<property name="managerPort">9066</property>
<property name="maxStringLiteralLength">65535</property>
<property name="sequnceHandlerType">0</property>
<property name="backSocketNoDelay">1</property>
<property name="frontSocketNoDelay">1</property>
<property name="processorExecutor">16</property>
<property name="sequnceHandlerType">2</property>
The second user name is changed to "mycat".
4.Master master library authorization
mysql> grant all on *.* to [email protected] identified by '123123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
5. Mycat View
Netstat View port
3310 9066 Port is open The
read-write separation arrangement is successful.
6. Install the database test ( yum -y install mariadb mariadb-server
)
Mycat server:
[root@mycat ~]# mysql -uroot -p123123 -h 127.0.0.1 -P9066
MySQL [(none)]> use HEADSHOW;
Database changed
MySQL [HEADSHOW]>
Then enter 3310 operation (logical database)
[root@mycat ~]# mysql -uroot -p123123 -h 127.0.0.1 -P3310
MySQL [(none)]> show databases;
MySQL [(none)]> use HEADSHOW;
MySQL [HEADSHOW]> show tables;
MySQL [HEADSHOW]> create table abc(id int(2));
MySQL [HEADSHOW]> show tables;
Enter the master main library, and the slave slave library to check whether mycat can connect to the main library and the slave library.
mysql> use test;
mysql> show tables;
7. Disable the test read-write separation from the library
1) Enter the slave library
mysql> stop slave io_thread;
mysql> use test;
mysql> show tables;
mysql> select * from abc;
2) Enter the master main library
mysql> stop slave io_thread;
mysql> use test;
mysql> show tables;
mysql> select * from abc;
3) Enter mycat
MySQL [HEADSHOW]> insert into abc values(2);
MySQL [HEADSHOW]> select * from abc
4) Enter the master main library to see if there is data
mysql> select * from abc;
The test read-write separation operation is successfully realized! ! !
The contents of the configuration file are all as follows:
schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="HEADSHOW" checkSQLschema="false" sqlMaxLimit="100" dataNode='dn1'></schema>
<dataNode name="dn1" dataHost="localhost1" database="test" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="192.168.9.9" url="192.168.9.9:3306" user="root"
password="123123">
<!-- can have multi read hosts -->
<readHost host="192.168.9.10" url="192.168.9.10:3306" user="root" password="123123" />
</writeHost>
<writeHost host="hostS1" url="localhost:3316" user="root"
password="123456" />
<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
</dataHost>
<!--
<dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc">
<heartbeat> </heartbeat>
<writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng" password="jifeng"></writeHost>
</dataHost>
<dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat>
<connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql>
<writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base" password="123456" > </writeHost> </dataHost>
<dataHost name="jdbchost" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost>
<dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc">
<heartbeat> </heartbeat>
<writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng" password="jifeng"></writeHost> </dataHost> -->
<!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql"
dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1"
url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost>
</dataHost> -->
</mycat:schema>
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="useSqlStat">0</property>
<property name="useGlobleTableCheck">0</property>
<property name="useHandshakeV10">1</property>
<property name="processorBufferPoolType">0</property>
<property name="serverPort">3310</property>
<property name="managerPort">9066</property>
<property name="maxStringLiteralLength">65535</property>
<property name="sequnceHandlerType">0</property>
<property name="backSocketNoDelay">1</property>
<property name="frontSocketNoDelay">1</property>
<property name="processorExecutor">16</property>
<property name="sequnceHandlerType">2</property>
<!--
off heap for merge/order/group/limit 1开启 0关闭
-->
<property name="useOffHeapForMerge">1</property>
<!--
单位为m
-->
<property name="memoryPageSize">1m</property>
<!--
单位为k
-->
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput">0</property>
<!--
单位为m
-->
<property name="systemReserveMemorySize">384m</property>
<!--是否采用zookeeper协调切换 -->
<property name="useZKSwitch">true</property>
</system>
<!-- 全局SQL防火墙设置 -->
<!--
<firewall>
<whitehost>
<host host="127.0.0.1" user="mycat"/>
<host host="127.0.0.2" user="mycat"/>
</whitehost>
<blacklist check="false">
</blacklist>
</firewall>
-->
<user name="root">
<property name="password">123123</property>
<property name="schemas">HEADSHOW</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>
<user name="mycat">
<property name="password">123123</property>
<property name="schemas">HEADSHOW</property>
<property name="readOnly">true</property>
</user>
</mycat:server>