提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
记得上篇文章我们已经在windows上配置了mysql的主从复制
详细请看:mysql主从复制 mysql水平分库
这篇文章写一篇 MySQL读写分离与分库分表
一、是什么?
读写分离就是只在主服务器上写,只在从服务器上读。基本的原理是让主数据库处理事务性查询,而从数据库处理select查询。
数据库复制被用来把主数据库上事务性查询导致的变更同步到集群中的从数据库。
二、使用步骤
1.将mysql主库中的data文档复制到从库里面(将数据库的服务先关掉)
2、将主库和从库的my.ini配置文件中节点数据库名称对应上
3.到idea中修改配置文件,把数据库分为主从两个
此处省略搭建项目流程(mapper controller 之类接口) 大体没变 跟上一篇项目一样:具体点击这里
#设置端口号
server.port=6607
#设置命名
spring.application.name=product-server
#循环读取mapper文件
mybatis.mapper-locations=classpath:/mappers/*.xml
#bean覆盖
#spring.main.allow-bean-definition-overriding=true
#数据源名称,多数据源以逗号分隔,自定义数据源名称为 m1和m2
spring.shardingsphere.datasource.names=m1,m2,s1,s2
#数据库连接池类名称
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
#数据库驱动类名
spring.shardingsphere.datasource.m1.dirver-class-name=com.mysql.cj.jdbc.Driver
#数据库url连接
spring.shardingsphere.datasource.m1.url=jdbc:mysql://127.0.0.1:3306/product_db1?serverTimezone=Asia/Shanghai
#数据库用户名
spring.shardingsphere.datasource.m1.username=root
#数据库密码
spring.shardingsphere.datasource.m1.password=123456
#数据库连接池类名称
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
#数据库驱动类名
spring.shardingsphere.datasource.m2.dirver-class-name=com.mysql.cj.jdbc.Driver
#数据库url连接
spring.shardingsphere.datasource.m2.url=jdbc:mysql://127.0.0.1:3306/product_db2?serverTimezone=Asia/Shanghai
#数据库用户名
spring.shardingsphere.datasource.m2.username=root
#数据库密码
spring.shardingsphere.datasource.m2.password=123456
#从库
spring.shardingsphere.datasource.s1.type=com.alibaba.druid.pool.DruidDataSource
#数据库驱动类名
spring.shardingsphere.datasource.s1.dirver-class-name=com.mysql.cj.jdbc.Driver
#数据库url连接
spring.shardingsphere.datasource.s1.url=jdbc:mysql://127.0.0.1:3307/product_db1?serverTimezone=Asia/Shanghai
#数据库用户名
spring.shardingsphere.datasource.s1.username=root
#数据库密码
spring.shardingsphere.datasource.s1.password=123456
#数据库连接池类名称
spring.shardingsphere.datasource.s2.type=com.alibaba.druid.pool.DruidDataSource
#数据库驱动类名
spring.shardingsphere.datasource.s2.dirver-class-name=com.mysql.cj.jdbc.Driver
#数据库url连接
spring.shardingsphere.datasource.s2.url=jdbc:mysql://127.0.0.1:3307/product_db2?serverTimezone=Asia/Shanghai
#数据库用户名
spring.shardingsphere.datasource.s2.username=root
#数据库密码
spring.shardingsphere.datasource.s2.password=123456
#设置主库和从库
spring.shardingsphere.sharding.master-slave-rules.m1.masterDataSourceName=m1
spring.shardingsphere.sharding.master-slave-rules.m1.slaveDataSourceNames=s1
spring.shardingsphere.sharding.master-slave-rules.m2.masterDataSourceName=m2
spring.shardingsphere.sharding.master-slave-rules.m2.slaveDataSourceNames=s2
#根据商品商品id进行分库
spring.shardingsphere.sharding.tables.product.databaseStrategy.inline.shardingColumn=shopId
spring.shardingsphere.sharding.tables.product.databaseStrategy.inline.algorithmExpression=m$->{shopId % 2+1}
#根据id进行表的划分
spring.shardingsphere.sharding.tables.product.keyGenerator.column=id
#根据雪花算法生成id的值
spring.shardingsphere.sharding.tables.product.keyGenerator.type=SNOWFLAKE
# 分片策略为 id 为偶数操作 m1 数据源,否则操作 m2
spring.shardingsphere.sharding.tables.product.actualDataNodes=m$->{1..2}.product_$->{1..2}
#根据主键id进行分表
spring.shardingsphere.sharding.tables.product.tableStrategy.inline.shardingColumn=id
# 指定表的数据分布情况,其中product_$可以自定义,配置数据节点 m1.product_1,m1.product_2,m2.product_1,m2.product_2
spring.shardingsphere.sharding.tables.product.tableStrategy.inline.algorithmExpression=product_$->{id % 2 + 1}
#指定公共表
spring.shardingsphere.sharding.broadcast-tables=shop
spring.shardingsphere.sharding.tables.shop.actual-data-nodes=m$->{1..2}.shop
spring.shardingsphere.sharding.tables.shop.keyGenerator.column=id
spring.shardingsphere.sharding.tables.shop.keyGenerator.type=SNOWFLAKE
#日志
spring.shardingsphere.props.sql.show=true
logging.level.root = info
logging.level.org.springframework.web = info
logging.level.com.itheima.dbsharding = debug
logging.level.druid.sql = debug
读写分离的主要修改配置:
4.测试接口
查询测试
select获取id,看是否是从库中查询的
结果如下:
看看控制台
从日志中可以看到s1,因为在前面配置文件中,主库是m1和m2,从库是s1和s2,这里显示是从s1和s2中查的,也就是说实现了读写分离
插入测试
看看结果
看看从库:也跟着更新数据
非常简单!希望对你有帮助!多多支持!!
5.总结
Mysql主从复制是一个异步的复制过程,底层是基于Mysql数据库自带的二进制日志功能。就是一台或多台Mysql数据库(slave,即从库)从另一台Mysql数据库(master,即主库)进行日志的复制然后再解析日志并应用到自身,最终实现从库的数据和主库的数据保持一致。Mysql主从复制是Mysql数据库自带功能,无需借助第三方工具。