当前位置:网站首页>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 3306

Two . 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
  1. 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;

  1. 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;



![](https://ask8088-private-1251520898.cn-south.myqcloud.com/developer-images/article/8880527/olx7yuoe8i.png?q-sign-algorithm=sha1&q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&q-sign-time=1635478227;1635485427&q-key-time=1635478227;1635485427&q-header-list=&q-url-param-list=&q-signature=9a66cbc0e07f49d580ba23f2837d15ccc742930c)



![](https://ask8088-private-1251520898.cn-south.myqcloud.com/developer-images/article/8880527/kde9k318zy.png?q-sign-algorithm=sha1&q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&q-sign-time=1635478234;1635485434&q-key-time=1635478234;1635485434&q-header-list=&q-url-param-list=&q-signature=bbaa48a51c13a8667f01bbf64dc4859a491934f6)

## The following two parameters are Yes, The master-slave configuration is successful !
  1. 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;
  1. 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 .
原网站

版权声明
本文为[Extraordinary]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/10/20211029145342086S.html

随机推荐