栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【ShardingSphere-JDBC】水平垂直分库分表及读写分离实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【ShardingSphere-JDBC】水平垂直分库分表及读写分离实例

主要内容
      • 全部知识的源码
      • ShardingSphere-JDBC简单介绍
      • 一、水平分表
      • 二、水平分库
      • 三、垂直分库
      • 四、公共表策略
      • 五、读写分离

全部知识的源码

Springboot项目结构:

表和库格式:所有表和库如下SQL语句(MySQL5.5版本)

sql:


SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for customer_1
-- ----------------------------
DROp TABLE IF EXISTS `customer_1`;
CREATE TABLE `customer_1`  (
  `id1` bigint(20) NOT NULL COMMENT '主键id,自增',
  `id2` bigint(20) NULL DEFAULT NULL COMMENT 'id2',
  `name` varchar(31) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '名称',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id1`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for customer_2
-- ----------------------------
DROP TABLE IF EXISTS `customer_2`;
CREATE TABLE `customer_2`  (
  `id1` bigint(20) NOT NULL COMMENT '主键id,自增',
  `id2` bigint(20) NULL DEFAULT NULL COMMENT 'id2',
  `name` varchar(31) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '名称',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id1`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user`  (
  `user_id` bigint(20) NOT NULL,
  `username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `ustatus` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

SET FOREIGN_KEY_CHECKS = 1;


SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for customer_1
-- ----------------------------
DROP TABLE IF EXISTS `customer_1`;
CREATE TABLE `customer_1`  (
  `id1` bigint(20) NOT NULL COMMENT '主键id,自增',
  `id2` bigint(20) NULL DEFAULT NULL COMMENT 'id2',
  `name` varchar(31) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '名称',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id1`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for customer_2
-- ----------------------------
DROP TABLE IF EXISTS `customer_2`;
CREATE TABLE `customer_2`  (
  `id1` bigint(20) NOT NULL COMMENT '主键id,自增',
  `id2` bigint(20) NULL DEFAULT NULL COMMENT 'id2',
  `name` varchar(31) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '名称',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id1`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user`  (
  `user_id` bigint(20) NOT NULL,
  `username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `ustatus` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

SET FOREIGN_KEY_CHECKS = 1;


SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user`  (
  `user_id` bigint(20) NOT NULL,
  `username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `ustatus` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;

pom.xml:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.4
         
    

    org.example
    helloByMaven
    1.0-SNAPSHOT

    
        1.8
        8
        8
    


    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.20
        
        
            mysql
            mysql-connector-java
        
        
            org.apache.shardingsphere
            sharding-jdbc-spring-boot-starter
            4.0.0-RC1
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.1
        
        
            org.projectlombok
            lombok
        
        
            junit
            junit
            4.12
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        

        
            
                src/main/java
                
                    **/*.xml
                
                true
            
        
    


主配置类ShardingsphereStudyApplication.java:

package App;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("mapper")
public class ShardingsphereStudyApplication {
    public static void main(String[] args) {
        SpringApplication.run(ShardingsphereStudyApplication.class, args);
    }
}

实体类Customer.java:

package entity;
import lombok.Data;
import java.util.Date;
@Data
public class Customer {
    private Long id1;//用于分表
    private Long id2;//用于分库
    private String name;
    private Date createTime;
    private Date updateTime;
}

实体类User.java:

package entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "t_user")
public class User {
    private Long userId;
    private String username;
    private String ustatus;
}

CustomerMapper.java:

package mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import entity.Customer;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CustomerMapper extends BaseMapper {
    @Override
    int insert(Customer record);
}

UserMapper.java:

package mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper {

}

CustomerMapper.xml:




    
        
        insert into customer (id2,`name`, create_time, update_time)
        values (#{id2,jdbcType=BIGINT},#{name,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
    

主配置文件application.properties:

#水平分表,分片策略:约定 id1 值奇数添加到 sharding-sphere.customer1 表,如果 id1 是偶数添加到 sharding-sphere.customer2
#spring.profiles.active=levelTable
#水平分库,分片策略: 约定 id2 值奇数添加到 sharding-sphere1 库,如果 id2 是偶数添加到 sharding-sphere2库
#spring.profiles.active=levelDatabase
#垂直分库
#spring.profiles.active=verticalDatabase
#主从分离
spring.profiles.active=masterSlaveSeparation

水平分表application-levelTable.properties:

# shardingjdbc 分片策略

# 配置数据源,给数据源起名称
spring.shardingsphere.datasource.names=sharding-sphere1

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

mybatis-plus.mapper-locations = classpath:/mapper/*.xml

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.sharding-sphere1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding-sphere1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding-sphere1.url=jdbc:mysql://localhost:3306/sharding-sphere1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.sharding-sphere1.username=root
spring.shardingsphere.datasource.sharding-sphere1.password=123456

# 指定 customer 表分布情况,配置表在哪个数据库里面,表名称都是什么 sharding-sphere1.customer1, sharding-sphere1.customer2
spring.shardingsphere.sharding.tables.customer.actual-data-nodes=sharding-sphere1.customer_$->{1..2}

# 指定 customer 表里面主键 id1 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.customer.key-generator.column=id1
spring.shardingsphere.sharding.tables.customer.key-generator.type=SNOWFLAKE

# 指定分片策略 约定 id1 值奇数添加到 customer1 表,如果 id1 是偶数添加到 customer2
spring.shardingsphere.sharding.tables.customer.table-strategy.inline.sharding-column=id1
spring.shardingsphere.sharding.tables.customer.table-strategy.inline.algorithm-expression=customer_$->{2-id1%2}

# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true
spring.message.encoding=UTF-8

水平分库application-levelDatabase.properties:

# shardingjdbc 分片策略

# 配置数据源,给数据源起名称,水平分库,俩数据源
spring.shardingsphere.datasource.names=sharding-sphere1,sharding-sphere2

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

mybatis-plus.mapper-locations = classpath:/mapper/*.xml

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.sharding-sphere1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding-sphere1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding-sphere1.url=jdbc:mysql://localhost:3306/sharding-sphere1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.sharding-sphere1.username=root
spring.shardingsphere.datasource.sharding-sphere1.password=123456

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.sharding-sphere2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding-sphere2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding-sphere2.url=jdbc:mysql://localhost:3306/sharding-sphere2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.sharding-sphere2.username=root
spring.shardingsphere.datasource.sharding-sphere2.password=123456


# 指定数据库分库情况和 customer 表分布情况,配置表在哪个数据库里面,表名称都是什么
# sharding-sphere1 sharding-sphere2 customer_1 customer_2
spring.shardingsphere.sharding.tables.customer.actual-data-nodes=sharding-sphere$->{1..2}.customer_$->{1..2}

# 指定 customer 表里面主键 id1 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.customer.key-generator.column=id1
spring.shardingsphere.sharding.tables.customer.key-generator.type=SNOWFLAKE

# 指定数据库分片策略 约定 id2 值奇数添加到 sharding-sphere1 库,如果 id2 是偶数添加到 sharding-sphere2
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id2
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=sharding-sphere$->{2-id2%2}
# 另外写法,指定表的字段做规则 其他表如果存在名字一样的字段不考虑
#spring.shardingsphere.sharding.tables.customer.database-strategy.inline.sharding-column=id2
#spring.shardingsphere.sharding.tables.customer.database-strategy.inline.algorithm-expression=sharding-sphere$->{2-id2%2}

# 指定表分片策略 约定 id1 值奇数添加到 customer1 表,如果 id1 是偶数添加到 customer2
spring.shardingsphere.sharding.tables.customer.table-strategy.inline.sharding-column=id1
spring.shardingsphere.sharding.tables.customer.table-strategy.inline.algorithm-expression=customer_$->{2-id1%2}

# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true
spring.message.encoding=UTF-8

垂直分库和配置公共表application-verticalDatabase.properties:

# shardingjdbc 分片策略

# 配置数据源,给数据源起名称,水平分库,俩数据源
spring.shardingsphere.datasource.names=sharding-sphere1,sharding-sphere2,user

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

mybatis-plus.mapper-locations = classpath:/mapper/*.xml

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.sharding-sphere1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding-sphere1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding-sphere1.url=jdbc:mysql://localhost:3306/sharding-sphere1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.sharding-sphere1.username=root
spring.shardingsphere.datasource.sharding-sphere1.password=123456

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.sharding-sphere2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding-sphere2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding-sphere2.url=jdbc:mysql://localhost:3306/sharding-sphere2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.sharding-sphere2.username=root
spring.shardingsphere.datasource.sharding-sphere2.password=123456

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.user.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.user.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.user.url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.user.username=root
spring.shardingsphere.datasource.user.password=123456

# 配置user_db 数据库里面t_user专库专表
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=user.t_user

# 指定 customer 表里面主键 id1 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

# 指定表分片策略
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user

## 配置公共表
#spring.shardingsphere.sharding.broadcast-tables=t_user
## 指设置公共表主键生成策略
#spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
#spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true
spring.message.encoding=UTF-8

读写分离application-masterSlaveSeparation.properties:

# shardingjdbc 分片策略

# 配置数据源,给数据源起名称,水平分库,俩数据源
spring.shardingsphere.datasource.names=sharding-sphere1,sharding-sphere2,user,s0

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

mybatis-plus.mapper-locations = classpath:/mapper/*.xml

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.sharding-sphere1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding-sphere1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding-sphere1.url=jdbc:mysql://localhost:3306/sharding-sphere1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.sharding-sphere1.username=root
spring.shardingsphere.datasource.sharding-sphere1.password=123456

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.sharding-sphere2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding-sphere2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding-sphere2.url=jdbc:mysql://localhost:3306/sharding-sphere2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.sharding-sphere2.username=root
spring.shardingsphere.datasource.sharding-sphere2.password=123456

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.user.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.user.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.user.url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.user.username=root
spring.shardingsphere.datasource.user.password=123456

# 配置数据源具体内容,包含连接池,驱动,地址,用户名和密码(从数据源)
spring.shardingsphere.datasource.s0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.s0.url=jdbc:mysql://localhost:3307/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.s0.username=root
spring.shardingsphere.datasource.s0.password=123456

spring.shardingsphere.sharding.master-slave-rules.user_db.master-data-source-name=user
spring.shardingsphere.sharding.master-slave-rules.user_db.slave-data-source-names=s0

# 配置user_db 数据库里面t_user专库专表
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=user_db.t_user

# 指定 customer 表里面主键 id1 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE
# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true
spring.message.encoding=UTF-8

测试类CustomerMapperTest.java:

import App.ShardingsphereStudyApplication;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import entity.Customer;
import entity.User;
import mapper.CustomerMapper;
import mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShardingsphereStudyApplication.class)
public class CustomerMapperTest {

    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private UserMapper userMapper;

    //____________________________水平分表___________________________________//
    @Test
    public void testInsertByLevelTable() {
        Customer customer = new Customer();
        customer.setName("Koma");
        customer.setId2(201716040225L);
        customer.setCreateTime(new Date());
        customer.setUpdateTime(new Date());

        for (long i=1; i<100; i++) {
            customerMapper.insert(customer);
        }
    }
    @Test
    public void findCustomerByLevelTable(){
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("id1",737719360205881345L);//奇数查customer_1
        Customer customer= customerMapper.selectOne(wrapper);
        System.out.println(customer);
    }

    //____________________________水平分库___________________________________//
    @Test
    public void testInsertByLevelDatabase() {
        Customer customer = new Customer();
        customer.setName("Koma");
        customer.setId2(201716040226L);//id2偶数加到sharding-sphere
        customer.setCreateTime(new Date());
        customer.setUpdateTime(new Date());
        customerMapper.insert(customer);
    }
    @Test
    public void findCustomerByLevelDatabase(){
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("id2",201716040226L);//奇数查customer_1
        Customer customer= customerMapper.selectOne(wrapper);
        System.out.println(customer);
    }

    //____________________________垂直分库(专库专表)和公共表___________________________________//
    @Test
    public void testInsertByVerticalDatabase() {
        User user = new User();
        user.setUsername("Koma_zhe_1");
        user.setUstatus("健康");
        userMapper.insert(user);
    }
    @Test
    public void findCustomerByVerticalDatabase(){
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",737958038979215361L);
        User user= userMapper.selectOne(wrapper);
        System.out.println(user);
    }
    //________________________________公共表测试删除_______________________________________//
    @Test
    public void testDeleteCommonTable(){
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",737964392833875969L);
        userMapper.delete(wrapper);
    }
}
ShardingSphere-JDBC简单介绍

Apache ShardingSphere 是一套开源的分布式数据库中间件解决方案组成的生态圈,它由 JDBC、Proxy 和 Sidecar(规划中)这 3 款相互独立,却又能够混合部署配合使用的产品组成。 它们均提供标准化的数据分片、分布式事务和数据库治理功能,可适用于如 Java 同构、异构语言、云原生等各种多样化的应用场景。

  • 一套开源的分布式数据库中间件解决方案。
  • ShardingSphere有三个产品:JDBC、Proxy、Sidecar(规划中)。
一、水平分表

同个数据库,多个表(表结构一致)

二、水平分库

不同数据库,相同的表(不同数据库里的表一样)

三、垂直分库

专库专用

四、公共表策略

存储固定数据的表,表数据很少发生变化,查询时经常要进行关联。在每个数据库中都创建出相同结构公共表。操作公共表时,同时操作添加了公共表的数据库中的公共表,添加记录时,同时添加,删除时,同时删除。

五、读写分离

主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库,主数据库一般是准实时的业务数据库。一台服务器充当主服务器,而另外一台服务器充当从服务器。

主库将变更写入 binlog 日志,然后从库连接到主库之后,从库有一个 IO 线程,将主库的 binlog 日志拷贝到自己本地,写入一个 relay 中继日志(relay log)中。接着从库中有一个 SQL 线程会从中继日志读取 binlog,然后执行 binlog 日志中的内容,也就是在自己本地再次执行一遍 SQL 语句,从而使从服务器和主服务器的数据保持一致。
从库会生成两个线程,一个 I/O 线程,一个 SQL 线程; I/O 线程会去请求主库的 binlog,并将得到的 binlog 写到本地的 relay-log(中继日志)文件中; 主库会生成一个 log dump 线程, 用来给从库 I/O 线程传 binlog; SQL 线程,会读取 relay log 文件中的日志,并解析成 sql 语句逐一执行。

参考:使用 ShardingSphere 实操MySQL分库分表实战

例子上我是对MySQL进行双份copy(windows),在本地起俩MySQL服务,配置主从服务器。
主(3306):

从(3307):

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943369.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号