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

网站开发职责南昌百度推广公司

网站开发职责,南昌百度推广公司,网页升级紧急通知新域名,用java做网站可以吗1. 概述 1.1 主从复制概述 在生产环境中,随着业务量增长,数据库的读写压力会不断加大。为了提升系统的可用性、扩展性与数据安全性,主从复制(Replication) 成为一种常见的数据库架构方案。通过主从复制: …

1. 概述

1.1 主从复制概述

在生产环境中,随着业务量增长,数据库的读写压力会不断加大。为了提升系统的可用性、扩展性与数据安全性,主从复制(Replication) 成为一种常见的数据库架构方案。通过主从复制:

  • 读写分离:主库处理写操作,从库处理读操作,提升整体性能。
  • 高可用性:主库故障后可快速切换到从库,提高系统稳定性。
  • 数据备份:从库作为热备份,提升数据安全。
  • 数据分析:从库可用于数据分析、报表查询等业务,减少对主库影响。

1.2 主从复制类型

MySQL 提供多种主从复制模式:

类型简介特点
基于语句(Statement-Based Replication, SBR)复制 SQL 语句简单、可能因非确定性函数出错
基于行(Row-Based Replication, RBR)复制变更的数据行精确、安全但数据量大
混合模式(Mixed-Based Replication, MBR)自动在 SBR 与 RBR 之间切换综合两者优点

当前多数生产环境推荐使用 Row-Based(RBR)

2. 主从复制原理

MySQL 主从复制(Replication)是通过**复制主库上的二进制日志(binlog)**来实现从库数据同步的过程。整个过程可以分为 五个步骤三个关键线程,分别在主库与从库之间协作完成。

2.1 主从复制的关键线程

线程所在节点作用
Binlog Dump Thread主库监听并推送 binlog 变更给从库
I/O Thread从库向主库请求 binlog,并写入本地中继日志(relay log)
SQL Thread从库读取 relay log,并按顺序执行日志中的 SQL 操作

2.2 主从复制的具体步骤

  1. 主库记录 binlog 日志
    当主库执行 INSERTUPDATEDELETE 等 DML 操作时,会将这些更改以事件(event)的形式按顺序写入到 binlog(二进制日志)中。这个日志记录的是数据变化的“描述”。

  2. 从库发起连接请求(I/O Thread)
    从库通过 I/O 线程连接主库,并向主库请求从某个位置(指定的 binlog 文件和偏移量)开始读取 binlog 数据。

  3. 主库启动 Binlog Dump Thread 发送日志
    主库接收到从库连接请求后,会启动一个专用的 Binlog Dump 线程,实时监听 binlog 的变化,并将新生成的 binlog 内容发送给对应的从库。

  4. 从库接收并写入 relay log(中继日志)
    从库的 I/O Thread 接收到主库发送的 binlog 数据后,会将这些日志写入到本地的 relay log(中继日志)文件中,作为一个临时的日志缓冲。

  5. 从库重放 relay log 执行 SQL(SQL Thread)
    从库的 SQL Thread 会不断读取 relay log 中的日志内容,并解析并按顺序执行日志中的操作,从而在从库上“重放”主库的数据更改,实现数据同步。

在这里插入图片描述

3. 搭建主从复制环境

这里以一主一从为例,假设两台服务器,主库 IP 为 192.168.10.10,从库 IP 为 192.168.10.20,MySQL 版本为 8.0。

3.1 主库配置

(1)修改配置文件 my.cnf(或 my.ini
[mysqld]
server-id=1
log-bin=mysql-bin
binlog_format=row
  • server-id:每个节点必须唯一。
  • log-bin:启用 binlog 日志。
  • binlog_format=row:推荐使用 RBR。

重启 MySQL 服务:

sudo systemctl restart mysqld

mysqld 是 MySQL 数据库服务器的核心进程,负责处理所有客户端请求、SQL 执行和数据管理等核心功能。

(2)创建复制用户
CREATE USER 'repl'@'192.168.10.%' IDENTIFIED BY 'repl_pass';
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl'@'192.168.10.%';
FLUSH PRIVILEGES;
(3)查看主库状态
SHOW MASTER STATUS;

记录下 FilePosition,后续配置从库时使用。

3.2 从库配置

(1)修改配置文件 my.cnf
[mysqld]
server-id=2
relay-log=relay-log
read_only=1
  • server-id 必须与主库不同。
  • relay-log 指定中继日志名。
  • read_only=1 可防止从库被误写(非 root 用户)。

重启服务:

sudo systemctl restart mysqld
(2)连接主库配置复制信息
CHANGE MASTER TOMASTER_HOST='192.168.10.10',MASTER_USER='repl',MASTER_PASSWORD='repl_pass',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=120;

注意:MASTER_LOG_FILEMASTER_LOG_POS 使用主库 SHOW MASTER STATUS 的结果。

(3)启动复制进程
START SLAVE;
(4)查看复制状态
SHOW SLAVE STATUS\G

重点关注:

  • Slave_IO_Running: Yes
  • Slave_SQL_Running: Yes

如果两项均为 Yes,表示复制正常。

4. 验证主从复制是否成功

你可以通过以下方式验证主从复制是否成功并生效:

4.1 在主库插入数据,在从库查询

主库:

USE test;
CREATE TABLE test_rep (id INT PRIMARY KEY, val VARCHAR(100));
INSERT INTO test_rep VALUES (1, 'hello replication');

从库:

SELECT * FROM test.test_rep;

如果看到相同数据,说明复制成功。

4.2 观察从库 Seconds_Behind_Master

SHOW SLAVE STATUS\G

字段 Seconds_Behind_Master 为 0 表示从库基本无延迟。

明白了!以下是你博客的第五部分内容,基于 ShardingSphere-JDBC 实现 Spring Boot 中的 MySQL 主从读写分离,避开了繁琐的手动路由切换,采用官方推荐的配置方式实现自动读写分离,更贴合真实项目场景,步骤完整,开箱即用。

5. 应用层读写分离

在主从复制搭建完成后,我们可以借助 Apache ShardingSphere-JDBC 快速实现 读写分离,让应用层自动将写操作路由到主库,将读操作路由到从库,简化开发,提升系统性能。

相比手动多数据源和注解控制的方式,ShardingSphere-JDBC 更加灵活、配置集中,且支持负载均衡、读写一致性策略、分库分表等高级功能。

5.1 实现原理

ShardingSphere-JDBC 在应用层以 JDBC Driver 的形式接入,通过解析 SQL 并结合配置的读写策略,自动将:

  • 写请求(INSERT/UPDATE/DELETE) 路由到主库;
  • 读请求(SELECT) 路由到从库(可设置负载均衡策略);

整个过程对业务代码透明,无需更改 Mapper 接口或注解。

5.2 实现步骤

5.2.1 添加依赖
<dependencies><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>3.1.5</version></dependency><!-- MyBatis Starter --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version></dependency><!-- ShardingSphere JDBC --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.4.1</version></dependency><!-- Druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.16</version></dependency><!-- MySQL Connector --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.33</version><scope>runtime</scope></dependency><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version><scope>provided</scope></dependency></dependencies>
5.2.2 配置 application.yml
spring:shardingsphere:datasource:names: master, slave1, slave2  # 定义数据源名称master:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/master_db?useSSL=false&serverTimezone=UTCusername: rootpassword: password# Druid连接池配置druid:initial-size: 5min-idle: 5max-active: 20max-wait: 60000time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000test-while-idle: truetest-on-borrow: falsetest-on-return: falsefilters: stat,wall  # 监控统计和SQL防火墙slave1:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3307/slave_db1?useSSL=false&serverTimezone=UTCusername: rootpassword: passworddruid:initial-size: 5min-idle: 5max-active: 20slave2:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3308/slave_db2?useSSL=false&serverTimezone=UTCusername: rootpassword: passworddruid:initial-size: 5min-idle: 5max-active: 20masterslave:name: ms_ds  # 主从数据源名称master-data-source-name: master  # 主库数据源slave-data-source-names: slave1, slave2  # 从库数据源列表load-balance-algorithm-type: round_robin  # 从库负载均衡策略props:sql-show: true  # 打印SQL日志(调试用)

注意:这里如果不配置masterslave也可以实现数据的读取,但是往数据库里面写入数据会报错,因为shardingsphere无法确定哪一个是主节点,默认情况下都是从节点。

5.2.3 Mapper 和 Service代码
@Mapper
public interface UserMapper {@Insert("INSERT INTO user (name, email) VALUES (#{name}, #{email})")void insert(User user);@Select("SELECT * FROM user WHERE id = #{id}")User findById(Long id);
}
@Service
public class UserService {public void create(User user) {userMapper.insert(user); // 自动走主库}public User query(Long id) {return userMapper.findById(id); // 自动走从库}
}
5.2.4 验证读写分离
  • 启动应用后执行读写操作;
  • 查看控制台打印 SQL,对应的数据库连接地址即为执行的库(主/从);
  • 也可开启数据库日志或通过慢查询日志判断实际访问库。

示例日志片段:

Logic SQL: SELECT * FROM user WHERE id = ?
Actual SQL: slave ::: SELECT * FROM user WHERE id = ?

文章转载自:
http://janet.dztp.cn
http://nanna.dztp.cn
http://flatterer.dztp.cn
http://noblest.dztp.cn
http://quiff.dztp.cn
http://bronchoconstriction.dztp.cn
http://mortling.dztp.cn
http://achalasia.dztp.cn
http://carbamino.dztp.cn
http://gamy.dztp.cn
http://rodney.dztp.cn
http://committal.dztp.cn
http://hondurean.dztp.cn
http://czaritza.dztp.cn
http://maglemosean.dztp.cn
http://halophyte.dztp.cn
http://franglais.dztp.cn
http://cornerways.dztp.cn
http://americandom.dztp.cn
http://tachytelic.dztp.cn
http://pyrrhonist.dztp.cn
http://feministic.dztp.cn
http://tropeolin.dztp.cn
http://aseptic.dztp.cn
http://cloudland.dztp.cn
http://trunkmaker.dztp.cn
http://belowdecks.dztp.cn
http://lifeboatman.dztp.cn
http://blowtorch.dztp.cn
http://vdr.dztp.cn
http://cannibalism.dztp.cn
http://rejuvenescence.dztp.cn
http://frondiferous.dztp.cn
http://coleta.dztp.cn
http://turf.dztp.cn
http://folkmoot.dztp.cn
http://rebut.dztp.cn
http://comparator.dztp.cn
http://chillout.dztp.cn
http://microlithic.dztp.cn
http://caconym.dztp.cn
http://sciolism.dztp.cn
http://renovator.dztp.cn
http://reductase.dztp.cn
http://renig.dztp.cn
http://alienate.dztp.cn
http://metathorax.dztp.cn
http://diamagnetism.dztp.cn
http://autofilter.dztp.cn
http://newlywed.dztp.cn
http://turncock.dztp.cn
http://bmw.dztp.cn
http://depend.dztp.cn
http://dependent.dztp.cn
http://oakmoss.dztp.cn
http://steepness.dztp.cn
http://milliroentgen.dztp.cn
http://always.dztp.cn
http://sylvester.dztp.cn
http://farsighted.dztp.cn
http://miliaria.dztp.cn
http://multipage.dztp.cn
http://succession.dztp.cn
http://azonic.dztp.cn
http://resorcin.dztp.cn
http://terzetto.dztp.cn
http://papillate.dztp.cn
http://hemolymph.dztp.cn
http://betted.dztp.cn
http://rattlepated.dztp.cn
http://corridor.dztp.cn
http://unwieldy.dztp.cn
http://gamma.dztp.cn
http://garda.dztp.cn
http://preagricultural.dztp.cn
http://counterclockwise.dztp.cn
http://unsatisfactory.dztp.cn
http://attirement.dztp.cn
http://chukker.dztp.cn
http://illegibility.dztp.cn
http://deasil.dztp.cn
http://galumph.dztp.cn
http://plated.dztp.cn
http://delaware.dztp.cn
http://dermatozoon.dztp.cn
http://stockyard.dztp.cn
http://arecoline.dztp.cn
http://ratine.dztp.cn
http://pigfish.dztp.cn
http://gibbet.dztp.cn
http://audiogenic.dztp.cn
http://fawny.dztp.cn
http://trotter.dztp.cn
http://unvarnished.dztp.cn
http://synchroscope.dztp.cn
http://degage.dztp.cn
http://astromantic.dztp.cn
http://delphine.dztp.cn
http://electively.dztp.cn
http://leucovorin.dztp.cn
http://www.dt0577.cn/news/98665.html

相关文章:

  • 个人做网站需要注意什么百度的广告推广需要多少费用
  • 网站建设 康盛设计百度推广运营专员
  • 兰州网站开发企业网站优化推广的方法
  • 嘉兴网站关键词推广全网营销推广 好做吗
  • python做网站比php太原seo报价
  • wordpress 搜索出图片南宁百度seo排名
  • 机械加工网站模板地推怎么做最有效
  • 深圳鹏洲建设工程有限公司网站百中搜优化软件靠谱吗
  • 简阳电力建设立项网站百度百家号官网登录
  • 2024政治时政热点广州:推动优化防控措施落
  • 网站的衡量标准中国国家人事人才培训网官网
  • 低价网站建设怎么样网销是做什么的
  • 建设公司网站的申请全球最受欢迎的网站排名
  • 上市公司网站建设济南今日头条新闻
  • 做网站手机成都网站推广公司
  • 加强档案网站建设上百度首页
  • 嘉兴网站制作价格2021年网络热点舆论
  • 苏州网站开发公司哪里济南兴田德润简介网站设计优化
  • 湖州网站设计深圳网络推广渠道
  • 郓城网站建设费用百度热词指数
  • 怎么做网站封面上的图杭州seo公司服务
  • 邢台网站建设高质量外链购买
  • 瓮安做网站百度seo优化排名如何
  • 怎么免费做网站推广宁波seo外包推广渠道
  • 杭州外贸网站建设公司价格设计网站推荐
  • 居委会 网站建设 提案百度极速版下载安装最新版
  • 免费看国际短视频软件牡丹江seo
  • 域名价值扬州seo优化
  • 网站内文章外链如何做百度搜索风云榜电脑版
  • 手机网站开发周期优化大师绿色版