• Mycat 安装配置


    参考:https://www.cnblogs.com/binghe001/p/13959992.html

    介绍

    Mycat支持水平分片、垂直分片。

    • 水平分片 :将一个表数据分隔到多个节点上,以行数据为基本单元。
    • 垂直分片:一个数据的多个表分别分布到不同节点。如数据库A的a,b,c表分别在节点A、B、C上。

    核心概念

    • Schema:称逻辑库,代表数据库实例的抽象。
    • Table:表,代表了某张表。这张表跟一个或多个DataNode依赖。
    • DataNode:代表了一个分片节点,它是存放表的物理节点。通过DataHost来配置具体的地址信息。
      • DataHost:定义具体的物理库的访问地址、账号密码,同时指定读、写库。

    在schema.xml文件我们可以清楚的认识这些概念。

    下载

    根据服务器情况下载对应的版本。
    https://github.com/MyCATApache/Mycat-download/tree/master/1.6-RELEASE

    # 解压
    tar -xvf xxx
    
    # 解压结果
    root@ubuntu:/home/liangshijie/mycat# ls
    bin  catlet  conf  lib  logs  version.txt
    
    # 配置环境变量
    vim /etc/profile
    # 追加:
    export PATH=/home/liangshijie/mycat/bin:${PATH}
    # 刷新环境变量
    source /etc/profile
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    配置

    基本情况

    环境:ubuntu18、使用docker启动
    数据库:study;
    数据表:user;
    mysql5.7主从集群:

    节点地址
    192.168.204.139
    192.168.204.138
    192.168.204.140

    配置文件

    # 进入mycat 的conf目录
    cd /home/liangshijie/mycat/conf
    
    • 1
    • 2
    # 修改
    vim server.xml
    
    # 将schemas改为study。
    # 在这里可以mycat客户端登录密码。
    # root账户默认读写
    # user账户是只读
    
    <user name="root">
            <property name="password">123456</property>
            <property name="schemas">study</property>
    </user>
    
    <user name="user">
            <property name="password">user</property>
            <property name="schemas">study</property>
            <property name="readOnly">true</property>
    </user>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    # 修改物理节点相关配置
    vim schema.xml
    # 先修改schema的name为我们的数据库study
    # 修改table标签,表为user,使用dn1节点
    # 根据集群修改dataHost
    
    
    DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">
    
    	<schema name="study" checkSQLschema="true" sqlMaxLimit="100">
    		
    		<table name="user" dataNode="dn1"  />	
    
    		
    		
    		
    		
    	schema>
    	
    	<dataNode name="dn1" dataHost="node1" database="study" />
    	 
    	
    	<dataHost name="node1" 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.204.139:3306" user="root"  password="123456">
    			
    			<readHost host="hostS2" url="192.168.204.137:3306" user="root" password="123456" />
    
    			<readHost host="hostS3" url="192.168.204.140:3306" user="root" password="123456" />
    		writeHost>
    		
    		
    	dataHost>
    	
    
    	
    mycat:schema>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    启动命令

    mycat start | stop | restart | status

    链接客户端

    服务器开放端口8066。
    使用可视化工具链接。账户密码:root\123456。

    测试

    插入或查询数据。
    查看当前server_id: SELECT @@server_id;

    整合Springboot

    跟Springboot整合只需修改mysql的数据源配置。

    
    spring:
      datasource:
        username: root
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://192.168.204.142:8066/study?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
    server:
      port: 8062
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.1.6.RELEASEversion>
            <relativePath/> 
        parent>
        <groupId>cn.lsjgroupId>
        <artifactId>read-write-separationartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>read-write-separationname>
        <description>Demo project for Spring Bootdescription>
        <properties>
            <java.version>8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.5.1version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
    
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.1.22version>
            dependency>
    
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.6version>
            dependency>
    
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombokgroupId>
                                <artifactId>lombokartifactId>
                            exclude>
                        excludes>
                    configuration>
                plugin>
            plugins>
        build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    @SpringBootApplication
    @MapperScan("cn.lsj.readwriteseparation.mapper")
    public class ReadWriteSeparationApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ReadWriteSeparationApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    @RequestMapping("test")
    @AllArgsConstructor
    @RestController
    public class TestController {
    
        private final UserService userService;
    
        @GetMapping("read")
        public User read() {
            return userService.getById(1);
        }
    
        @PostMapping("write")
        public String write(@RequestBody User user) {
            userService.saveOrUpdate(user);
            return "成功";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    @Data
    public class User {
        private int id;
        private String username;
    }
    
    
    public interface UserMapper extends BaseMapper<User> {
    }
    
    
    public interface UserService extends IService<User> {
    }
    
    
    @Service
    public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    【Python】9*9乘法口诀表(while、for两种循环)
    记录一次WLAN Panic导致的gcore问题分析
    「Redis数据结构」列表对象(List)
    量子光学的进步:光子学的“下一件小事”
    spring boot酒店会员点餐系统毕业设计源码072005
    插入排序和选择排序
    统一SQL 支持Oracle cast函数转换
    Swift 周报 第十六期
    网站收录查询-批量网站收录查询软件
    计算机网络——DNS协议
  • 原文地址:https://blog.csdn.net/qq_38974073/article/details/128192655