Mysql database-Centos and Raspbian master-slave replication (backup)

Link of station B: https://www.bilibili.com/read/cv7380764

Centos is the main database, Mysql version: 5.5.64-MariaDB

On Raspbian (Raspberry Pi) is the backup database (from the database), Mysql version: 5.5.64-MariaDB

Master-slave replication start condition: Mysql version is consistent,

The MariaDB database management system is a branch of MySQL and is mainly maintained by the open source community. The purpose of using GPL license MariaDB is to be fully compatible with MySQL, including API and command lines, so that it can easily become a substitute for MySQL.

Why do master-slave replication

1. Backup

2. Master-slave replication read-write separation


1. Modify the master-slave database my.cnf

Add the following content in the database /etc/my.cnf, the master database and the slave database must be modified

Master database

[root@ecs-a4df ~]# vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin    # 开启二进制日志
server-id=105        # 服务器唯一ID

From database

root@raspberrypi:/home/pi# vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin    # 开启二进制日志
server-id=205        # 服务器唯一ID

2.  Restart the master-slave database

[root@ecs-a4df ~]# mysql restart

3. Create an account on the main database and authorize the slave to view the status of the main database master

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* to 'myslave'@'%' identified by 'q123456';
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |  2052388 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

4. Configure the slave database

master_host: IP address

master_user: the username of the backup account (the account created before on the master database)

master_password: backup account password

master_log_file: the main database log file (there is in the status of the master database master viewed above)

master_log_pos: (there is in the status of the master database master viewed above)

MariaDB [(none)]> change master to
master_host='192.168.3.100',master_user='myslave',master_password='q123456',
         master_log_file='mysql-bin.000001',master_log_pos=2052388;
MariaDB [(none)]> start slave; ## 启动主从复制功能

5. View the status of the replication function from the server

mysql> show slave status;
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+----------------+--------------------+------------+-------------+-------------------------+-----------------------------+---------------+-----------+---------------------+-----------------------------------------------------------------------------+------------------+--------------------------------+----------------------------+
| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File          | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_SSL_Crl | Master_SSL_Crlpath | Using_Gtid | Gtid_IO_Pos | Replicate_Do_Domain_Ids | Replicate_Ignore_Domain_Ids | Parallel_Mode | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                                     | Slave_DDL_Groups | Slave_Non_Transactional_Groups | Slave_Transactional_Groups |
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+----------------+--------------------+------------+-------------+-------------------------+-----------------------------+---------------+-----------+---------------------+-----------------------------------------------------------------------------+------------------+--------------------------------+----------------------------+
| Waiting for master to send event | 192.168.3.100 | myslave     |        3306 |            60 | mysql-bin.000001 |             2052388 | mysqld-relay-bin.000028 |         18415 | mysql-bin.000001      | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |             2052388 |           18725 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |              105 |                |                    | No         |             |                         |                             | conservative  |         0 | NULL                | Slave has read all relay log; waiting for the slave I/O thread to update it |                0 |                              0 |                          0 |
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+----------------+--------------------+------------+-------------+-------------------------+-----------------------------+---------------+-----------+---------------------+-----------------------------------------------------------------------------+------------------+--------------------------------+----------------------------+
1 row in set

mysql> 

You can see that both of these are YES, indicating that the function is operating normally, and the Mysql master-slave configuration is complete.

 

 

Mysql master-slave replication process

Insert picture description here

  1. Update events (update, insert, delete) of the main database db are written to binlog
  2. The main library creates a binlog dump thread and sends the contents of the binlog to the slave library
  3. Start from the library and initiate a connection to connect to the main library
  4. After starting from the library, create an I/O thread, read the binlog content from the main library and write it to the relay log
  5. After starting from the library, create a SQL thread, read the content from the relay log, execute the read update event from the position of Exec_Master_Log_Pos, and write the update content to the slave db

 

What is the principle of Mysql master-slave replication?

binlog: binary log, the binary file of all update event logs stored in the main library.

The basis of master-slave replication is that the master database records all changes in the database to binlog. Binlog is a file that saves all changes to the structure or content of the database from the moment the database server is started.

MySQL master-slave replication is an asynchronous replication process. The master library sends update events to the slave library, reads the update record from the library, and executes the update record, so that the content of the slave library is consistent with the main library.

In the main library, as long as there is an update event, it will be written to the binlog in turn, and then pushed to the slave library as the data source for the slave library to copy.

Binlog output thread. Whenever a slave library connects to the main library, the main library will create a thread and send the binlog content to the slave library. For each sql event that is about to be sent to the slave, the binlog output thread will lock it. Once the event is read by the thread, the lock will be released, even when the event is completely sent to the slave library, the lock will also be released.

In the slave library, when copying starts, the slave library will create two threads for processing:

I/O thread from the library. When the START SLAVE statement is executed from the slave library, the slave library creates an I/O thread, which connects to the master library and requests the master library to send the update records in the binlog to the slave library. Read the updates sent by the binlog output thread of the main library from the library I/O thread and copy these updates to a local file, including the relay log file.

SQL thread from the library. Create a SQL thread from the library, this thread reads and executes the update events written from the library I/O thread to the relay log.

As you can know, for each master-slave replication connection, there are three threads. The master library with multiple slave libraries creates a binlog output thread for each slave library connected to the master library, and each slave library has its own I/O thread and SQL thread.

By creating two independent threads from the library, the reading and writing from the library are separated when copying. Therefore, even if the thread responsible for execution is slow, the thread responsible for reading the update statement will not become slow. For example, if the slave library has not been running for a period of time, when it is started here, although its SQL thread executes slowly, its I/O thread can quickly read all the binlog contents from the main library. In this way, even if the slave library stops running before the SQL thread finishes executing all the read statements, the I/O thread at least completely reads all the content and safely backs it up in the local relay log of the slave library. , Ready to execute the statement at the next startup from the library. ( https://www.hoohack.me/2017/07/11/learning-mysql-replication-detail )

Guess you like

Origin blog.csdn.net/qq_33259323/article/details/108266812