MySQL 基于Mycat实现读写分离
前言
本环境是基于 Centos 7.8 系统构建MySQL-5.7.14
具体构建,请参考 MySQL-5.7.14 环境构建
在实际的生产环境中,对数据库的读和写都在同一个数据库服务器中,是不能满足实际需求的。无论是在安全性、高可用性还是高并发等各个方面都是完全不能满足实际需求的。因此,通过主从复制的方式来同步数据,再通过读写分离来提升数据库的并发负载能力。有点类似于前面我们学习过的rsync,但是不同的是rsync是对磁盘文件做备份,而mysql主从复制是对数据库中的数据、语句做备份。
一、什么是读写分离?
读写分离的原理
读写分离,基本的原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。一般来说都是通过 主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy)来提升数据库的并发负载能力 这样的方案来进行部署与实施的。
读写分离与主从同步
如何实现mysql的读写分离?
就是基于主从复制架构,简单来说,就搞一个主库,挂多个从库,然后我们就单单只是写主库,然后主库会自动把数据给同步到从库上去。
MySQL主从复制原理的是啥?
主库将变更写binlog日志,然后从库连接到主库之后,从库有一个IO线程,将主库的binlog日志拷贝到自己本地,写入一个中继日志中。
接着从库中有一个SQL线程会从中继日志读取binlog,然后执行binlog日志中的内容,也就是在自己本地再次执行一遍SQL,这样就可以保证自己跟主库的数据是一样的。
这里有一个非常重要的一点,就是从库同步主库数据的过程是串行化的,也就是说主库上并行的操作,在从库上会串行执行。
由于从库从主库拷贝日志以及串行执行SQL的特点,在高并发场景下,从库的数据一定会比主库慢一些,是有延时的。
所以经常出现,刚写入主库的数据可能是读不到的,要过几十毫秒,甚至几百毫秒才能读取到。
而且这里还有另外一个问题,就是如果主库突然宕机,然后恰好数据还没同步到从库,那么有些数据可能在从库上是没有的,有些数据可能就丢失了。
所以mysql实际上在这一块有两个机制,一个是半同步复制,用来解决主库数据丢失问题;一个是并行复制,用来解决主从同步延时问题。
这个所谓半同步复制,semi-sync复制,指的就是主库写入binlog日志之后,就会将强制此时立即将数据同步到从库,从库将日志写入自己本地的relay log之后,接着会返回一个ack给主库,主库接收到至少一个从库的ack之后才会认为写操作完成了。
所谓并行复制,指的是从库开启多个线程,并行读取relay log中不同库的日志,然后并行重放不同库的日志,这是库级别的并行。
为什么需要读写分离
因为数据库的“写”(写10000条数据到MySQL可能要3分钟)操作是比较耗时的。
但是数据库的“读”(从MySQL读10000条数据可能只要5秒钟)。
所以读写分离,解决的是,数据库的写入,影响了查询的效率。
什么时候需要读写分离
大型网站为了解决大量的并发访问,除了在网站实现分布式负载均衡,远远不够。到了数据业务层、数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器来处理如此多的数据库连接操作,数据库必然会崩溃,特别是数据丢失的话,后果更是不堪设想。这时候,我们会考虑如何减少数据库的连接,下面就进入我们今天的主题。
利用主从数据库来实现读写分离,从而分担主数据库的压力。在多个服务器上部署mysql,将其中一台认为主数据库,而其他为从数据库,实现主从同步。其中主数据库负责主动写的操作,而从数据库则只负责主动读的操作(slave从数据库仍然会被动的进行写操作,为了保持数据一致性),这样就可以很大程度上的避免数据丢失的问题,同时也可减少数据库的连接,减轻主数据库的负载。
二、MySQL 读写分离解决方案
- 360 Atlas-Sharding
- Alibaba cobar
- Mycat
- TDDL
- Heisenberg
- Oceanus
- Vitess
- OneProxy
- DRDS
三、MySQL 基于Mycat实现读写分离
mycat 架构
mycat 高可用架构
部署jdk环境
注:Mycat用Java开发,需要有JAVA运行环境,mycat依赖jdk1.7的环境
[root@mysql-rpm ~]# ll jdk-8u144-linux-x64.rpm
-rw-r--r-- 1 root root 169971490 Feb 6 22:07 jdk-8u144-linux-x64.rpm
[root@mysql-rpm ~]# yum install jdk-8u144-linux-x64.rpm -y
[root@mysql-rpm ~]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
部署Mycat
# 安装mycat
[root@mysql-rpm ~]# ll Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
-rw-r--r-- 1 root root 15662280 Jan 21 21:46 Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@mysql-rpm ~]# tar xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mysql-rpm ~]# ll /usr/local/mycat/
total 12
drwxr-xr-x 2 root root 190 Feb 6 22:15 bin
drwxrwxrwx 2 root root 6 Mar 1 2016 catlet
drwxrwxrwx 4 root root 4096 Feb 6 22:15 conf
drwxr-xr-x 2 root root 4096 Feb 6 22:15 lib
drwxrwxrwx 2 root root 6 Oct 28 2016 logs
-rwxrwxrwx 1 root root 217 Oct 28 2016 version.txt
# 配置mycat环境变量
[root@mysql-rpm ~]# vim /etc/profile.d/mycat.sh
export PATH=$PATH:/usr/local/mycat/bin
[root@mysql-rpm ~]# source /etc/profile.d/mycat.sh
读写分离配置
[root@mysql-rpm conf]# vim schema.xml
<?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="db1" />
<dataHost name="localhost1" 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.5.11:3306" user="mycat"
password="123456">
<readHost host="hostS2" url="192.168.5.14:3306" user="mycat" password="123456" />
<readHost host="hostS2" url="192.168.5.14:3307" user="mycat" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
创建管理用户
# 主库
grant create,insert,delete,update,select on db1.* to 'mycat'@'192.168.5.%' identified by '123456';
# 3306
grant select on db1.* to 'mycat'@'192.168.5.%' identified by '123456';
# 3307
grant select on db1.* to 'mycat'@'192.168.5.%' identified by '123456';
启动mycat
# 启动mycat
[root@mysql-rpm conf]# mycat console
或者
[root@mysql-rpm conf]# mycat start
# 查看mycat端口
[root@mysql-rpm conf]# netstat -lnutp | egrep '[89]066'
tcp6 0 0 :::8066 :::* LISTEN 15566/java
tcp6 0 0 :::9066 :::* LISTEN 15566/java
# 查看mycat进程
[root@mysql-rpm conf]# mycat status
Mycat-server is running (15564).
查看mycat是否部署成功
# 登录mycat
[root@rhel7 ~]# mysql -uroot -p123456 -h192.168.5.12 -P8066 -DTESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [TESTDB]>
# 查看mycat数据库列表
MySQL [TESTDB]> show @@databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.01 sec)
# 查看mycat数据节点信息
MySQL [TESTDB]> show @@datanode;
+------+----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| NAME | DATHOST | INDEX | TYPE | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+------+----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| dn1 | localhost1/db1 | 0 | mysql | 0 | 10 | 1000 | 169 | 0 | 0 | 0 | -1 |
+------+----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
1 row in set (0.00 sec)
查看mycat的heartbeat状况
mycat 部署成功!!!
登录mycat执行操作
[root@rhel7 ~]# mysql -uroot -p123456 -h192.168.5.12 -P8066 -DTESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [TESTDB]>
MySQL [TESTDB]>
MySQL [TESTDB]> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.01 sec)
MySQL [TESTDB]> use TESTDB
Database changed
MySQL [TESTDB]> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| course |
| student |
+---------------+
2 rows in set (0.01 sec)
MySQL [TESTDB]> select * from student;
+--------------+-----------+---------+---------+-----------+--------------+
| stu_id | stu_name | stu_sex | stu_age | stu_major | stu_college |
+--------------+-----------+---------+---------+-----------+--------------+
| 201804550101 | 郭奎 | 男 | 22 | 计科 | 信工学院 |
| 201804550102 | 吕宇航 | 男 | 18 | 计科 | 信工学院 |
| 201804550103 | 张豪辉 | 女 | 19 | 计科 | 信工学院 |
| 201804550107 | 丁志杰 | 男 | 17 | 金融学 | 金贸学院 |
| 201804550109 | 范伟 | 男 | 19 | 金融学 | 金贸学院 |
| 201804550116 | 张依婷 | 女 | 17 | 大数据 | 信工学院 |
| 201804550120 | 张维 | 男 | 19 | 计科 | 信工学院 |
| 201804550121 | 朱柳阳 | 女 | 20 | 计科 | 信工学院 |
| 201804550144 | 谭兵炎 | 男 | 20 | 大数据 | 信工学院 |
| 201804550153 | 杨志强 | 男 | 17 | 大数据 | 信工学院 |
+--------------+-----------+---------+---------+-----------+--------------+
10 rows in set (0.57 sec)
MySQL [TESTDB]> select * from course;;
+----------+--------------------------+-----------+--------------+-----------+
| cs_id | cs_name | cs_credit | cs_type | cs_depart |
+----------+--------------------------+-----------+--------------+-----------+
| 5200313 | 数据库原理及应用 | 4 | 核心专业 | 信工 |
| 5203314 | 计算机导论 | 4 | 通识教育 | 信工 |
| 5219314 | 数据结构 | 5 | 专业核心 | 信工 |
| 5223013 | 大学物理 | 4 | 专业基础 | 信工 |
| 5227614 | 毕业实习 | 4 | 集中实践 | 信工 |
| 5230912 | 云计算 | 2 | 共同选修 | 信工 |
| 5236212 | 机器学习 | 2 | 共同选修 | 信工 |
| 5237514 | c语言 | 4 | 专业基础 | 信工 |
| 5245112 | 区块链 | 2 | 任意选修 | 信工 |
| 7200422 | 知识产权法 | 2 | 任意选修 | 文法 |
| 20201833 | 概率论 | 3 | 专业基础 | 基础 |
| 20202336 | 高等数学 | 6 | 专业基础 | 基础 |
| 29299131 | 劳动教育 | 1 | 集中实践 | 学务 |
+----------+--------------------------+-----------+--------------+-----------+
13 rows in set (0.11 sec)
MySQL [TESTDB]> select * from course;
+----------+--------------------------+-----------+--------------+-----------+
| cs_id | cs_name | cs_credit | cs_type | cs_depart |
+----------+--------------------------+-----------+--------------+-----------+
| 5200313 | 数据库原理及应用 | 4 | 核心专业 | 信工 |
| 5203314 | 计算机导论 | 4 | 通识教育 | 信工 |
| 5219314 | 数据结构 | 5 | 专业核心 | 信工 |
| 5223013 | 大学物理 | 4 | 专业基础 | 信工 |
| 5227614 | 毕业实习 | 4 | 集中实践 | 信工 |
| 5230912 | 云计算 | 2 | 共同选修 | 信工 |
| 5236212 | 机器学习 | 2 | 共同选修 | 信工 |
| 5237514 | c语言 | 4 | 专业基础 | 信工 |
| 5245112 | 区块链 | 2 | 任意选修 | 信工 |
| 7200422 | 知识产权法 | 2 | 任意选修 | 文法 |
| 20201833 | 概率论 | 3 | 专业基础 | 基础 |
| 20202336 | 高等数学 | 6 | 专业基础 | 基础 |
| 29299131 | 劳动教育 | 1 | 集中实践 | 学务 |
+----------+--------------------------+-----------+--------------+-----------+
13 rows in set (0.00 sec)
登录mycat 查看信息
查看mycat 附属信息
# 查看mycat当前版本
MySQL [TESTDB]> show @@version;
+-----------------------------------------+
| VERSION |
+-----------------------------------------+
| 5.6.29-mycat-1.6-RELEASE-20161028204710 |
+-----------------------------------------+
1 row in set (0.00 sec)
# 查看mycat前端连接状况
MySQL [TESTDB]> show @@connection;
+------------+------+---------------+------+------------+------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
| PROCESSOR | ID | HOST | PORT | LOCAL_PORT | USER | SCHEMA | CHARSET | NET_IN | NET_OUT | ALIVE_TIME(S) | RECV_BUFFER | SEND_QUEUE | txlevel | autocommit |
+------------+------+---------------+------+------------+------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
| Processor1 | 3 | 192.168.5.150 | 9066 | 41519 | user | TESTDB | utf8:33 | 284 | 2049 | 437 | 4096 | 0 | | |
| Processor3 | 1 | 192.168.5.11 | 9066 | 59596 | user | TESTDB | utf8:33 | 141 | 295 | 460 | 4096 | 0 | | |
+------------+------+---------------+------+------------+------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
2 rows in set (0.00 sec)
# 查看mycat后端连接状况
MySQL [TESTDB]> show @@backend;
+------------+------+---------+--------------+------+--------+--------+---------+------+--------+----------+------------+--------+----------+---------+------------+
| processor | id | mysqlId | host | port | l_port | net_in | net_out | life | closed | borrowed | SEND_QUEUE | schema | charset | txlevel | autocommit |
+------------+------+---------+--------------+------+--------+--------+---------+------+--------+----------+------------+--------+----------+---------+------------+
| Processor0 | 16 | 12 | 192.168.5.14 | 3306 | 43466 | 2741 | 679 | 1936 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor0 | 4 | 23 | 192.168.5.11 | 3306 | 40298 | 1653 | 427 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor0 | 20 | 13 | 192.168.5.14 | 3307 | 33278 | 1883 | 481 | 1636 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor0 | 8 | 18 | 192.168.5.11 | 3306 | 40286 | 1653 | 427 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor0 | 24 | 16 | 192.168.5.14 | 3306 | 43482 | 1103 | 301 | 1036 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor0 | 12 | 9 | 192.168.5.14 | 3307 | 33262 | 2741 | 679 | 1936 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor1 | 1 | 17 | 192.168.5.11 | 3306 | 40284 | 1653 | 427 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor1 | 17 | 13 | 192.168.5.14 | 3306 | 43468 | 1961 | 499 | 1636 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor1 | 5 | 19 | 192.168.5.11 | 3306 | 40288 | 1575 | 409 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor1 | 21 | 15 | 192.168.5.14 | 3306 | 43476 | 1493 | 391 | 1336 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor1 | 9 | 21 | 192.168.5.11 | 3306 | 40292 | 1575 | 409 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor1 | 25 | 16 | 192.168.5.14 | 3307 | 33288 | 1025 | 283 | 1036 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor1 | 13 | 10 | 192.168.5.14 | 3307 | 33264 | 2741 | 679 | 1936 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor2 | 2 | 24 | 192.168.5.11 | 3306 | 40296 | 1575 | 409 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor2 | 18 | 14 | 192.168.5.14 | 3306 | 43470 | 1961 | 499 | 1636 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor2 | 6 | 20 | 192.168.5.11 | 3306 | 40290 | 1575 | 409 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor2 | 22 | 14 | 192.168.5.14 | 3307 | 33282 | 1415 | 373 | 1336 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor2 | 10 | 22 | 192.168.5.11 | 3306 | 40294 | 1575 | 409 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor2 | 26 | 17 | 192.168.5.14 | 3306 | 43486 | 713 | 211 | 736 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor2 | 27 | 18 | 192.168.5.14 | 3306 | 43490 | 401 | 139 | 436 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor2 | 14 | 11 | 192.168.5.14 | 3307 | 33266 | 2741 | 679 | 1936 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor3 | 3 | 16 | 192.168.5.11 | 3306 | 40282 | 1575 | 409 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor3 | 19 | 12 | 192.168.5.14 | 3307 | 33276 | 1883 | 481 | 1636 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor3 | 7 | 15 | 192.168.5.11 | 3306 | 40280 | 1653 | 427 | 1936 | false | false | 0 | db1 | utf8:33 | 3 | true |
| Processor3 | 23 | 15 | 192.168.5.14 | 3307 | 33284 | 1415 | 373 | 1336 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor3 | 11 | 10 | 192.168.5.14 | 3306 | 43456 | 2819 | 697 | 1936 | false | false | 0 | db1 | latin1:5 | 3 | true |
| Processor3 | 15 | 11 | 192.168.5.14 | 3306 | 43464 | 2741 | 679 | 1936 | false | false | 0 | db1 | latin1:5 | 3 | true |
+------------+------+---------+--------------+------+--------+--------+---------+------+--------+----------+------------+--------+----------+---------+------------+
27 rows in set (0.00 sec)
# 显示数据源
MySQL [TESTDB]> show @@datasource;
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| dn1 | hostM1 | mysql | 192.168.5.11 | 3306 | W | 0 | 10 | 1000 | 206 | 0 | 0 |
| dn1 | hostS2 | mysql | 192.168.5.14 | 3306 | R | 0 | 9 | 1000 | 205 | 0 | 0 |
| dn1 | hostS2 | mysql | 192.168.5.14 | 3307 | R | 0 | 8 | 1000 | 204 | 0 | 0 |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
3 rows in set (0.00 sec)
测试MySQL 读写分离、负载均衡效果
登录mycat执行命令端,创建test1数据库
[root@rhel7 ~]# mysql -uroot -p123456 -h192.168.5.12 -P8066 -DTESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB 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, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [TESTDB]>
MySQL [TESTDB]> CREATE TABLE test1 (id int(10),name varchar(10),address varchar(20) DEFAULT NULL);
Query OK, 0 rows affected (0.01 sec)
MySQL [TESTDB]> desc test1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.13 sec)
关闭两个从数据库的主从同步功能
--- 3306
[root@mysql-source_code ~]# mysql -uroot -p123456 -S /mysql/3306/mysql.sock
mysql: [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 15
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, 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> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
--- 3307
[root@mysql-source_code ~]# mysql -uroot -p123456 -S /mysql/3307/mysql.sock
mysql: [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 15
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, 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> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
主库、从库分别输入数据
--- 主库
mysql> use db1
Database changed
mysql> select * from test1;
Empty set (0.00 sec)
mysql> insert into test1 values(1,'test1','master');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 1 | test1 | master |
+------+-------+---------+
1 row in set (0.00 sec)
--- 3306
mysql> use db1
Database changed
mysql> select * from test1;
Empty set (0.00 sec)
mysql> insert into test1 values(2,'test1','slave1');
Query OK, 1 row affected (0.01 sec)
mysql> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 2 | test1 | slave1 |
+------+-------+---------+
1 row in set (0.00 sec)
--- 3307
mysql> use db1
Database changed
mysql> select * from test1;
Empty set (0.00 sec)
mysql> insert into test1 values(3,'test1','slave2');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 3 | test1 | slave2 |
+------+-------+---------+
1 row in set (0.00 sec)
登录mycat 8066 端口,查看test1表中的数据
mycat 已经实现从库负载均衡
mycat 插入第四条数据
MySQL [TESTDB]> insert into test1 values(4,'test1','master');
Query OK, 1 row affected (0.00 sec)
MySQL [TESTDB]> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 3 | test1 | slave2 |
+------+-------+---------+
1 row in set (0.01 sec)
MySQL [TESTDB]> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 2 | test1 | slave1 |
+------+-------+---------+
1 row in set (0.01 sec)
# 当两个salve 开启主从复制后,mycat 再次查询
MySQL [TESTDB]> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 2 | test1 | slave1 |
| 1 | test1 | master |
| 4 | test1 | master |
+------+-------+---------+
3 rows in set (0.00 sec)
MySQL [TESTDB]> select * from test1;
+------+-------+---------+
| id | name | address |
+------+-------+---------+
| 3 | test1 | slave2 |
| 1 | test1 | master |
| 4 | test1 | master |
+------+-------+---------+
3 rows in set (0.00 sec)
mycat 读写分离功能测试成功!!!
四、Mycat-web 管理部署
mycat部署依赖于zookeeper,所以部署mycat前,先要zookeeper部署
部署zookeeper
# 上传zookeeper
[root@mysql-rpm ~]# ll zookeeper-3.4.9.tar.gz Mycat-web-1.0-SNAPSHOT-20160617163048-linux\ .tar.gz
-rw-r--r-- 1 root root 53956321 Feb 7 00:47 Mycat-web-1.0-SNAPSHOT-20160617163048-linux .tar.gz
-rw-r--r-- 1 root root 22724574 Feb 7 00:47 zookeeper-3.4.9.tar.gz
# 解压zookeeper
[root@mysql-rpm ~]# tar xf zookeeper-3.4.9.tar.gz -C /usr/local/src/
# 进入安装目录,拷贝配置文件
[root@mysql-rpm ~]# cd /usr/local/src/zookeeper-3.4.9/conf
[root@mysql-rpm conf]# cp zoo_sample.cfg zoo.cfg
# 启动zookeeper服务
[root@mysql-rpm conf]# cd ../bin/
[root@mysql-rpm bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/src/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@mysql-rpm bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/src/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: standalone
# 查看zookeeper服务
[root@mysql-rpm bin]# netstat -lnutp | grep 2181
tcp6 0 0 :::2181 :::* LISTEN 20461/java
部署Mycat-web
# 解压Mycat-web
[root@mysql-rpm ~]# tar xf Mycat-web-1.0-SNAPSHOT-20160617163048-linux\ .tar.gz -C /usr/local/src/
# 进入Mycat-web安装目录,启动Mycat-web
[root@mysql-rpm ~]# cd /usr/local/src/mycat-web/
[root@mysql-rpm mycat-web]# nohub ./start.sh &
[1] 20957
[root@mysql-rpm mycat-web]# nohup: ignoring input and appending output to ‘nohup.out’
[root@mysql-rpm mycat-web]# more tail -f nohup.out
# 查看Mycat-web启动状况
[root@mysql-rpm mycat-web]# netstat -lnutp | grep 8082
tcp6 0 0 :::8082 :::* LISTEN 20958/java
web登录Mycat-web
http://192.168.5.12:8082/mycat/
配置Mycat-web
创建配置管理
创建完成
监控功能
mycat监控
SQL监控