当前位置:网站首页>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 .
边栏推荐
- How to use the cloud game server is the cloud server stable
- Pan micro reached cooperation with Tencent to help enterprises connect with banking services and support enterprise digital upgrading
- How to enable IPv6 network access for personal broadband
- How to build video websites? What are the types of video websites?
- How to recover the garbled words in the software?
- If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations
- How many graphics cards are required for cloud game servers? What should be paid attention to when purchasing servers
- Cloud recommendation Vol.1: quick start to remote control under f-stack, go modules and 5g!
- Designing complex messaging systems using bridging patterns
- [security] Tencent public cloud released the revolutionary function of "driving threat operation" of the new SOC security operation center!
猜你喜欢

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

Advanced BOM tool intelligent packaging function

Introduction to development model + test model

application. Yaml configuring multiple running environments

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

Leetcode969: pancake sorting (medium, dynamic programming)

BIM model example

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations
Cloudpods golang practice

163 mailbox login portal display, enterprise mailbox computer version login portal
随机推荐
Build your own cloud game server. What if the cloud game server is attacked
A complete collection of SQL commands. Each command has an example. Xiaobai can become a God after reading it!
Centeros environment setup
How to batch output ean 13 code to pictures
Start tcapulusdb process
Must the company domain name have a trademark registration? What if the registered domain name is rejected?
An attempt to use Navicat tool to copy and export MySQL database data
Mainstay of network detection - nping User Guide
The difference between classless routing and classless routing
Leetcode969: pancake sorting (medium, dynamic programming)
What are the main functions of DNS? What are the benefits of IP address translation
How to protect your code - ollvm (1)
Cloud game cannot select a server cloud game server fees
Echo framework: add API logging Middleware
Coding -- the leader of R & D tools in the cloud native Era
BIM model example
Facebook was officially renamed meta, and the digital twin meta universe was applied!
163 mailbox login portal display, enterprise mailbox computer version login portal
Efficient Internet access and systematic learning
How to register a trademark? How to improve the passing rate of trademark registration?