以下是基本架构图,具体如下:

一、配置MySQL主从、主主,详情请查看(
MySQL主主同步:https://blog.csdn.net/baidu_38432732/article/details/80773634
MySQL主从同步:https://blog.csdn.net/baidu_38432732/article/details/80653873 )
二、安装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
一下备注部分为分表,因目前没需要所以没有对分表进行配置
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
show slave status
修改
server.xml
[root@mysql conf]# vim server.xml
show slave status 表示一种集群策略,只适用在一主一从的环境中,当主 down 掉, 从可以充当主和从
balance属性
balance=”0”, 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上
balance=”1”,全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡
balance=”2”,所有读操作都随机的在 writeHost、 readhost 上分发。
balance=”3”, 所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力
writeType 属性
负载均衡类型,目前的取值有 3 种:
writeType=”0”, 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost, 重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties
writeType=”1”,所有写操作都随机的发送到配置的 writeHost
writeType="2",不执行写操作
switchType属性
-1 表示不自动切换
1 默认值,自动切换
2 基于MySQL主从同步的状态决定是否切换
3 基于MySQLgalarycluster的切换机制(适合集群)(1.4.1)
心跳语句为show status like‘wsrep%’
强调:
仅仅主从读写分离的配置:
dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
show slave status
password="123456">
主挂掉之后自动切换到从的配置:
dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
show slave status
password="123456">
password="123456"/>
4、启动mycat,并查看是否启动
[root@mysql conf]# /usr/local/mycat/bin/mycat start
[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,建表并插入数据
[root@localhost ~]# mysql -uroot -h192.168.1.76 -P8066 -p
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)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use TESTDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table company(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.01 sec)
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)
mysql> select * from travelrecord;
+---------+---------+------------+------+------+
| 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
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 42
Server version: 5.6.39-log Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from travelrecord;
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
1 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
同理登陆从服务器并在从服务器上插入数据以便验证读写分离
[root@localhost ~]# mysql -uroot -p123456
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
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use liuys;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from travelrecord;
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 2000002 | alex | 2018-06-08 | 500 | 3 |
+---------+---------+------------+------+------+
1 rows in set (0.00 sec)
mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(2000001, 'alice', '2017-08-08',500.0,3);
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服务器时,又可以看到数据
[root@localhost ~]# mysql -uroot -h192.168.1.76 -P8066 -p
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)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use TESTDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
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服务器时此时数据就缺失了从服务器上刚插入的那条数据
[root@localhost ~]# mysql -uroot -h192.168.1.76 -P8066 -p
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)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use TESTDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
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读学分离已部署完毕