以下是基本架构图,具体如下:
一、配置MySQL主从,详情请查看(搭建MySQL 主从同步 )
二、安装mycat
1、配置java环境
[root@mysql ~]# tar -xf jdk-8u77-linux-x64.gz
[root@mysql ~]# mkdir /usr/local/java
[root@mysql ~]# mv jdk1.8.0_77/ /usr/local/java/
[root@mysql ~]#vim /etc/profile
export JAVA_HOME=/usr/local/java/jdk1.8.0_77
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
[root@mysql ~]#source /etc/profile
[root@mysql ~]# java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)
说明此时Java环境已经部署成功
2、安装mycat
下载mycat包并安装(http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz)
[root@mysql ~]# wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@mysql ~]# tar -xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@mysql ~]# mv mycat/ /usr/local/
[root@mysql mycat]# cd /usr/local/mycat/
[root@mysql mycat]# vim /etc/profile
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin
[root@mysql mycat]# source /etc/profile
此时安装完毕
3、配置mycat
[root@mysql mycat]# cd conf/
修改schema.xml文件
[root@mysql mycat]# vim schema.xml
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />
<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3" rule="mod-long" />
<table name="employee" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" />
<table name="customer" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile">
<childTable name="orders" primaryKey="ID" joinKey="customer_id" parentKey="id">
<childTable name="order_items" joinKey="order_id" parentKey="id" />
</childTable>
<childTable name="customer_addr" primaryKey="ID" joinKey="customer_id" parentKey="id" />
</table> -->
</schema>
<dataNode name="mysql_first" dataHost="dthost1" database="liuys" />
<!-- <dataNode name="liuys-dn2" dataHost="dbhost2" database="liuys" />
<dataNode name="dn3" dataHost="localhost1" database="db3" /> -->
<dataHost name="dthost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user();</heartbeat>
<writeHost host="hostM1" url="192.168.1.128:3306" user="root" password="123456">
<readHost host="hostS1" url="192.168.1.127:3306" user="root" password="123456" />
</writeHost>
<!--<writeHost host="hostS1" url="localhost:3316" user="root" password="123456" /> -->
</dataHost>
</mycat:schema>
<!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="processorBufferPoolType">0</property>
<property name="handleDistributedTransactions">0</property>
<property name="useOffHeapForMerge">1</property>
<property name="useZKSwitch">true</property>
-->
<user name="root">
<property name="password">123456</property>
<property name="schemas">TESTDB</property>
<property name="password">456</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
[root@mysql conf]# netstat -anptu |grep java
tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 3063/java
tcp 0 0 :::42155 :::* LISTEN 3063/java
tcp 0 0 :::34457 :::* LISTEN 3063/java
tcp 0 0 :::1984 :::* LISTEN 3063/java
tcp 0 0 :::8066 :::* LISTEN 3063/java
tcp 0 0 :::9066 :::* LISTEN 3063/java
tcp 0 0 192.168.1.76:36158 192.168.1.127:3306 ESTABLISHED 3063/java
tcp 0 0 192.168.1.76:35258 192.168.1.128:3306 ESTABLISHED 3063/java
设置验证环境并验证
(1)、登陆mycat,建表并插入数据
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
affiliates. Other names may be trademarks of their respective
owners.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(2000002, 'alex', '2018-06-08',500.0,3);
Query OK, 1 row affected (0.42 sec)
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
1 rows in set (0.01 sec)
(2)、登陆主数据库服务器并验证刚插入的数据
[root@localhost ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 5.6.39-log Source distribution
affiliates. Other names may be trademarks of their respective
owners.
You can turn off this feature to get a quicker startup with -A
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
1 rows in set (0.00 sec)
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.6.39-log Source distribution
affiliates. Other names may be trademarks of their respective
owners.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> select * from travelrecord;
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
1 rows in set (0.00 sec)
Query OK, 1 row affected (0.42 sec)
mysql> select * from travelrecord;
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000001 | alice | 2017-08-08 | 500 | 3 |
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
回到主服务器查看时,肯定无法查到该条数据
此时为主服务器
mysql> select * from travelrecord;
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
1 rows in set (0.00 sec)
当我们回到mycat服务器时,又可以看到数据
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
affiliates. Other names may be trademarks of their respective
owners.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> select * from travelrecord;
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000001 | alice | 2017-08-08 | 500 | 3 |
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
3 rows in set (0.01 sec)
(4)、当停止从服务器时
[root@localhost ~]# /etc/init.d/mysqld stop
Shutting down MySQL.... SUCCESS!
我们回到mycat服务器时此时数据就缺失了从服务器上刚插入的那条数据
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
affiliates. Other names may be trademarks of their respective
owners.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> select * from travelrecord;
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
1 rows in set (0.01 sec)
此时说明数据只能在从服务器上读取
(4)、相反当我们停止主服务器确报错无法连接数据库
此时说明主数据库是不能停
至此mycat读学分离已部署完毕