MySQL-database read and write separation (below)

♥️Author : Xiao Liu at Station C

♥️Personal homepage:  Xiao Liu's homepage 

♥️Efforts may not necessarily pay off, but there will be gains! Come on! Work hard together for a better life!

♥️ Learn the operation and maintenance experience summed up in two years, and a full set of network experiment tutorials for Cisco simulators. Column: Cloud Computing Technology

♥️Xiao Liu private message can ask casually, as long as you know it, you will not be stingy, thank you CSDN for letting you and me meet!

foreword

The last chapter talked about MySQL read and write separation (middle) this chapter continues

Table of contents

MySQL

5 Dual-master and dual-slave read and write separation

5.1 Configuration

1). schema.xml

2). user.xml

5.2 Testing

Finish


MySQL

MySQL is a relational database management system , developed by the Swedish company MySQL AB , which is a product of Oracle . MySQL is one of the most popular relational database management systems . In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System, relational database management system ) application software. MySQL is a relational database management system. Relational databases store data in different tables instead of putting all the data in one big warehouse, which increases speed and improves flexibility. The SQL language used by MySQL is the most commonly used standardized language for accessing databases. MySQL software adopts a dual authorization policy, which is divided into community edition and commercial edition . Due to its small size, fast speed, low overall cost of ownership, and especially the open source feature, MySQL is generally chosen as the website database for the development of small, medium and large websites .

4.4 Testing
Execute DDL and DML statements on the two master databases Master1 and Master2 respectively to check the data synchronization status of the database servers involved.
create database db01;
use db01;
create table tb_user(
id int(11) not null primary key ,
name varchar(50) not null,
sex varchar(1)
)engine=innodb default charset=utf8mb4;
insert into tb_user(id,name,sex) values(1,'Tom','1');
insert into tb_user(id,name,sex) values(2,'Trigger','0');
insert into tb_user(id,name,sex) values(3,'Dawn','1');
insert into tb_user(id,name,sex) values(4,'Jack Ma','1');
insert into tb_user(id,name,sex) values(5,'Coco','0');
insert into tb_user(id,name,sex) values(6,'Jerry','1');
Execute DML and DDL operations in Master1 to see if the data can be synchronized to the other three databases.
Perform DML and DDL operations in Master2 to see if the data can be synchronized to the other three databases.

After completing the construction of the above-mentioned dual-master and dual-slave structure, next, let’s take a look at how to complete this dual-master-slave read-write separation.

5 dual master dual slave read and write separation

5.1 Configuration

MyCat controls the read-write separation and load balancing of the background database by the balance attribute of the datahost tag in the schema.xml file .
Control, through writeType and switchType to complete automatic switching on failure.

1). schema.xml

Configure logic library:
<schema name="ITCAST_RW2" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn7">
</schema>
Configure data nodes:
<dataNode name="dn7" dataHost="dhost7" database="db01" />
Configure the node host:
<dataHost name="dhost7" maxCon="1000" minCon="10" balance="1" writeType="0"
dbType="mysql" dbDriver="jdbc" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="master1" url="jdbc:mysql://192.168.200.211:3306?
useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8"
user="root" password="1234" >
<readHost host="slave1" url="jdbc:mysql://192.168.200.212:3306?
useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8"
user="root" password="1234" />
</writeHost>
<writeHost host="master2" url="jdbc:mysql://192.168.200.213:3306?
useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8"
user="root" password="1234" >
<readHost host="slave2" url="jdbc:mysql://192.168.200.214:3306?
useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8"
user="root" password="1234" />
</writeHost>
</dataHost>
The specific corresponding situations are as follows:

 Property description:

balance="1"
Represents all readHost and stand by writeHost participating in the load balancing of the select statement, simply
Simply speaking, when the dual-master and dual-slave mode (M1->S1 , M2->S2 , and M1 and M2 are mutually active and standby ) , under normal circumstances,
M2, S1, and S2 all participate in the load balancing of the select statement ;
writeType
0: All write operations are forwarded to the first writeHost , if writeHost1 hangs up , it will switch to writeHost2 ;
1: All write operations are randomly sent to the configured writeHost ;
switchType
-1 : Do not switch automatically
1 : automatic switching

2). user.xml

Configure the root user to also have access to the logical library ITCAST_RW2 .
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">SHOPPING,ITCAST,ITCAST_RW2</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="true">
<schema name="DB01" dml="0110" >
<table name="TB_ORDER" dml="1110"></table>
</schema>
</privileges>
-->
</user>

5.2 Test

Log in to MyCat , test query and update operations, and determine whether read-write separation is possible and whether the read-write separation strategy is correct.
When one of the main databases is down, can it be automatically switched?

Finish

Alright, this is the end of our MySQL read-write separation, and the MySQL series will also be put in a paragraph. Thank you for your support all the way, thank you, and thank CSDN for letting you and me meet!

♥️Following is the driving force for my creation

♥️Like, is the greatest recognition for me

♥️This is Xiaoliu, I am inspiring to do every article well, thank you everyone

Guess you like

Origin blog.csdn.net/lzl10211345/article/details/131837628