- 5、mysql主从复制原理
-
主库db的更新事件(update、insert、delete)被写到binlog
-
主库创建一个binlog dump thread,把binlog的内容发送到从库
-
从库启动并发起连接,连接到主库
-
从库启动之后,创建一个I/O线程,读取主库传过来的binlog内容并写入到relay log
-
从库启动之后,创建一个SQL线程,从relay log里面读取内容,从Exec_Master_Log_Pos位置开始执行读取到的更新事件,将更新内容写入到slave的db
二、springboot项目多数据源配置,实现读写分离
-
1、主从多数据源配置
-
yml配置
server:
port: 8888
servlet:
encoding:
charset: UTF-8
force: true
enabled: true
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3307/test?serverTimezone=GMT%2B8&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=false
username: root
password: 654321
slave:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3308/test?serverTimezone=GMT%2B8&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=false
username: root
password: 654321
- 数据源配置
/**
- @author cgg
**/
@Configuration
@Ena