您当前的位置: 首页 >  sql

Dongguo丶

暂无认证

  • 0浏览

    0关注

    472博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Mycat数据库中间件对Mysql读写分离和分库分表配置

Dongguo丶 发布时间:2018-05-06 15:39:54 ,浏览量:0

Mycat是一个开源的分布式数据库系统,不同于oracle和mysql,Mycat并没有存储引擎,但是Mycat实现了mysql协议,前段用户可以把它当做一个Proxy。其核心功能是分表分库,即将一个大表水平分割为N个小表,存储在后端mysql存储引擎里面。最新版本的Mycat不仅支持mysql,还可以支持MS SqlServer,Oracle,DB2等关系型数据库,而且还支持MongoDB这种NoSQL。Mycat对调用者屏蔽了后端存储具体实现。

Mycat的原理是先拦截用户的SQL语句并做分析:分片分析,路由分析,读写分离分析,缓存分析等,再对将该SQL语句发送到指定规则的数据库。最终将存储引擎返回的结果返回给用户.

本次搭建的Mycat读写分离和分库分表配置是基于5台服务器(1台Mac,4台Centos),架构图如下,Mac服务器上面安装Mycat;M1(OS名称:Server1,Ip:172.16.130.189)和M2(OS名称:Server2,Ip:172.16.130.190)是两个写节点; S1(OS名称:Server3,Ip:172.16.130.191)和S2(OS名称:Server4,Ip:172.16.130.192)是两个读节点。且M1和S1配置了主从复制,M2和S2也配置了主从复制。

步骤

1.在Server1,Server2,Server3和Server4中安装相同版本的Mysql,安装步骤在我之前的一篇文章中:四·安装mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz(基于Centos7源码安装)

2.配置Server3同步Server1;Server4同步Server2,在我的另外一篇文章中:一.Mysql主从复制配置

   其中关键代码如下

--------------------------------------Server1配置-----------------------------------------------------------
mysql> grant replication slave on *.* to 'mysqlsync'@'172.16.130.191' identified by 'mysqlsync';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show master status;
+------------+----------+--------------+------------------+-------------------+
| File       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------+----------+--------------+------------------+-------------------+
| bin.000003 |      455 |              |                  |                   |
+------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
--------------------------------------Server3配置-----------------------------------------------------------

  mysql> change master to master_host='172.16.130.189',master_user='mysqlsync',master_password='mysqlsync',master_log_file='bin.000003',master_log_pos=154;   Query OK, 0 rows affected, 2 warnings (0.01 sec)

  mysql> start slave;   Query OK, 0 rows affected (0.00 sec)

 
 
3.在Server1和Server2上面分别新建一个名为mycattest的schema,且配置的访问用户名为mycattest,密码为1234
4.修改wapper.cnf
#********************************************************************
# Wrapper Properties
#********************************************************************
# Java Application
wrapper.java.command=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java
wrapper.working.dir=..

# Java Main class.  This class must implement the WrapperListener interface
#  or guarantee that the WrapperManager class is initialized.  Helper
#  classes are provided to do this for you.  See the Integration section
#  of the documentation for details.
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
set.default.REPO_DIR=lib
set.APP_BASE=.

# Java Classpath (include wrapper.jar)  Add class path elements as
#  needed starting from 1
wrapper.java.classpath.1=lib/wrapper.jar
wrapper.java.classpath.2=conf
wrapper.java.classpath.3=%REPO_DIR%/*

# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=lib

# Java Additional Parameters
#wrapper.java.additional.1=
wrapper.java.additional.1=-DMYCAT_HOME=.
wrapper.java.additional.2=-server
wrapper.java.additional.3=-XX:MaxPermSize=64M
wrapper.java.additional.4=-XX:+AggressiveOpts
wrapper.java.additional.5=-XX:MaxDirectMemorySize=2G
wrapper.java.additional.6=-Dcom.sun.management.jmxremote
wrapper.java.additional.7=-Dcom.sun.management.jmxremote.port=1984
wrapper.java.additional.8=-Dcom.sun.management.jmxremote.authenticate=false
wrapper.java.additional.9=-Dcom.sun.management.jmxremote.ssl=false
wrapper.java.additional.10=-Xmx4G
wrapper.java.additional.11=-Xms1G

# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=3

# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=64

# Application parameters.  Add parameters as needed starting from 1
wrapper.app.parameter.1=io.mycat.MycatStartup
wrapper.app.parameter.2=start

#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Format of output for the console.  (See docs for formats)
wrapper.console.format=PM

# Log Level for console output.  (See docs for log levels)
wrapper.console.loglevel=INFO

# Log file to use for wrapper output logging.
wrapper.logfile=logs/wrapper.log

# Format of output for the log file.  (See docs for formats)
wrapper.logfile.format=LPTM

# Log Level for log file output.  (See docs for log levels)
wrapper.logfile.loglevel=INFO

# Maximum size that the log file will be allowed to grow to before
#  the log is rolled. Size is specified in bytes.  The default value
#  of 0, disables log rolling.  May abbreviate with the 'k' (kb) or
#  'm' (mb) suffix.  For example: 10m = 10 megabytes.
wrapper.logfile.maxsize=0

# Maximum number of rolled log files which will be allowed before old
#  files are deleted.  The default value of 0 implies no limit.
wrapper.logfile.maxfiles=0

# Log Level for sys/event log output.  (See docs for log levels)
wrapper.syslog.loglevel=NONE

#********************************************************************
# Wrapper Windows Properties
#********************************************************************
# Title to use when running as a console
wrapper.console.title=Mycat-server

#********************************************************************
# Wrapper Windows NT/2000/XP Service Properties
#********************************************************************
# WARNING - Do not modify any of these properties when an application
#  using this configuration file has been installed as a service.
#  Please uninstall the service before modifying this section.  The
#  service can then be reinstalled.

# Name of the service
wrapper.ntservice.name=mycat

# Display name of the service
wrapper.ntservice.displayname=Mycat-server

# Description of the service
wrapper.ntservice.description=The project of Mycat-server

# Service dependencies.  Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=

# Mode in which the service is installed.  AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START

# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=false

wrapper.ping.timeout=120
configuration.directory.in.classpath.first=conf

5.修改server.xml




    
    druidparser
    0  
    0  

        2
       
         
    
    
        
        0
        
        
        
        
        
        
        
        
        0
        
            
        1

        
        1m

        
        1k

        0

        
        384m


        
        true


    
    
    
    
    
    
        123456
        TESTDB
        
        
        
    

    
        user
        TESTDB
        true
    

6.修改schema.xml




     
             
     
    
    
     
     
     
         
   
        select user()
        
        
            
            
        
    
    
        select user()
        
        
            
            
        
        
    

7.启动Mycat

bogon:bin Randy$ ./mycat console
Running Mycat-server...
wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
jvm 1    | 
jvm 1    | 2017-03-18 21:21:18,296 [INFO ][WrapperSimpleAppMain] total resouces of dataHost 172.16.130.190 is :2  (io.mycat.backend.datasource.PhysicalDBPool:PhysicalDBPool.java:100) 
jvm 1    | 2017-03-18 21:21:18,298 [INFO ][WrapperSimpleAppMain] total resouces of dataHost 172.16.130.189 is :2  (io.mycat.backend.datasource.PhysicalDBPool:PhysicalDBPool.java:100) 
jvm 1    | 2017-03-18 21:21:18,307 [INFO ][WrapperSimpleAppMain] create layer cache pool TableID2DataNodeCache of type encache ,default cache size 10000 ,default expire seconds18000  (io.mycat.cache.CacheService:CacheService.java:125) 
jvm 1    | 2017-03-18 21:21:18,308 [INFO ][WrapperSimpleAppMain] create child Cache: TESTDB_ORDERS for layered cache TableID2DataNodeCache, size 50000, expire seconds 18000  (io.mycat.cache.DefaultLayedCachePool:DefaultLayedCachePool.java:80) 
jvm 1    | 2017-03-18 21:21:18,329 [DEBUG][WrapperSimpleAppMain] Configuring ehcache from ehcache.xml found in the classpath: file:/Users/Randy/Developer/16InstallSoftware/mycat/conf/ehcache.xml  (net.sf.ehcache.config.ConfigurationFactory:ConfigurationFactory.java:132) 

******测试:

由于我在上面的配置中,定了表g_user按照sharding-by-mod算法规则来分片,

 

通过Mycat插入了两条数据

insert into g_user(id) values(1);
insert into g_user(id) values(2);

通过查询,发现id=1的数据被插入到了M2和S2数据库服务器,而id=2的数据被插入到了M1和S1数据库

关注
打赏
1638062488
查看更多评论
立即登录/注册

微信扫码登录

0.0381s