当前位置: 首页 > news >正文

基于shardingsphere的分库分表方案

一、准备docker容器

启动两个mysql的docker容器

docker run -v /root/mysql_volume/data:/var/lib/mysql -v /root/mysql_volume/conf:/etc/mysql/conf.d -v /root/mysql_volume/my.cnf:/etc/my.cnf -p 3306:3306 --name mysql --restart=always --privileged=true -e MYSQL_ROOT_PASSWORD=aaaa1143134686 -d mysql:8.0.30 --lower_case_table_names=1docker run -v /root/mysql_volume_child1/data:/var/lib/mysql -v /root/mysql_volume_child1/conf:/etc/mysql/conf.d -v /root/mysql_volume_child1/my.cnf:/etc/my.cnf -p 3307:3306 --name mysql-child1 --restart=always --privileged=true -e MYSQL_ROOT_PASSWORD=aaaa1143134686 -d mysql:8.0.30 --lower_case_table_names=1

主节点的my.cnf配置

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
lower_case_table_names = 1
# my.cnf 配置
server-id=1
log-bin=mysql-bin
binlog_format=ROWpid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock!includedir /etc/mysql/conf.d/

从节点的my.cnf配置

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
lower_case_table_names = 1server-id=2   # 每个从库要唯一
relay-log=relay-log
read_only=1pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock!includedir /etc/mysql/conf.d/

重启主节点和从节点的mysql的docker容器后,在主节点下查看binlog位置

SHOW MASTER STATUS;

在子节点配置主节点信息

CHANGE MASTER TOMASTER_HOST='ip',MASTER_USER='root',MASTER_PASSWORD='mima',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=117056;START SLAVE;SHOW SLAVE STATUS;

二、配置shardingspheres

pom文件引入

        <!-- ShardingSphere JDBC Core --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.1.1</version></dependency>

配置文件引入

spring:application:name: 可变羽翼核心shardingsphere:datasource:# 定义多个数据源names: master, slave1master:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://ip:3306/wing_blog?serverTimezone=UTCusername: rootpassword: passwdslave1:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://ip:3307/wing_blog?serverTimezone=UTCusername: rootpassword: passwdrules:readwrite-splitting:data-sources:myds:type: Staticprops:write-data-source-name: masterread-data-source-names: slave1

http://www.xdnf.cn/news/514153.html

相关文章:

  • redis读写一致问题
  • Visual Studio已更新为17.14+集成deepseek实现高效编程
  • AI大模型(二)embedding模型调用后对产生的数据进行分析
  • 水平可见直线--上凸包(andrew算法
  • 【嵙大o】C++作业合集
  • 不同版本 Linux 系统账号操作指令 ——rtkit 账号删除、普通账号的创建 / 删除 / 权限修改超详细大全
  • 如何在 Windows 11 或 10 上安装 Amazon Corretto
  • Ubuntu 20.04 报错记录: Matplotlib 无法使用 OpenCV 的 libqxcb.so
  • O2O电商变现:线上线下相互导流——基于定制开发开源AI智能名片S2B2C商城小程序的研究
  • Python蓝色飘雪
  • Linux云计算训练营笔记day10(MySQL数据库)
  • Java虚拟机 - JVM与Java体系结构
  • MyBatis 核心技术详解:从连接池到多表查询
  • Python多进程、多线程、协程典型示例解析
  • 深入理解 OpenCV 的 DNN 模块:从基础到实践
  • OpenSearch入门:从文档示例到查询实战
  • MCP - Cline 接入 高德地图 Server
  • DAY 29 复习日:类的装饰器
  • # 终端执行 java -jar example.jar 时(example.jar为项目jar包)报错:“没有主清单属性” 的解决方法
  • 第一章:重启之始
  • 零基础搭建!基于PP-ShiTuV2的轻量级图像识别系统(Docker+API部署指南)
  • 蓝桥杯1140 最小质因子之和(Hard Version)
  • 2KW压缩机驱动参考设计【SCH篇】
  • 使用conda创建python虚拟环境,并自定义路径
  • C++学习:六个月从基础到就业——C++20:协程(Coroutines)
  • Golang内存逃逸
  • 用代码解读_AI_强化学习在机器人路径规划中的应用与优化
  • nginx相关面试题30道
  • OpenCV-去噪效果和评估指标方法
  • MapReduce-WordCount实现按照value降序排序、字符小写、识别不同标点