当前位置:网站首页>Read write separation master-slave replication of MySQL
Read write separation master-slave replication of MySQL
2022-06-27 21:55:00 【PoJo123】
notes : This article is based on the relevant videos and materials of dark horse programmer , The code is simple , It's easier to understand , If you have any questions or source code information, you can leave a message in the comment area or contact the author !
List of articles
The opening
Read and write all the pressure to amuse a database to bear , High pressure , If the database server disk is damaged, the data will be lost , A single point of failure , It can realize the read-write separation of multiple databases 
One 、Mysql Master slave copy
(1) Introduce
Mysql The master-slave replication of is an asynchronous replication process , The bottom layer is based on Mysql Binary log function of database . It means that one machine will have more than one Mysql database (slave, From the library ) From the other Mysql database (maseter, The main library ), Copy the log, then parse the log and apply it to itself , Finally, the data of the slave database is consistent with that of the master database .Mysql The master-slave copy of Mysql The functions of the database , No need to use third-party tools ;
Mysql The replication process is divided into three steps :
- master Record changes to binary (binary log)
- slave take master Of binary log Copy to its trunk log (relay log)
- slave Redo events in relay log , Apply the changes to your own database

(2) To configure
(1)
(2)
The server is unique Id It's not fixed
(3)
(4)
Create a new user in the main library , To facilitate data access from the library ;
(5) see Master Log file path for the library , Note that the file path will change when you execute the following commands before executing other commands , So try not to perform other operations 
(6) Configuration from library 
(7)
(8) Configure according to the previously configured information 
(9) Check whether the slave database has been configured 
(10) To test , Perform an operation in the main library , You can also see the result of the operation from the library ;
Two 、 Read write separation cases
(1) background
In the face of increasing system access , The throughput of the database is facing a huge bottleneck , For applications with a large number of concurrent operations and fewer write operations at this time , Split the database into master and slave , The main database is responsible for transactional addition, deletion, modification and query , The slave library is responsible for query operations , It can effectively avoid row lock caused by data update , The query performance of the whole system has been greatly improved ;
(2)Sharding-JDBC Introduce

(3)sharding-jdbc Use
Import maven coordinate

Configure the read-write separation rule in the configuration file
spring:
shardingsphere:
datasource:
names:
master,slave
# The main data source
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.138.100:3306/rw?characterEncoding=utf-8
username: root
password: root
# From a data source , Can be more
slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.138.101:3306/rw?characterEncoding=utf-8
username: root
password: root
masterslave:
# Read write separation configuration , Load balancing
load-balance-algorithm-type: round_robin # polling
# The final data source name
name: dataSource
# Main database data source name
master-data-source-name: master
# From the list of library data source names , Multiple commas separate
slave-data-source-names: slave
props:
sql:
show: true # Turn on SQL Show , Default false
- Allow... To be configured in the configuration file bean Define override configuration items
Created after datasource It's going to cover the front datasource;
If you feel the content is well written , You can't get lost with one button three times !!!!
More learning will be updated later , Let's study together !!!!!!
边栏推荐
- [LeetCode]30. 串联所有单词的子串
- Common problems encountered by burp Suite
- GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
- A method of go accessing gbase 8A database
- [LeetCode]508. 出現次數最多的子樹元素和
- oracle迁移mysql唯一索引大小写不区分别怕
- 石子合并问题分析
- At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
- [LeetCode]动态规划解分割数组I[Red Fox]
- Quick excel export according to customized excel Title Template
猜你喜欢
随机推荐
[LeetCode]动态规划解拆分整数I[Silver Fox]
Express e stack - small items in array
[LeetCode]161. Edit distance of 1
MYSQL和MongoDB的分析
[LeetCode]515. 在每个树行中找最大值
畅游动态规划之区间DP
Luogu p5706 redistributing fertilizer and house water
Xiao Wang's interview training task
Magic POI error in reading excel template file
Process control task
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
[LeetCode]513. 找树左下角的值
Go from entry to practice -- CSP concurrency mechanism (note)
Go from introduction to actual combat - context and task cancellation (notes)
Bean paste green protects your eyes
Go from introduction to practice -- coordination mechanism (note)
Matlab finds the position of a row or column in the matrix
Go从入门到实战——协程机制(笔记)
GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
List of language weaknesses --cwe, a website worth learning










