以下是基本架构图,具体如下:
一、配置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
<!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>show slave status</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="hostM1" url="192.168.1.77:3306" 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>
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%’
强调:
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" 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>show slave status</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.1.90:3306" user="root"
password="123456">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.1.89:3306" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" 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>show slave status</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.1.90:3306" user="root"
password="123456">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.1.89:3306" user="root" password="123456" />
</writeHost>
<writeHost host="hostM2" url="192.168.1.89:3306" user="root"
password="123456"/>
</dataHost>
</mycat:schema>
[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读学分离已部署完毕