一 环境

1.1 结构

192.168.132.125 mycat

192.168.132.121  master

192.168.132.122  slave

主从已经配置完成

1.2 安装mycat

192.168.132.125安装mycat

[root@mycat ~]#   cd /usr/local/src/

[root@mycat src]# wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

[root@mycat src]# tar -xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

[root@mycat src]# cd mycat/

[root@mycat mycat]# ll

drwxr-xr-x.  root root   Jul  : bin
drwxrwxrwx. root root Feb catlet
drwxrwxrwx. root root Jul : conf
drwxr-xr-x. root root Jul : lib
drwxrwxrwx. root root Oct logs
-rwxrwxrwx. root root Oct version.txt

1.3 配置mycat

[root@mycat mycat]# cd conf/

[root@mycat conf]# ll

-rwxrwxrwx.  root root    Oct    autopartition-long.txt
-rwxrwxrwx. root root Oct auto-sharding-long.txt
-rwxrwxrwx. root root Oct auto-sharding-rang-mod.txt
-rwxrwxrwx. root root Oct cacheservice.properties
-rwxrwxrwx. root root Oct ehcache.xml
-rwxrwxrwx. root root Oct index_to_charset.properties
-rwxrwxrwx. root root Oct log4j2.xml
-rwxrwxrwx. root root Oct migrateTables.properties
-rwxrwxrwx. root root Oct myid.properties
-rwxrwxrwx. root root Oct partition-hash-int.txt
-rwxrwxrwx. root root Oct partition-range-mod.txt
-rwxrwxrwx. root root Oct rule.xml
-rwxrwxrwx. root root Oct schema.xml #主要配置这个文件
-rwxrwxrwx. root root Oct sequence_conf.properties
-rwxrwxrwx. root root Oct sequence_db_conf.properties
-rwxrwxrwx. root root Oct sequence_distributed_conf.properties
-rwxrwxrwx. root root Oct sequence_time_conf.properties
-rwxrwxrwx. root root Oct server.xml
-rwxrwxrwx. root root Oct sharding-by-enum.txt
-rwxrwxrwx. root root Oct wrapper.conf
drwxrwxrwx. root root Jul : zkconf
drwxrwxrwx. root root Jul : zkdownload

[root@mycat conf]# cat schema.xml

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!‐‐ TESTDB 表示mycat 的逻辑数据库名称当schema 节点没有子节点table 的时候,一定要有dataNode 属性存在‐‐>
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="" dataNode="dn1">
</schema> <dataNode name="dn1" dataHost="localhost1" database="master1" /> <dataHost name="localhost1" maxCon="" minCon="" balance=""
writeType="" dbType="mysql" dbDriver="native" switchType="" slaveThreshold="">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.132.121:3306" user="root"
password="">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.132.122:3306" user="root" password="" />
</writeHost> </dataHost> </mycat:schema>

注意 dataHost 节点的下面三个属性balance, switchType, writeType

balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost 上。

balance="1",全部的readHost 与stand by writeHost 参与select语句的负载均衡,简单的说,当双主双从模式(M1‐>S1,M2‐>S2,并且M1 与M2 互为主备),正常情况下,M2,S1,S2都参与select 语句的负载均衡。

balance="2",所有读操作都随机的在writeHost、readhost 上分发。

balance="3",所有读请求随机的分发到writeHost 下的readhost 执行,writeHost 不负担读压力

writeType 表示写模式

writeType="0",所有的操作发送到配置的第一个writehost

writeType="1",随机发送到配置的所有writehost

writeType="2",不执行写操作

switchType 指的是切换的模式,目前的取值也有4 种:

switchType=‘‐1‘ 表示不自动切换

switchType=‘1‘ 默认值,表示自动切换

switchType=‘2‘ 基于MySQL 主从同步的状态决定是否切换,心跳语句为show slave status

switchType=‘3‘基于MySQL galary cluster 的切换机制(适合集群)(1.4.1),心跳语句为show status like ‘wsrep%‘。

配置端口,用户名

[root@mycat conf]# vim 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"></property> <!-- 1为开启实时统计、0为关闭 -->
<property name="useGlobleTableCheck"></property> <!-- 1为开启全加班一致性检测、0为关闭 --> <property name="sequnceHandlerType"></property>
<!-- <property name="useCompression"></property>--> <!--1为开启mysql压缩协议-->
<!-- <property name="fakeMySQLVersion">5.6.</property>--> <!--设置模拟的MySQL版本号-->
<!-- <property name="processorBufferChunk"></property> -->
<!--
<property name="processors"></property>
<property name="processorExecutor"></property>
-->
<!--默认为type : DirectByteBufferPool | type ByteBufferArena-->
<property name="processorBufferPoolType"></property>
<!--默认是65535 64K 用于sql解析时最大文本长度 -->
<!--<property name="maxStringLiteralLength"></property>-->
<!--<property name="sequnceHandlerType"></property>-->
<!--<property name="backSocketNoDelay"></property>-->
<!--<property name="frontSocketNoDelay"></property>-->
<!--<property name="processorExecutor"></property>-->
<!--
<property name="handleDistributedTransactions"></property>
<!-- off heap for merge/order/group/limit 1开启 0关闭 -->
<property name="useOffHeapForMerge"></property>
<!-- 单位为m -->
<property name="memoryPageSize">1m</property>
<!-- 单位为k -->
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput"></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"></property>
<property name="schemas">TESTDB</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="" >
<table name="tb01" dml=""></table>
<table name="tb02" dml=""></table>
</schema>
</privileges>
-->
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
</mycat:server

二 验证

2.1 启动Mycat

[root@mycat conf]# ../bin/mycat start

2.2 日志

[root@mycat conf]# cat /usr/local/src/mycat/logs/wrapper.log

STATUS | wrapper  | // :: | --> Wrapper Started as Daemon
STATUS | wrapper | // :: | Launching a JVM...
ERROR | wrapper | // :: | Unable to start JVM: No such file or directory () #没有JVM,安装java
ERROR | wrapper | // :: | JVM exited while loading the application.
FATAL | wrapper | // :: | There were failed launches in a row, each lasting less than seconds. Giving up.
FATAL | wrapper | // :: | There may be a configuration problem: please check the logs.
STATUS | wrapper | // :: | <-- Wrapper Stopped

[root@mycat conf]# yum -y install java

[root@mycat conf]# ../bin/mycat start

[root@mycat conf]# tail -f /usr/local/src/mycat/logs/wrapper.log

INFO   | jvm     | // :: | -- ::, [INFO ][$_NIOREACTOR--RW] connectionAcquired MySQLConnection [id=, lastTime=, user=root, schema=master1, old shema=master1, borrowed=false, fromSlaveDB=true, threadId=, charset=latin1, txIsolation=, autocommit=true, attachment=null, respHandler=null, host=192.168.132.122, port=, statusSync=null, writeQueue=, modifiedSQLExecuted=false]  (io.mycat.backend.mysql.nio.handler.NewConnectionRespHandler:NewConnectionRespHandler.java:)
INFO | jvm | // :: | MyCAT Server startup successfully. see logs in logs/mycat.log

2.3 连接,端口为8066

[root@mycat ~]# mysql -uroot -p1234567 -h192.168.132.125  -P8066

MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+

2.4 写入数据测试

MySQL [(none)]> use TESTDB;
MySQL [TESTDB]> show tables;
MySQL [TESTDB]> select * from test;
MySQL [TESTDB]> insert into test values ();
MySQL [TESTDB]> select * from test;
+------+
| id |
+------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+------+
mater查看
mysql> select * from master1.test;
+------+
| id |
+------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+------+
slave查看
mysql> select * from master1.test;
+------+
| id |
+------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+------+

成功写入数据

2.5 验证读写分离

修改日志级别为debug

[root@mycat conf]# vim log4j2.xml

<Loggers>
<!--<AsyncLogger name="io.mycat" level="info" includeLocation="true" additivity="false">-->
<!--<AppenderRef ref="Console"/>-->
<!--<AppenderRef ref="RollingFile"/>-->
<!--</AsyncLogger>-->
<asyncRoot level="debug" includeLocation="true"> <AppenderRef ref="Console" />
<AppenderRef ref="RollingFile"/> </asyncRoot>
</Loggers>

[root@mycat conf]# /usr/local/src/mycat/bin/mycat stop

[root@mycat conf]# /usr/local/src/mycat/bin/mycat start

写入一个数据

MySQL [TESTDB]> insert  into test values (666);

[root@mycat conf]# tail -f /usr/local/src/mycat/logs/wrapper.log

INFO   | jvm     | // :: | -- ::, [DEBUG][$_NIOREACTOR--RW] con need syn ,total syn cmd  commands SET names utf8;schema change:false con:MySQLConnection [id=, lastTime=, user=root, schema=master1, old shema=master1, borrowed=true, fromSlaveDB=false, threadId=, charset=utf8, txIsolation=, autocommit=true, attachment=dn1{insert  into test values ()}, respHandler=SingleNodeHandler [node=dn1{insert  into test values ()}, packetId=], host=192.168.132.121, port=, statusSync=null, writeQueue=, modifiedSQLExecuted=true]  (io.mycat.backend.mysql.nio.MySQLConnection:MySQLConnection.java:)
INFO | jvm | // :: | -- ::, [DEBUG][$_NIOREACTOR--RW] release connection MySQLConnection [id=, lastTime=, user=root, schema=master1, old shema=master1, borrowed=true, fromSlaveDB=false, threadId=, charset=utf8, txIsolation=, autocommit=true, attachment=dn1{insert into test values ()}, respHandler=SingleNodeHandler [node=dn1{insert into test values ()}, packetId=], host=192.168.132.121, port=, statusSync=null, writeQueue=, modifiedSQLExecuted=true] (io.mycat.server.NonBlockingSession:NonBlockingSession.java:)
INFO | jvm | // :: | -- ::, [DEBUG][$_NIOREACTOR--RW] release channel MySQLConnection [id=, lastTime=, user=root, schema=master1, old shema=master1, borrowed=true, fromSlaveDB=false, threadId=, charset=utf8, txIsolation=, autocommit=true, attachment=null, respHandler=null, host=192.168.132.121, port=, statusSync=null, writeQueue=, modifiedSQLExecuted=false] (io.mycat.backend.datasource.PhysicalDatasource:PhysicalDatasource.java:)
INFO | jvm | // :: | -- ::, [DEBUG][Timer1] con query sql:select user() to con:MySQLConnection [id=, lastTime=, user=root, schema=master1, old shema=master1, borrowed=true, fromSlaveDB=false, threadId=, charset=latin1, txIsolation=, autocommit=true, attachment=null, respHandler=null, host=192.168.132.121, port=, statusSync=null, writeQueue=, modifiedSQLExecuted=false] (io.mycat.sqlengine.SQLJob:SQLJob.java:)

发现在192.168.132.121 主数据库上面

查找数据

MySQL [TESTDB]> select * from test;

INFO   | jvm     | // :: | -- ::, [DEBUG][$_NIOREACTOR--RW] con need syn ,total syn cmd  commands SET names utf8;schema change:false con:MySQLConnection [id=, lastTime=, user=root, schema=master1, old shema=master1, borrowed=true, fromSlaveDB=true, threadId=, charset=utf8, txIsolation=, autocommit=true, attachment=dn1{select * from test}, respHandler=SingleNodeHandler [node=dn1{select * from test}, packetId=], host=192.168.132.122, port=, statusSync=null, writeQueue=, modifiedSQLExecuted=false]  (io.mycat.backend.mysql.nio.MySQLConnection:MySQLConnection.java:)
INFO | jvm | // :: | -- ::, [DEBUG][$_NIOREACTOR--RW] release connection MySQLConnection [id=, lastTime=, user=root, schema=master1, old shema=master1, borrowed=true, fromSlaveDB=true, threadId=, charset=utf8, txIsolation=, autocommit=true, attachment=dn1{select * from test}, respHandler=SingleNodeHandler [node=dn1{select * from test}, packetId=], host=192.168.132.122, port=, statusSync=io.mycat.backend.mysql.nio.MySQLConnection$StatusSync@3055d901, writeQueue=, modifiedSQLExecuted=false] (io.mycat.server.NonBlockingSession:NonBlockingSession.java:)

在salve192.168.132.122从数据库上面,读和写在不同的服务器上面

2.6 问题

但这种方式有个问题,即master 挂了以后,slave 也不能提供读和写服务Slave 挂了 不能查询,但是可以插入。

停掉主数据库

[root@master1 ~]# systemctl stop mysqld

[root@mycat ~]# mysql -uroot -p1234567 -h192.168.132.125  -P8066 TESTDB

MySQL [TESTDB]> select * from test;
ERROR (HY000): Connection refused
MySQL [TESTDB]> insert into test values ();
ERROR (HY000): Connection refused

已经不能查询,也不可以插入

停掉slave

不可以查,但是可以写

[root@slave ~]# systemctl stop mysqld

MySQL [TESTDB]> select * from test;
ERROR (HY000): Connection refused
MySQL [TESTDB]> insert into test values ();
Query OK, row affected (0.00 sec)

读写分离配置完成

这种结构过于简单,但是实现了读写分离的功能,生产中需要综合考虑单点故障的问题,需要更可靠的架构!

mysql主从之基于mycat实现读写分离的更多相关文章

  1. 基于Mycat实现读写分离

    随着应用的访问量并发量的增加,应用读写分离是很有必要的.当然应用要实现读写分离,首先数据库层要先做到主从配置,本人前一篇文章介绍了mysql数据库的主从配置方式即:<mysql数据库主从配置&g ...

  2. Mariadb 基于Mycat实现读写分离

    环境:Mariadb主:192.168.200.129 Mariadb从:192.168.200.114 Mycat    :192.168.200.112 (1)      安装jdk,先查看本机是 ...

  3. SQL Server、MySQL主从搭建,EF Core读写分离代码实现

    一.SQL Server的主从复制搭建 1.1.SQL Server主从复制结构图 SQL Server的主从通过发布订阅来实现 1.2.基于SQL Server2016实现主从 新建一个主库&quo ...

  4. Dubbo入门到精通学习笔记(二十):MyCat在MySQL主从复制的基础上实现读写分离、MyCat 集群部署(HAProxy + MyCat)、MyCat 高可用负载均衡集群Keepalived

    文章目录 MyCat在MySQL主从复制的基础上实现读写分离 一.环境 二.依赖课程 三.MyCat 介绍 ( MyCat 官网:http://mycat.org.cn/ ) 四.MyCat 的安装 ...

  5. mycat(读写分离、负载均衡、主从切换)

    博主本人平和谦逊,热爱学习,读者阅读过程中发现错误的地方,请帮忙指出,感激不尽 1.环境准备 1.1新增两台虚拟机 mycat01:192.168.247.81 mycat02:192.168.247 ...

  6. MySQL - MyCat 实现读写分离

    前言 MyCat是一个彻底开源的,面向企业应用开发的大数据库集群,支持事务.ACID.可以替代MySQL的加强版数据库.其功能有可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群.融 ...

  7. Mycat实现读写分离,主备热切换

    实验环境:ubutu server 14 Master IP:172.16.34.212 Slave IP:172.16.34.34.156 Mycat server IP:172.16.34.219 ...

  8. SpringBoot 整合 MyCat 实现读写分离

    MyCat一个彻底开源的,面向企业应用开发的大数据库集群.基于阿里开源的Cobar产品而研发.能满足数据库数据大量存储:提高了查询性能.文章介绍如何实现MyCat连接MySQL实现主从分离,并集成Sp ...

  9. MySQL+Amoeba实现数据库主从复制和读写分离

    MySQL读写分离是在主从复制的基础上进一步通过在master上执行写操作,在slave上执行读操作来实现的.通过主从复制,master上的数据改动能够同步到slave上,从而保持了数据的一致性.实现 ...

随机推荐

  1. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  2. dynamic_cast, static_cast, const_cast, reinterprt_cast浅析

    用法:dynamic_cast < type-id > ( expression ) 说明:Type-id必须是类的指针.类的引用或者void *:如果type-id是指针类型,那么exp ...

  3. @noi.ac - 442@ 牛羊被他抢了

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 众所周知小G热衷于搏弈,有一天他来到你的大草原上,抢走了你所有的 ...

  4. Redis在Laravel项目中的应用实例详解

    https://mp.weixin.qq.com/s/axIgNPZLJDh9VFGVk7oYYA 在初步了解Redis在Laravel中的应用 那么我们试想这样的一个应用场景 一个文章或者帖子的浏览 ...

  5. windows 怎样关闭redis

    安装redis之后在命令行窗口中输入 redis-server redis.windows.conf 启动redis关闭命令行窗口就是关闭 redis.---redis作为windows服务启动方式r ...

  6. js获取当前时间戳以及前一天时间戳

    js获取当前时间戳以及前一天时间戳(毫秒) var timestamp = (new Date()).getTime(); console.log(timestamp);//打印当前时间戳 conso ...

  7. Google Colab——用谷歌免费GPU跑你的深度学习代码

    Google Colab简介 Google Colaboratory是谷歌开放的一款研究工具,主要用于机器学习的开发和研究.这款工具现在可以免费使用,但是不是永久免费暂时还不确定.Google Col ...

  8. hdu 2986 Ballot evaluation (Simulation)

    Problem - 2986 之前在华工赛见过的一道简单的模拟,用map轻松干掉.为了精确,要全程用整型比较.轻松1y~ 代码如下: #include <cstdio> #include ...

  9. java NIO之HelloWorld

    Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API,以便提高传输速度.但实际上,在最新的JDK中旧的I/0包已经使用NIO重新 ...

  10. BraveOS正式版发布,希望大家下载使用

    废话不多说,直接贴图才是王道 这里是DOS系统+默认官方(Platform系统) 下载地址:http://pan.baidu.com/s/1eQINwx8 (引导进Platform系统后,默认管理员帐 ...