当前位置:网站首页>Using the database middleware MYCAT to realize read-write separation (dual master and dual slave)
Using the database middleware MYCAT to realize read-write separation (dual master and dual slave)
2022-06-24 02:28:00 【Extraordinary】
Let's continue with the previous document ! Because we still need to use the above mysql database , Make some configuration in this
First delete the database testdb, Then close it slave And reset ( On two mysql All the above must be carried out )
drop database testdb stop slave reset master
Next, let's start a new chapter , In this chapter we will realize mysql Dual master and dual slave read / write separation ( High availability ).
We are already in front of 192.168.10.1 and 192.168.10.2 Built on mysql, And then we have 192.168.10.3 and 192.168.10.4 To build mysql
Service planning :
role | IP Address | Host name |
|---|---|---|
master1 | 192.168.10.1 | Master1 |
slave1 | 192.168.10.2 | Slave1 |
master2 | 192.168.10.3 | Master1 |
slave2 | 192.168.10.4 | Slave2 |
One . Prepare the front-end environment ( build msyql)
stay 192.168.10.3 and 192.168.10.4 To build mysql service
yum install -y mariadb-server mariadb
systemctl start mariadb && systemctl enable mariadb
# Get into /etc/my.cnf
add to :
[mysqld]
skip-grant-tables
restart mysql
systemctl restart mariadb
mysql -uroot -p
enter
Set up root The password for
UPDATE mysql.user SET password = PASSWORD('123456') WHERE user = 'root';
exit
# Get into /etc/my.cnf
stay [mysqld] Comment out the paragraph of :skip-grant-tables Save and exit vi.
restart mysql
systemctl restart mariadb
## to grant authorization
mysql -uroot -p123456
grant all privileges on *.* to [email protected]'%' identified by '123456';
grant all privileges on *.* to [email protected]'localhost' identified by '123456';
flush privileges;
exit
## Verify database access ( stay 192.168.10.1 On )
mysql -uroot -p123456 -h 192.168.10.1 -P 3306
mysql -uroot -p123456 -h 192.168.10.2 -P 3306
mysql -uroot -p123456 -h 192.168.10.3 -P 3306
mysql -uroot -p123456 -h 192.168.10.4 -P 3306Two . Modify the configuration file
1. modify master1(192.168.10.1) Configuration file for
Modify the configuration file :vim /etc/my.cnf # The primary server is unique ID server-id=1 # Enable binary logging log-bin=mysql-bin # Set up the database not to be copied ( Multiple can be set ) binlog-ignore-db=mysql binlog-ignore-db=information_schema # Set up the database to be copied binlog-do-db= The name of the master database to be copied # Set up logbin Format binlog_format=STATEMENT # As a slave database , Update binary log files when there are write operations log-slave-updates # Represents the amount of each increment of the self growing field , It refers to the starting value of the auto increment field , The default value is 1, The value range is 1 .. 65535 auto-increment-increment=2 # Indicates the number from which the self growing field starts , Refers to how many fields are incremented at a time , His range is 1 .. 65535 auto-increment-offset=1
2. modify master2(192.168.10.3) Configuration file for
Modify the configuration file :vim /etc/my.cnf # The primary server is unique ID server-id=3 # Enable binary logging log-bin=mysql-bin # Set up the database not to be copied ( Multiple can be set ) binlog-ignore-db=mysql binlog-ignore-db=information_schema # Set up the database to be copied binlog-do-db= The name of the master database to be copied # Set up logbin Format binlog_format=STATEMENT # As a slave database , Update binary log files when there are write operations log-slave-updates # Represents the amount of each increment of the self growing field , It refers to the starting value of the auto increment field , The default value is 1, The value range is 1 .. 65535 auto-increment-increment=2 # Indicates the number from which the self growing field starts , Refers to how many fields are incremented at a time , His range is 1 .. 65535 Be careful : Self growing fields cannot be combined with master1 The same as auto-increment-offset=2
3. modify slave1(192.168.10.2) Configuration file for
Modify the configuration file :vim /etc/my.cnf # From the server only ID server-id=2 # Enable relay logging relay-log=mysql-relay
4. modify slave2(192.168.10.4) Configuration file for
Modify the configuration file :vim /etc/my.cnf # From the server only ID server-id=4 # Enable relay logging relay-log=mysql-relay
5. Dual host 、 Double slave restart mysql service Turn off firewall
systemctl restart mariadb && systemctl stop firewalld && systemctl disable firewalld
- stay master(192.168.10.1,192.168.10.3) Set up an account on the host and authorize slave
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456'; flush privileges;
7. see master(192.168.10.1,192.168.10.2) The state of
## Inquire about Master1 The state of
show master status;
# Inquire about Master2 The state of
show master status;
- stay slave1,slave2 Execute the following commands on the .
Slava1 Copy Master1,Slava2 Copy Master2
##Slava1 Copy command of CHANGE MASTER TO MASTER_HOST='192.168.10.1',MASTER_USER='slave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=473; ##Slava2 Copy command of CHANGE MASTER TO MASTER_HOST='192.168.10.3',MASTER_USER='slave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=473; ## Start the replication function of two slave servers start slave; # View the status of the slave server show slave status\G; ## The following two parameters are Yes, The master-slave configuration is successful ! ## Slave_IO_Running: Yes ## Slave_SQL_Running: Yes
9. Two master Copy each other ( Prepare for each other )
Master2 Copy Master1,Master1 Copy Master2
## Master1 Copy command of CHANGE MASTER TO MASTER_HOST='192.168.10.3',MASTER_USER='slave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=473; ## Master2 Copy command of CHANGE MASTER TO MASTER_HOST='192.168.10.1',MASTER_USER='slave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=473; # Start the replication function of two master servers start slave; # View the status of the slave server show slave status\G;   ## The following two parameters are Yes, The master-slave configuration is successful !
- Verify master-slave replication of dual master and dual slave
## effect : stay master1 New database 、 new table 、insert Record ,Master2 And copy from opportunity
## The following order is in master1 On the implementation create database testdb use testdb create table mytbl(id int,name varchar(20)); insert into mytbl values(1,'zhangsan'); ## The following order is in slave1,master2,slave2 On the implementation : select * from testdb.mytbl;
- modify Mycat Configuration file for schema.xml
modify <dataHost> Of balance attribute , Use this property to configure the type of read-write separation
Load balancing type , The current values are 4 The default is 0: (1)balance="0", Do not turn on the read-write separation mechanism , All read operations are sent to the currently available writeHost On . (2)balance="1", All of the readHost And stand by writeHost Participate in select Statement load balancing , To put it simply , When two masters and two slaves Pattern (M1->S1,M2->S2, also M1 And M2 Prepare for each other ), Under normal circumstances ,M2,S1,S2 All involved select Statement load balancing . (3)balance="2", All the reading operations are random in writeHost、readhost To distribute . (4)balance="3", All read requests are randomly distributed to readhost perform ,writerHost No pressure to read ## Dual master and dual slave mode selection :balance="1" One master and one slave :balance="3"
For dual master and dual slave read-write separation balance Set to 1
#balance="1": All of the readHost And stand by writeHost Participate in select Statement load balancing . #writeType="0": All writes are sent to the first of the configuration writeHost, The first one is cut to the second one that still exists #writeType="1", All writes are sent randomly to the configured writeHost,1.5 It is not recommended to discard it later #writeHost, After restart, the one after switching shall prevail , The switch is recorded in the configuration file :dnindex.properties . #switchType="1": 1 The default value is , Automatic switch . # -1 Does not automatically switch # 2 be based on MySQL The state of master-slave synchronization determines whether to switch .
12. Verify read / write separation and high availability
## Verify read-write separation ## Writing to host Master1 Database table mytbl Insert data with system variables into , Cause inconsistency between master and slave data mysql -uroot -p123456 INSERT INTO mytbl VALUES(2,@@hostname); mysql -umycat -p123456 -P 8066 -h 192.168.10.1 select * from mytbl; ## stay Mycat Look in mytbl surface , You can see the query statement in Master2(192.168.10.3)、Slava1(192.168.10.2)、Slava2(192.168.10.4) Switching between master and slave hosts
## verification mysql High availability stay master1(192.168.10.1) Enter the following command : systemctl stop mariadb systemctl status mariadb ## stay Mycat Inserting data into the is still successful ,Master2 Automatically switch to write host mysql -umycat -p123456 -P 8066 -h 192.168.10.1 INSERT INTO mytbl VALUES(3,@@hostname); select * from mytbl; ## stay Mycat Look in mytbl surface , You can see the query statement in Slave2(192.168.10.4) Search on ## And then at boot master1(192.168.10.1) Enter the following command : systemctl start mariadb systemctl status mariadb ## stay Mycat Look in mytbl surface , You can see the query statement in Master2(192.168.10.3)、Slava1(192.168.10.2)、Slava2(192.168.10.4) Switching between master and slave hosts summary :Master1、Master2 Mutual standby ,master1 After downtime, there will be master2 continue master1 Write work , But at this time, the query operation is only slave2 Conduct . When master1 recovery , At this point, there will be three query machines ,master2 Continue as write . When master2 After downtime , from master1 Become a write operation , At this time, the only read operation is slave1. in other words , When master Something goes wrong ,slave And will not continue to work .
边栏推荐
- What are the general contents of the enterprise website construction scheme
- application. Yaml configuring multiple running environments
- 5g Gigabit router dual band industrial grade
- Leetcode problem solving notes for slow ploughing of stupid cattle (dynamic update...)
- How to protect your code - ollvm (1)
- Super parameter tuning of neural network using keras tuner
- Code 128 barcode details
- How long can the trademark registration be completed? How to improve the speed of trademark registration?
- What is the domain name trademark? What are the registration conditions for domain names and trademarks?
- How to design cloud desktop server? What is the future of cloud desktop?
猜你喜欢

application. Yaml configuring multiple running environments

BIM model example

163 mailbox login portal display, enterprise mailbox computer version login portal
Cloudpods golang practice

Introduction to development model + test model

Advanced BOM tool intelligent packaging function

2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set

Leetcode969: pancake sorting (medium, dynamic programming)

How to fill in and register e-mail, and open mass mailing software for free

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations
随机推荐
Wechat open platform: OpenAPI, cloud development and basic management capability upgrade
November 1 global network security hotspot
Using robot framework to realize multi platform automated testing
The dealer management and control platform in the leather industry simplifies the purchase approval process and easily controls agents
Development status of industrial Internet
How to solve the problem of uncaught (in promise) when easywasmlayer plays a video?
A complete collection of SQL commands. Each command has an example. Xiaobai can become a God after reading it!
[tcapulusdb knowledge base] manually view the online operation of tcapulusdb
Tencent cloud double 11 Live Room activity rules
Modify the original place where the method needs to be called and triggered
Operation and maintenance platform tcapulusdb transaction management
An attempt to use Navicat tool to copy and export MySQL database data
How to recover the garbled words in the software?
2021 game security industry summit: Security Co Construction and healthy development of escort industry
Cloud rendering: cloud exhibition hall of Tencent digital ecology Conference - open roaming mode on cloud
Tencent peace of mind ecological alliance was officially established as a linkage partner. Open technology helps "digital agriculture"
Which cloud game service provider is more reliable when the cloud game server is open source
How long can the trademark registration be completed? How to improve the speed of trademark registration?
Benchmarking Shopify? Similarities and differences between "two giants" of Chinese e-commerce SAAS and Weimeng
Release of the first batch of bay area group standards! Tencent security takes the lead in developing trusted digital identity standards