Mysql master-slave replication and how they work

First, what is the master-slave replication

Master-slave replication is used to create a master database and exactly the same database environment, called from the database, the master database is generally quasi-real-time business database. In the most commonly used mysql database, supports single, asynchronous assignment. In the evaluation process, the server acts as a primary server, while the other acts as a server from a server; when the primary server sends the update information written to a specific binary file. And maintains an index file is used to track log cycle. This log can be recorded and sent to the update server. When a connection from the server to the master server notifies the master server from the server reads the position of the last successful update from the server's log file. Then receive any updates that occur from time from the server which would then lock the main server and wait for notification of new updates.

Second, the role of the master copy from

First, ensure data security; hot to do data backup, as backup database, the primary database server fails, the switch to continue to work from the database to avoid data loss.
Second is to enhance I / O performance. With daily production volume of business growing, I / O access frequency increasing, stand-alone can not meet at this time to do more library storage, reduce disk I / O access frequency, improve the individual devices I / O performance.
The third is to separate read and write, so that the database can support more concurrent; particularly important in the report. As part of the report sql statement is very slow, leading to lock the table, affecting front desk. If the future use of master, report uses slave, then the report will not cause sql lock the front desk, to ensure the reception speed.

Third, the principle of master-slave replication

Lord involves copying files from the
main library: binlog
from the library:
relaylog relay log
master.info main library information file
information relaylog.info relaylog applications

It involves three main threads from the copy in
the main library:
Binlog_Dump the Thread:
From the Library:
SLAVE_IO_THREAD
SLAVE_SQL_THREAD

DETAILED works as shown: Mysql master-slave replication and how they work
1. From the database to perform change master to command (the primary connection information database replication origin +)
2. The above information from the database will record to a file master.info
3. database command execution start slave immediately turn SLAVE_IO_THREAD and SLAVE_SQL_THREAD two threads

  1. From the database SLAVE_SQL_THREAD, read the information in the file master.info acquired IP, PORT, User, Pass, binlog position information
  2. Requests a connection from the primary database database SLAVE_IO_THREAD, it specializes in providing a master database SLAVE_IO_THREAD, responsible for interaction and SLAVE_SQL_THREAD
  3. The SLAVE_IO_THREAD binlog position information (mysql-bin.00000 *, ***), requesting a new primary database binlog
  4. Master database by Binlog_DUMP_Thread the latest binlog, SALVE_IO_THREAD from the library through the network to the TP
  5. SLAVE_IO_THREAD binlog receiving a new log stored in the TCP / IP buffer, immediately return an ACK to the master database, and updates the master.info
    9.SLAVE_IO_THREAD the TCP / IP data cache, relaylog dumped to the disk.
  6. SLAVE_SQL_THREAD read information relay.info in acquiring location information to relaylog had been applied last
  7. SLAVE_SQL_THREAD will follow the position of the point of last playback the latest relaylog, again to update the information relay.info
  8. Application from the database will automatically purge relay through regular clean-up

Once the master-slave replication successfully constructed, of which the primary database has undergone new changes, it will give SLAVE_IO_THREAD, enhanced real-time master-slave replication by slave_dump_THREAD sending signals.

Guess you like

Origin blog.51cto.com/14256904/2421231