当前位置: 首页 > 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

相关文章:

  • 【C++】尾置返回类型(Trailing Return Type)总结
  • AI写PPT可以用吗?我测试了3款AI写PPT工具,分享感受
  • Graphics——基于.NET 的 CAD 图形预览技术研究与实现——CAD c#二次开发
  • 【NGINX】 -10 keepalived + nginx + httpd 实现的双机热备+ 负载均衡
  • 湖北理元理律师事务所:债务管理的社会价值探索
  • Science Robotics 封面论文:基于形态学开放式参数化的仿人灵巧手设计用于具身操作
  • 基于Java+MySQL+Servlet的留言系统开发全解析
  • uniapp中的easycom工作机制
  • 构建 TypoView:一个富文本样式预览工具的全流程记录
  • Go 语言中的一等公民(First-Class Citizens)
  • 数位和:从定义到编程实现
  • jupyter启动出现OSError: [Errno 28] No space left on device
  • atcoder C - ~
  • 文件IO之标准IO
  • Binary Prediction with a Rainfall Dataset-(回归+特征工程+xgb)
  • 入门OpenTelemetry——应用自动埋点
  • ColorAid —— 一个面向设计师的色盲模拟工具开发记
  • 多模态大语言模型arxiv论文略读(八十)
  • Git多人协作
  • SOLID 面对象设计的五大基本原则
  • 前四月国家铁路发送货物12.99亿吨,同比增长3.6%
  • 事关中国,“英伟达正游说美国政府”
  • 菲律宾选举委员会公布中期选举结果,马科斯阵营选情未达预期
  • 雅典卫城上空现“巨鞋”形状无人机群,希腊下令彻查
  • 钕铁硼永磁材料龙头瞄准人形机器人,正海磁材:已向下游客户完成小批量供货
  • 【社论】打破“隐形高墙”,让老年人更好融入社会