MySQL master-slave replication (one master and one slave)

MySQL master-slave replication (one master and one slave)

What is master-slave replication

MySQL master-slave replication means that data can be copied from the master node of a MySQL database server to one or more slave nodes. The master node records all write operations and records these write operations in the binary log. The slave node obtains and applies these binary logs by connecting to the master node, thereby achieving data replication.

MySQL uses asynchronous replication by default, which means that after the master node performs write operations, it does not wait for the slave nodes to apply these operations, but immediately returns them to the client. The slave node asynchronously receives and applies the binary log of the master node to maintain consistency with the master node data.

The slave node can replicate all databases in the master node, or you can choose to replicate specific databases or specific tables. By configuring the parameters of the master node and slave node, you can make corresponding settings.

The principle of master-slave replication

image-20230714173923619

The master server records data changes and writes these changes to the binary log

The slave server will regularly check the master's binary log. If a change is found, it will request the master to send a binary event. The master will open an I/O thread, send the binary event to the slave and save it to the local relay log of the slave node.

The slave node will start the SQL thread, read the binary log from the relay log and replay it locally to make the data of the slave node consistent with the master node.

Finally, the I/O thread and SQL thread will go to sleep and wait for the next time they are awakened

Advantages of master-slave replication

  • Improved availability: When the master node fails, it can quickly switch to the slave node to ensure continuous availability of the database.

  • Load balancing: Distribute write requests to the master node and read operations to the slave nodes to reduce the load on the master node and improve performance.

  • Data backup: slave nodes can be used as backups to the master node to prevent data loss

Preparation

Prepare two servers and install MySQL separately.

IP type
192.168.200.10 master node
192.168.200.20 slave node

Step 1: Modify the hostname

Modify the host name of 192.168.200.10 to master

Modify the host name of 192.168.200.20 slave

[root@master ~]# hostname -I
192.168.200.10 
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]# 

[root@slave ~]# hostname -I
192.168.200.20 
[root@localhost ~]# hostnamectl set-hostname slave
[root@localhost ~]# bash
[root@slave ~]# 

Step 2: Install using yum

[root@master ~]# yum install -y mariadb-server mariadb
[root@slave ~]# yum install -y mariadb-server mariadb

Step 4: Start the mysql service and set it to start automatically at boot

[root@master ~]# systemctl enable mariadb --now
[root@slave ~]# systemctl enable mariadb --now
# 查看状态
[root@slave ~]# systemctl status mariadb
  
[root@slave ~]# systemctl status mariadb

Step 5: Check to see if the port is exposed

[root@master ~]# netstat -tlnp |grep 3306
tcp        0      0 0.0.0.0:3306   0.0.0.0:*       LISTEN      15802/mysqld    

[root@slave ~]# netstat -tlnp |grep 3306
tcp        0      0 0.0.0.0:3306       0.0.0.0:*     LISTEN      15991/mysqld   

Step 6: Initialize the database and test login

[root@master ~]# mysql_secure_installation 
[root@slave ~]#  mysql_secure_installation 

[root@master ~]# mysql -uroot -p000000
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '000000';flush privileges;
# root 用户授予所有数据库的所有权限,并设置密码为 '000000'
MariaDB [(none)]> exit;

[root@slave ~]# mysql -uroot -p000000
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '000000';flush privileges;
MariaDB [(none)]> exit;

Step 7: Firewall releases port 3306

[root@master ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent && firewall-cmd --reload
success
success

[root@slave ~]#  firewall-cmd --zone=public --add-port=3306/tcp --permanent && firewall-cmd --reload
success
success

Connect in SQLyog

image-20230714182151902

image-20230714182158683

Configure the main database Master

Modify the configuration file of the MySQL database

[root@master ~]# vim /etc/my.cnf
[mysqld]
# 添加如下两行信息
server-id=10
log_bin

Restart mysql service

[root@master ~]# systemctl restart mariadb

Create an account for copying and authorize it

Authorize the slave node to perform master-slave replication. After executing this command, the slave node user csqwill be granted replication permissions and can use the password 000000to connect to the master node for replication.

MariaDB [(none)]> grant replication slave on *.* to 'csq'@'192.168.200.%' identified by '000000';

Login mysql

Display a list of binary log files on the master node

记住下面的Log_name 和File_size,配置从数据库要用

MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       398 |
+--------------------+-----------+
# Log_name 列显示二进制日志文件的名称
# File_size 列显示二进制日志文件的大小

Configure slave database Slave

Modify mysql database configuration file

[root@slave ~]# vim /etc/my.cnf
[mysqld]
# 添加如下信息
server-id=20

Restart mysql service

[root@slave ~]# systemctl restart mariadb

Login mysql

Configure information to connect to the main database

# 因为配置主数据库的信息参数有很多当你忘记的时候可以查看如下命令
mysql> help change master to;

image-20230714190102613

We can copy and modify it slightly

这里要设置刚刚创建的用户账号信息,以及主数据库的IP

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.200.10',
    ->   MASTER_USER='csq',
    ->   MASTER_PASSWORD='000000',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mariadb-bin.000001',
    ->   MASTER_LOG_POS=398;
# MASTER_LOG_FILE 和  MASTER_LOG_POS 
# 这两个参数就是指定主数据库的二进制日志文件名称和位置
# 从节点可以确定从哪个位置开始复制数据

Start the master-slave replication process on the slave node.

MariaDB [(none)]> start slave;
# 执行该命令后,从节点将开始连接主节点,并开始复制主节点上的二进制日志

Check the status of the slave database

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.200.10
                  Master_User: csq
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 398
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 398
              Relay_Log_Space: 827
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10

test

Master database creates a database CSQ

MariaDB [(none)]> create database CSQ;

View from database

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| CSQ                |
| mysql              |
| performance_schema |
+--------------------+

SQLyog view

image-20230714191624035

image-20230714191646058

Completed

Guess you like

Origin blog.csdn.net/qq_52089863/article/details/131730574