mysql master (master-slave synchronization) Copy from

mysql master-slave synchronization

1, mysql master-slave synchronization (replication) Concept

 1. Copy Mysql data to a host on other hosts (slaves), and the instructions again achieved.

 2. The server acts as a replication master, while one or more other servers act as from the server.

 3. The master index server writes binary log files will be updated, and maintain files to track log cycle.

 4. When a connection from the master server, it notifies the position of the last successful update read from the master server in the log.

 5. receive any updates since then generated from the server, the server then waits for the master block and notifies the new update.

 binlog : binary log files for recording data update mysql or potential updates (such as DELETE statements delete the actual data does not meet the criteria)

2, Mysql which support replication

 1.  based replication statement:  execute SQL statements in the main server, from the server to perform the same statement.

   Note: MySQL default statement-based replication, high efficiency. Once clone not found, automatically selected row-based replication.

 2.  row-based replication:  the content changes replicate the past, rather than executing commands from the server again from the beginning mysql5.0 support.

 3.  mixed types of replication:  defaults based replication statement, if it is found based on statements can not be precisely replicated, will use row-based replication.

3, Mysql master-slave replication principle

 1. master server will change to the binary data are recorded binlog log, as long as the data changes on the master , it is changed to write binary logs;

 2. salve server within a certain time interval of the master binary logs to detect whether changes , if changed, it is a start I / O Thread request master binary event

 3. At the same master node is each I / O thread starts a thread dump , send a binary event, and saved to the local relay node from the log

 4. The  read node SQL thread starts from the relay log binary log replay locally , so that their data remains consistent and the master node

 5. Finally, I / O Thread and SQL Thread will go to sleep, waiting to be awakened once.

 You need to understand:

  1) generates two threads from the library, an I / O thread, a thread SQL;
  2) I / O request binlog thread to the main library, and the resulting written binlog local relay-log (relay logs ) file;
  3) generates a main library thread log dump, from the library used to I / O threads pass the binlog;
  . 4) the sQL thread reads the relay log log file, and resolves to sql statement executed one by one;

4, Mysql flowchart replication

 1. master binlog operation log recorded in the statement

 2. salve on the master server will detect whether the binary log change occurs within a certain time interval, if the change occurs

 3. salave open two threads: IO thread and the SQL thread

  1) IO thread: is responsible for reading the contents of master binlog to the relay log relay log in;

  2) SQL Thread: responsible for reading from the relay log log out binlog content and updates to the slave database (to ensure consistent data)

 

 mysql synchronization delays

1, resulting in a common cause mysql synchronization delay

 1) Network: The host machine or played from the bandwidth, high latency network between master and slave, the master binlog not cause the total amount to the slave, causing delay.

 2) performance of the machine: from the rotten machine using the machine? For example, the use of SSD and the host or the use of slave SATA.

 3) From the high-load machine: There are a lot of businesses will do the statistics on the slave, the slave server into an high load, resulting in a lot of cases the slave delay

 4) large transactions: for example, in RBR mode, perform with a large number of delete operations, such mysqlbinlog binlog view in SQL by viewing the information and use processlist can quickly confirm

 5) Lock:  Lock conflicts can also cause the slave SQL thread execution is slow, such as some select .... for update of SQL from the machine, or use the MyISAM engine, and so on.

2, hardware (optimization)

 1. With a good server, such 4u 2u significantly better than performance, performance 2u 1u significantly better than.

 2. The disk array storing or ssd or san, lifting random write performance.

 3. guaranteed between master and slave are in the same switch below, and a Gigabit environment.

  Summary : powerful hardware, the delay would become smaller. In short, reduced latency solution is to spend money and time to spend.

3, mysql master from synchrotron

 1) sync_binlog at the slave side is set to 0

  When the transaction commits, Mysql just binlog_cache data is written Binlog file, but fsync like disk does not perform synchronization instruction notification file system cache is flushed to disk

  And let Filesystem to decide what to do time synchronization, this is the best performance.

 2)slave端 innodb_flush_log_at_trx_commit = 2

  MySQL will be the log buffer data is written to log file every time a transaction is committed, but flush (brush to disk) will not be operating at the same time.

  In this mode, MySQL will flush is performed once per second (the brush disk) operation.

 3) -logs-slave-updates received from the server from the primary server to update not charged to its binary log.

 4) Direct disable binlog slave terminal

Guess you like

Origin www.cnblogs.com/8lala/p/12469779.html