Master-slave replication principle

1. Configuration content:

  • The main library opens the binlog log to record operations on the database.
  • The slave library writes the binlog log content of the main library to its own relay-log log
  • Read the relay-log redo log file from the library, and then execute these in your own library

2. The three threads involved

Main library thread

Binlog output thread: The main library opens the binlog log to record database changes. When the slave library connects to the main library, the main library creates a thread to send the binlog to the slave library.

From library thread

Slave I/O thread (Slave_IO_Running): When START SLAVE is executed, the slave creates an I/O thread to connect to the main library, request the main library to send the binlog update record, read the update content sent by the binlog and add it to the local file Relay log file.
Slave SQL thread (Slave_SQL_Running): Create a SQL thread from the library, read and execute the update event written from the library I/O thread to the relay log.

3. Specific steps:

Step 1: The update events (update, insert, delete) of the main database db are written to the binlog.
Step 2: Initiate a connection from the database and connect to the main database.
Step 3: At this time, the main database creates a binlog dump thread thread and saves the contents of the binlog Send to the slave library
Step 4: After the slave library is started, create an I/O thread, read the binlog content from the main library and write it to the relay log.
Step 5: Create a SQL thread from the library and read from the relay log Get the content, execute the read update event from the Exec_Master_Log_Pos position, and write the update content to the slave's db

Guess you like

Origin blog.csdn.net/make_1998/article/details/109251394