当前位置:网站首页>MySQL读写分离
MySQL读写分离
2022-07-17 08:31:00 【毕竟尹稳健】
读写分离
1、MySQL主从复制
介绍:
MySQL 主从复制是指数据可以从一个MySQL数据库服务器主节点复制到一个或多个从节点。MySQL 默认采用异步复制方式,这样从节点不用一直访问主服务器来更新自己的数据,数据的更新可以在远程连接上进行,从节点可以复制主数据库中的所有数据库或者特定的数据库,或者特定的表。


配置主库:
第一步:修改MySQL数据库的配置文件/etc/my.cnf
log-bin=mysql-bin #[必须]启用二进制日志
server-id = 100 #[必须]服务器唯一ID
第二步:重启MySQL服务
systemctl restart mysqld
第三步:登录MySQL数据库,执行下面的SQL
-- 创建用于主从复制的用户,slave = 用户名 % = 代表所有IP都可以连接(可换成具体ip) 123456 = 密码
CREATE USER 'slave'@'%' IDENTIFIED with mysql_native_password by '1111';
-- 为该用户赋予权限
GRANT replication slave on *.* to 'slave'@'%';
-- 刷新配置
FLUSH PRIVILEGES;
-- 查看创建的用户是否存在
use mysql;
select user, host from user;
-- 查看当前binlog节点及position值
show master status;

配置从库Slave
第一步:修改MySQL数据库的配置文件
server-id = 101 #[必须]服务器唯一ID
第二步:重启MySQL服务
第三步:登录MySQL,执行下面的SQL
-- 添加主数据库信息及读取binlog的节点信息
CHANGE MASTER TO MASTER_HOST = '47.112.138.176',
MASTER_USER = 'slave',
MASTER_PASSWORD = '1111',
MASTER_LOG_FILE = 'mysql-bin.000001',
MASTER_LOG_POS = 988
-- 启动从库
START SLAVE;
-- 查看主从状态
SHOW SLAVE STATUS;
2、读写分离案例(Sharding-JDBC)
1、Sharding-JDBC介绍

2、Sharding-JDBC入门案例
1、导入依赖:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2、在配置文件中配置读写分离规则
spring:
shardingsphere:
datasource:
names:
master,slave
# 主数据源
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://ip/rw?serverTimezone=Asia/Shanghai
username: root
password: 1111
# 从数据源
slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/rw?serverTimezone=Asia/Shanghai
username: root
password: 1111
masterslave:
# 读写分离配置
load-balance-algorithm-type: round_robin
# 最终的数据源名称
name: dataSource
# 主库数据源名称
master-data-source-name: master
# 从库数据源名称列表,多个逗号分隔
slave-data-source-names: slave
props:
sql:
show: true #开启SQL显示,默认false
# 3、在配置文件中配置允许Bean定义覆盖配置项
main:
allow-bean-definition-overriding: true
3、项目实现读写分离
1、导入依赖:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2、在配置文件中配置读写分离规则
spring:
shardingsphere:
datasource:
names:
master,slave
# 主数据源
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://ip/reggie?serverTimezone=Asia/Shanghai
username: root
password: 1111
# 从数据源
slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai
username: root
password: 1111
masterslave:
# 读写分离配置
load-balance-algorithm-type: round_robin
# 最终的数据源名称
name: dataSource
# 主库数据源名称
master-data-source-name: master
# 从库数据源名称列表,多个逗号分隔
slave-data-source-names: slave
props:
sql:
show: true #开启SQL显示,默认false
# 3、在配置文件中配置允许Bean定义覆盖配置项
main:
allow-bean-definition-overriding: true
边栏推荐
- JS学习笔记04-05——构造函数的修改以及使用工厂方法创建
- CPU load of webgl - webgl comparison
- 深度学习第一周Introduction to Deep Learning习题整理
- Paddleserving service deployment tensorrt reports an error, shape of TRT subgraph is [-1, -1768],
- ARM计算新应用,违规垂钓“一网打尽”
- New data type of redis bitmaps
- 终结重复开发,两三下搞定登录系统个性化
- Use torch NN builds the simplest neural network framework
- trochvision中数据集的使用
- 深度学习之线性回归+基础优化
猜你喜欢

idea按条件断点调试

Redis common data types - redis list and redis set

Cbcgpedit control used by BCG

Eureka Basics

【AXI】解读AXI协议的额外信号(QOS信号,REGION信号,与USER信号)

How to position the circle of friends? Three core steps to build a circle of friends selling popular products

图片浏览器

Gateway new generation gateway

scratch逆序输出 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月

Solutions to license invalidation caused by MATLAB update
随机推荐
Softmax 回归 + 损失函数 + 图片分类数据集
Microservices and microservice architecture
scratch逆序输出 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月
5g at that time, where will driverless driving go in the future?
WebGL的CPU负载问题-WebGL对比
How to set preferences when developing esp8266 and esp32 with Arduino
65、Restful规范
凸面镜面3D玻璃轮廓扫描
深度学习第三周Shallow Neural Networks习题整理
Opportunities and challenges of Brazilian mobile game Investment Agency
Junit5
Nacos new configuration management
Snap 1669 combine deux notes de liste
【LeetCode】通用操作总结
Sorting out of neural network basics exercises in the second week of in-depth study
2022年上海市安全员C证国家题库及答案
The latest generation of Internet: Web 3.0
49、Mysql使用
RestTemplate
分布式事务-最大努力通知方案