mysql master-slave replication, read-write separation, dual-master dual-slave replication, read-write separation

MySQL master-slave replication

Overview

       Master-slave replication is to transfer the data of the master database to the slave database server through the binary log (binlog), and then re-execute these logs on the slave database, so that the data of the slave database and the master database are synchronized.

Insert image description here

principle

Insert image description here

build

Prepare two servers

Master:192.168.40.137

Slaver:192.168.40.138

1. Turn off the firewall

[root@mysql-master ~]# systemctl stop firewalld
[root@mysql-master ~]# systemctl disable firewalld
[root@mysql-slaver ~]# systemctl stop firewalld
[root@mysql-slaver ~]# systemctl disable firewalld

2.Install mysql

Install mysql on both servers and use ps to check whether the installation is successful.

[root@mysql-master ~]# ps aux|grep mysqld
[root@mysql-slaver ~]# ps aux|grep mysqld

3. Main library configuration

Modify the main library configuration file /etc/my.cnf

# mysql服务器id,保证整个集群环境中唯一
server-id=1
# 是否只读,1代表只读,0代表读写
read-only=0

Restart the MySQL server

[root@mysql-master ~]# systemctl restart mysqld

Log in to MySQL, create a remote connection account, and grant master-slave replication permissions

# 创建root用户,并设置密码,该用户可以在任意主机连接mysql服务
mysql> create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
# 为'root'@'%'用户分配主从复制权限
mysql> grant replication slave on *.* to 'root'@'%';

View binary coordinates

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000004 |      377 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+

4. Configuration from library

Modify the slave configuration file /etc/my.cnf

# mysql服务器id,保证整个集群环境中唯一
server-id=2
# 是否只读,1代表只读,0代表读写
read-only=1

Restart the MySQL server

[root@mysql-master ~]# systemctl restart mysqld

Log in to MySQL and set the main database configuration

mysql> change replication source to source_host="192.168.40.137",source_user="root",source_password='123456',source_log_fike='binlog.000004',source_log_pos=377;

Enable synchronization

mysql> start replica;

View master-slave replication status

mysql> show replica status\G;

Insert image description here

5. Test

Create a database, then create a table, then insert data to view

mysql> CREATE DATABASE MyDatabase;
Query OK, 1 row affected (0.01 sec)

mysql> USE MyDatabase;
Database changed
mysql> CREATE TABLE MyTable (
    ->   id INT NOT NULL AUTO_INCREMENT,
    ->   name VARCHAR(50) NOT NULL,
    ->   age INT NOT NULL,
    ->   PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO MyTable (name, age) VALUES
    ->   ('张三', 20),
    ->   ('李四', 25),
    ->   ('王五', 30);
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql>INSERT INTO MyTable (name, age) VALUES
    ->   ('张三', 20),
    ->   ('李四', 25),
    ->   ('王五', 30);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> update MyTable set age=20;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 6  Changed: 2  Warnings: 0

Insert image description here

MyCat implements reading and writing separation

       Read and write separation means to separate the read and write operations on the database to correspond to different database servers. The main database provides write operations, and the slave database provides read operations, which can effectively reduce the pressure on a single database.
Insert image description here

One master and one slave environment preparation

Check whether the master-slave replication environment of the previous master and slave is normal. Similarly, if the following effect occurs, it is normal.

Insert image description here

Configuration

MyCat controls the read-write separation and load balancing of the backend database by the balance attribute of the datahost tag in the schema.xml file.

balance value
Insert image description here

1. Modify the schema.xml file and specify the logical library and read-write host IP

 <!-- 新建一个逻辑库 dataNode为dn4-->
        <schema name="DB_RW" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn4" >
        </schema>
        <dataNode name="dn4" dataHost="dhost4" database="db1" />
        <!--新建一个主机dhost4 负载均衡策略为1-->
        <dataHost name="dhost4" maxCon="1000" minCon="10" balance="1"
                          writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- 写操作对应的主机-->
                <writeHost host="hostM1" url="jdbc:mysql://192.168.40.137:3306?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="123456">
                 <!-- 读操作对应的主机-->
                        <readHost host="hostS1" url="jdbc:mysql://192.168.40.138:3306?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="123456">
                        </readHost>
                </writeHost>
        </dataHost>

2. Add user permissions to access the database in the server.xml file

Insert image description here

3. Restart MyCat

[root@mysql-master mycat]# bin/mycat restart

Test view

Insert image description here

Because master-slave replication is set up, modifications to the data in the slave database will not affect the master database, so modify the data in the slave database and query the master database to see whether the queried data is from the slave database or the master database.

Insert image description here

You can see that the slave database has modified the data, and what is retrieved is the data from the slave database, realizing the separation of reading and writing.

Dual master dual slave introduction and environment preparation

1 Introduction

       One host Master1 is used to handle all write requests, and its slave Slave1 and another host Master2 and its slave Slave2 are responsible for all read requests. When the Master1 host goes down, the Master2 host is responsible for writing requests, and Master1 and Master2 serve as backup machines for each other.

Insert image description here

2. Environment preparation

server install software illustrate
192.168.40.137 JDK、MyCat、MySQL MyCat intermediate server
192.168.40.138 MySQL Master1
192.168.40.139 MySQL Slave1
192.168.40.170 MySQL Master2
192.168.40.171 MySQL Slave2

Turn off all firewalls

systemctl stop firewalld
systemctl disable firewalld

Configuration

Insert image description here

1. Main database configuration (Master1)

Modify the configuration file /etc/my.cnf

# 主库配置
# mysql服务器id,保证整个集群环境中唯一
server-id=1
# 指定同步的数据库
binlog-do-db=db01
binlog-do-db=db02
binlog-do-db=db03
# 在作为从数据库的时候,有写入操作也要更新二进制文件
log-slave-updates

Restart service

systemctl restart mysqld

2. Main library configuration (Master2)

Modify the configuration file /etc/my.cnf

# 主库配置
# mysql服务器id,保证整个集群环境中唯一
server-id=3
# 指定同步的数据库
binlog-do-db=db01
binlog-do-db=db02
binlog-do-db=db03
# 在作为从数据库的时候,有写入操作也要更新二进制文件
log-slave-updates

Restart service

systemctl restart mysqld

3. Create an account for master-slave replication

Both Master1 and Master2 execute

# 创建root用户,并设置密码,该用户可以在任意主机连接mysql服务
mysql> create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
# 为'root'@'%'用户分配主从复制权限
mysql> grant replication slave on *.* to 'root'@'%';

View the binary coordinates of the two main libraries

mysql> show master status;

Master1

mysql> show master status;
+---------------+----------+----------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB   | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+----------------+------------------+-------------------+
| binlog.000004 |      659 | db01,db02,db03 |                  |                   |
+---------------+----------+----------------+------------------+-------------------+
1 row in set (0.00 sec)

Master2

mysql> show master status;
+---------------+----------+----------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB   | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+----------------+------------------+-------------------+
| binlog.000002 |      659 | db01,db02,db03 |                  |                   |
+---------------+----------+----------------+------------------+-------------------+
1 row in set (0.00 sec)

4. Slave configuration (Slave1)

Modify the slave configuration file /etc/my.cnf

# mysql服务器id,保证整个集群环境中唯一
server-id=2

Restart the MySQL server

systemctl restart mysqld

5. Slave library configuration (Slave2)

Modify the slave configuration file /etc/my.cnf

# mysql服务器id,保证整个集群环境中唯一
server-id=4

Restart the MySQL server

systemctl restart mysqld

6. Configure the two slave libraries to associate with the main library

Execute in Slave1

mysql> change master to master_host="192.168.40.138",master_user="root",master_password='123456',master_log_file='binlog.000004',master_log_pos=659;
Query OK, 0 rows affected, 8 warnings (0.04 sec)

start repository

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)

View master-slave replication status

mysql> show slave status\G;

Insert image description here


Execute in Slave2

mysql> change master to master_host="192.168.40.170",master_user="root",master_password='123456',master_log_file='binlog.000002',master_log_pos=659;
Query OK, 0 rows affected, 8 warnings (0.04 sec)

start repository

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)

View master-slave replication status

mysql> show slave status\G;

Insert image description here

7. Copy each other between the two main libraries

Master1 replicates Master2 ,Execute in Master1

mysql>  change master to master_host="192.168.40.170",master_user="root",master_password='123456',master_log_file='binlog.000002',master_log_pos=659;
Query OK, 0 rows affected, 8 warnings (0.04 sec)

Start the slave library and check the master-slave replication status

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> show slave status\G;

Insert image description here


Master2 replicates Master1 ,Execute in Master2

mysql>  change master to master_host="192.168.40.138",master_user="root",master_password='123456',master_log_file='binlog.000004',master_log_pos=659;
Query OK, 0 rows affected, 8 warnings (0.01 sec)

Start the slave library and check the master-slave replication status

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> show slave status\G;

Insert image description here

8. Test

Create DB01 database:

CREATE DATABASE db01;

Select the DB01 database:

USE db01;

Create a mytabletable named with 3 fields:

CREATE TABLE mytable (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    age INT NOT NULL,
    address VARCHAR(100) NOT NULL,
    PRIMARY KEY (id)
);

mytableInsert 5 pieces of Chinese data into the table :

INSERT INTO mytable (name, age, address) VALUES
    ('张三', 20, '北京'),
    ('李四', 25, '上海'),
    ('王五', 30, '广州'),
    ('赵六', 35, '深圳'),
    ('钱七', 40, '成都');

After a simple test, it is possible to achieve dual master and dual slave functions.

Dual master and dual slave read and write separation configuration

       The read-write separation and load balancing of the MyCat console database are controlled by the balance attribute of the dataHost tag in the schema.xml file and automatic switching is completed on failure through writeType and switchType.

Modify MyCat’s schema.xml file

   <!--因为只设置了db01这个库,以它为例-->
        <dataNode name="dn4" dataHost="dhost4" database="db01" />
        <!--新建一个主机dhost4 负载均衡策略为1-->
        <dataHost name="dhost4" maxCon="1000" minCon="10" balance="1"
                          writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- 写操作对应的主机 Master1 - Slave1-->
                <writeHost host="Master1" url="jdbc:mysql://192.168.40.138:3306?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="123456">
                 <!-- 读操作对应的主机-->
                        <readHost host="Slave1" url="jdbc:mysql://192.168.40.139:3306?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="123456">
                        </readHost>
                </writeHost>
                <!-- Master2 - Slave2 -->
                <writeHost host="Master2" url="jdbc:mysql://192.168.40.170:3306?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="123456">
                 <!-- 读操作对应的主机-->
                        <readHost host="Slave2" url="jdbc:mysql://192.168.40.171:3306?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8" user="root" password="123456">
                        </readHost>
                </writeHost>
        </dataHost>

Insert image description here

Restart MyCat

[root@mycat mycat]# bin/mycat restart

Login to view

[root@mycat mycat]# mysql -h 192.168.40.137 -P 8066 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6.7.3-release-20210913163959 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;
+----------+
| DATABASE |
+----------+
| DB01     |
| DB_RW    |
+----------+
2 rows in set (0.00 sec)

mysql> use DB_RW;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_db01 |
+----------------+
| mytable        |
+----------------+
1 row in set (0.02 sec)

Test view

       The test can be done by inserting data to view or letting Master1 hang up, and then whether Master2 can be started to achieve high availability. The test is relatively simple, so I won’t post pictures.

Solutions to small network problems

Cancel NetworkManager to take over network settings:

nmcli n off

Set NetworkManager to take over network settings:

nmcli n on

Guess you like

Origin blog.csdn.net/qq_52589631/article/details/131138964