Getting Started Guide to MySQL Master-Slave Replication: Basic Concepts and Configuration Steps


foreword

In order to consolidate the knowledge learned, the author tried to start publishing some blogs of learning notes for future review. Of course, it would be great if it could help some newcomers learn new technologies. The author is a piece of food. If there are any mistakes in the records in the article, readers and friends are welcome to criticize and correct.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

1. Problem analysis

All the pressure of reading and writing is borne by one database. If the disk of the database server is damaged under high pressure, the data will be lost, which is a single point of failure.

insert image description here

After the read-write separation optimizer

insert image description here

Two, Mysql master-slave replication

1 Introduction

1. What is Mysql master-slave replication

  1. MySOL master-slave replication is an asynchronous replication process, and the bottom layer is based on the binary log function that comes with the Mysgl database.
  2. It means that one or more MySOL databases (slave, that is, the slave library) copy the log from another MySL database (master, that is, the main library), and then parse the log and apply it to itself, and finally realize the data of the slave library and the main library data remains consistent.
  3. MySOL master-slave replication is a built-in function of the MySOL database without the need for third-party tools.

2. The MySQL replication process is divided into three steps

  1. The master records the changes to the binary log (binary log)
  2. The slave copies the master's binary log to its relay log (relay log)
  3. The slave redoes events in the relay log and applies the changes to its own database

insert image description here

2. Configuration

1. Configuration - Preconditions

Prepare two servers in advance, install Mysq1 respectively and start the service successfully

  1. Master library Master 192.168.154.129
  2. From the library slave 192.168.154.130

insert image description here

2. Configuration - main library master

  • Step 1: Modify the configuration file /etc/my.cnf of the Mysq1 database

[mysqld]
log-bin=mysql-bin #[must] enable binary log
server-id=100 #[must] server unique ID

insert image description here

  • Step 2: Restart the Mysql1 service

systemctl restart mysqld

  • Step 3: Log in to the Mysql database and execute the following SQL
  1. GRANT REPLICATION SLAVE ON * .* to ‘xiaoming’@‘%’ identified by ‘Root@123456’;
  2. The role of the above SOL is to create a user xiaoming, the password is Root@123456, and grant the REPLICATION SLAVE permission to the xiaoming user. It is often used to establish the user authority required for replication, that is, the slave must be authorized by the master to be a user with this authority, in order to be able to replicate through this user.

注1: ON * .* 中 * 和 . 之间是没有空格的,此处由于MD语法格式空一格

insert image description here

  • Step 4: Log in to the Mysql database, execute the following SQL, and record the values ​​​​of File and Position in the result

show master status;

insert image description here
注:上面SOL的作用是查看Master的状态,执行完此SQL后不要再执行任何操作

3. Configuration - from the library slave

  • Step 1: Modify the configuration file /etc/my.cnf of the Mysg1 database

[mysqld]
server-id=101 #[required] server unique ID

insert image description here

  • Step 2: Restart the Mysql1 service

systemctl restart mysqld

  • Step 3: Log in to the Mysq1 database and execute the following SOL
  1. change master to
    master_host=‘192.168.154.129’,master_user=‘xiaoming’,master_password=‘Root@123456’,master_log_file=‘mysql-bin.000001’,master_log_pos=441;
  2. start slave;

000001 和 441 是根据 master 库决定的

insert image description here

  • Step 4: Log in to the Mysql database, execute the following SQL, and check the status of the slave database

show slave status;

insert image description here

3. Test

insert image description here

Three, read and write separation case

1. Background

In the face of increasing system access, the throughput of the database is facing a huge bottleneck. For application systems with a large number of concurrent read operations and fewer write operations at the same time, the database is split into a master database and a slave database. Effectively avoid row locks caused by data updates, greatly improving the query performance of the entire system.

insert image description here

2. Introduction to shardingDBC

1. Introduction to shardingDBC

Sharding-IDBC is positioned as a lightweight lava framework, an additional service provided by the DBC layer of ava. It uses the client to directly connect to the database and provides services in the form of jar packages without additional deployment and dependencies. It can be understood as an enhanced version of the JDBC driver, fully compatible with ]DBC and various ORM frameworks. Using sharding-JDBC can easily realize the separation of database reading and writing in the program.

2. Features of shardingDBC

  1. Applicable to any]DB-based ORM framework, such as: JPA, Hibernate, Mybatis, SpringJDBCTemplate or use DBC directly.
  2. Support any third-party database connection pool, such as: DBCP, C3PO, BoneCp, Druid, HikariCP, etc.
  3. Any database that implements the ]DBC specification is supported. Currently supports MySQL, Oracle, SQLServer, PostgresQL and any database that follows the SQL92 standard.

3. Getting Started Case

  1. Import maven coordinates
 <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>
  1. Configure read-write separation rules in the configuration file
server:
  port: 8080
mybatis-plus:
  configuration:
    #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id-type: ASSIGN_ID
spring:
  shardingsphere:
    datasource:
      names:
        master,slave
      # 主数据源
      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
      # 从数据源
      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:
      # 读写分离配置
      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. Configure in the configuration file to allow bean definitions to override configuration items

  //和spring同级
  main:
    allow-bean-definition-overriding: true

4. Functional testing

insert image description here
insert image description here

Summarize

Everyone is welcome to leave a message for exchange and criticism. If the article is helpful to you or you think the author's writing is not bad, you can click to follow, like, and bookmark to support.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/132101976