当前位置:网站首页>MySQL read / write separation
MySQL read / write separation
2022-07-19 14:41:00 【After all, Yin Jianwei】
Read / write separation
1、MySQL Master slave copy
Introduce :
MySQL Master-slave replication means that data can be copied from one MySQL The database server master node is replicated to one or more slave nodes .MySQL Asynchronous replication is used by default , In this way, the slave node does not need to visit the master server all the time to update its data , Data can be updated on a remote connection , The slave node can copy all databases in the master database or specific databases , Or a specific watch .


Configure the main library :
First step : modify MySQL Profile of the database /etc/my.cnf
log-bin=mysql-bin #[ must ] Enable binary logging
server-id = 100 #[ must ] The server is unique ID
The second step : restart MySQL service
systemctl restart mysqld
The third step : Sign in MySQL database , Execute the following SQL
-- Create a user for master-slave replication ,slave = user name % = On behalf of all IP All can be connected ( It can be changed into specific ip) 123456 = password
CREATE USER 'slave'@'%' IDENTIFIED with mysql_native_password by '1111';
-- Give the user permission
GRANT replication slave on *.* to 'slave'@'%';
-- Refresh configuration
FLUSH PRIVILEGES;
-- Check whether the created user exists
use mysql;
select user, host from user;
-- View the current binlog Nodes and position value
show master status;

Configure slave Slave
First step : modify MySQL Profile of the database
server-id = 101 #[ must ] The server is unique ID
The second step : restart MySQL service
The third step : Sign in MySQL, Execute the following SQL
-- Add master database information and read binlog Node information of
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 from library
START SLAVE;
-- Check the master-slave status
SHOW SLAVE STATUS;
2、 Read write separation cases (Sharding-JDBC)
1、Sharding-JDBC Introduce

2、Sharding-JDBC Introductory cases
1、 Import dependence :
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2、 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://ip/rw?serverTimezone=Asia/Shanghai
username: root
password: 1111
# From a data source
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:
# Read write separation configuration
load-balance-algorithm-type: round_robin
# 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
# 3、 Allow... To be configured in the configuration file Bean Define override configuration items
main:
allow-bean-definition-overriding: true
3、 The project realizes the separation of reading and writing
1、 Import dependence :
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2、 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://ip/reggie?serverTimezone=Asia/Shanghai
username: root
password: 1111
# From a data source
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:
# Read write separation configuration
load-balance-algorithm-type: round_robin
# 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
# 3、 Allow... To be configured in the configuration file Bean Define override configuration items
main:
allow-bean-definition-overriding: true
边栏推荐
- Overview report of Chinese AI medical imaging industry in 2022
- Win10 Microsoft Store打不开(开启TLS 1.2)
- Zhikanghu property elderly care service plan
- 深入理解事务隔离级别
- C语音 杨氏矩阵 · 左旋字符串 · 判断字符串是否旋转
- 详解C语言文件操作
- Colliding Mice碰撞老鼠工程分析
- Is it true that tongdaxin opens an account? Is it safe for tongdaxin to open an account?
- ospf 附录 防环 重发布
- SQL相关的时间日期类型
猜你喜欢

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言

微信小程序---wxss模板样式

ospf-LSA

Redis source code and design analysis -- 3 Dictionaries

Data consistency between redis and MySQL

Méthode de compilation de la courbe RPS d'O'Neill (originale par le Dr Tao)

Optimal Biking Strategy【DP + 二分】

敏捷的第一步:把 “迭代” 变为 “冲刺” 开始!

Homework on the first day of summer rhcsa training

C语音 杨氏矩阵 · 左旋字符串 · 判断字符串是否旋转
随机推荐
JSON path syntax introduction and usage scenarios
Optimal Biking Strategy【DP + 二分】
详解C语言文件操作
Code runner for vs code, with more than 40million downloads! Support more than 50 languages
QChartView添加在QGridLayout中时覆盖了之前的控件
ShanDong Multi-University Training #3
MySQL CPU使用率飙升,如何定位是被谁占用了
常见的内置函数、可迭代对象、迭代器对象、异常捕获、异常捕获的用途、生成器对象、模块、绝对导入与相对导入、包的概念、模块
Redis源码与设计剖析 -- 1.简单动态字符串
Use tongweb's hot deployment function with caution
Display module in pyGame
Méthode de compilation de la courbe RPS d'O'Neill (originale par le Dr Tao)
4 a company has branches in six cities C1, C2, C3... C6. The connection between cities Ci and CJ (I, j=1,2,3,... 6) and the cost are listed in the following weighted adjacency matrix C
C - usage of this
Compréhension initiale de la fonction - partie 2
JVM performance optimization
Explain the operation of C language file in detail
[Luogu p3220] and not (construction) (digit DP) (inference)
华为无线设备配置动态负载均衡
matplotlib绘制多折线图(解决matplotlib中文无法显示问题)