Springboot+mysqlrouter+mybatis+mysql realizes the complete technical solution of read-write separation architecture

1、安装Mysql8
https://blog.csdn.net/atongmu2017/article/details/90610444
解决: You are not allowed to create a user with GRANT
https://blog.csdn.net/mxskymx/article/details/88765072

2. Configure the one-master and multiple-slave database architecture
https://blog.csdn.net/zyhlwzy/article/details/80569422
Supplementary note: The
binlog log records data change records and grows very rapidly, so you need to set the log expiration time, otherwise it will cause disk As the growth rate accelerates, even when the machine is down, you need to set the log expiration time, expire_logs_days = 7, and the specific expiration days are determined according to the database growth situation.
Insert picture description here

3. Configure mysqlrouter
https://blog.csdn.net/wzy0623/article/details/100518636
Supplementary instructions:
1. mysqlrouter is a database middleware, a bit similar to nginx, which distributes database link requests to various databases, but it is not correct The operation content is analyzed, so it can not realize the sub-database sub-table.
2. mysql8 contains the folder mysqlrouter, so after installing mysqlrouter, the version number may not be available. The version of mysqlrouter shows the version of the database. At this time, you need to adjust the order of environment variables to adjust.
Insert picture description here
Insert picture description here
3. About the configuration of mysqlrouter

[routing:secondary]
bind_address     = 192.168.2.34
bind_port        = 7001
destinations     = 192.168.2.40:3306,192.168.2.70:3306,192.168.2.75:3306
routing_strategy = round-robin

[routing:primary]
bind_address     = 192.168.2.34
bind_port        = 7002
destinations     = 192.168.2.75:3306
routing_strategy = first-available

In the 1-master-multi-slave architecture, I think the master library cannot be configured from the slave library, and a slave library cannot be randomly selected as the master library. The final result may cause data inconsistency and data disorder. The reasons are as follows:
1. The restart of the main library and network fluctuations are high probability events. Once the main library is temporarily inaccessible due to restart or network fluctuations, a slave library will be enabled as the master library, which will break the original master-slave configuration and write data to the slave library , The final data may be messy.
2. The data in the slave database may not be fully synchronized. We need to elect a slave database as the master database. We need to judge the data synchronization status and choose a slave database that is not fully synchronized as the master database. Finally, it may cause data inconsistency.
The method to judge whether the data is completely synchronized:
enter the slave database, show slave status\G;
view: Master_Log_File and Exec_Master_Log_Pos
Insert picture description here
enter the master database: show master status;
Insert picture description here
for comparison, if the instructions are consistent, the data is completely synchronized and can be elected as the master database.

4. Springboot realizes the separation of reading and writing .
https://www.cnblogs.com/wuyoucao/p/10965903.html
1. Basic principle:
In the application, through annotations, specify the data source used by the method, so that the separation of reading and writing is realized.
2. The data source points to mysqlrouter, which is forwarded by mysqlrouter. It is not a directly linked database. The account and password of the slave database must be consistent, otherwise the database access will fail.
#Configuration development environment

mysql:
  datasource:
    #读库数目
    num: 1
    type-aliases-package: com.example.demo.dao
    mapper-locations: classpath:mapper/*.xml
    config-location: classpath:/mybatis-config.xml
    write:
      url: jdbc:mysql://192.168.2.34:7002/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
    read:
      url: jdbc:mysql://192.168.2.34:7001/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver

5. Explore common data architecture
http://blog.sina.com.cn/s/blog_939c81b40102xw09.html

Guess you like

Origin blog.csdn.net/u011582840/article/details/105758328