mysql主从复制

mysql主从复制:

需要注意的一些点:1、master必须要打开binglog,设置唯一的server id 2、每一个slave都必须要设置唯一的server id 3、为slaves建立一个可以读取master binlog的授权账户 4、如果master有数据,你必须复制已有数据到slave,并且使用chagne master to设置master 的postition、file、host等。

mysql 的binlog包含三种形式:statement-based,Row-Based,mixed

statement-based:优点 非常简单,节省disk空间,兼容性好;缺点 许多语句没办法正确表达如包含rand(),delete/update...limit 没有order by,version(),uuid(),UDF等,对于复杂的语句需要评估与执行,而对于row-based则只需要执行affetced rows。

row-based:优点 1所有的改变都可以复制;2更少的行锁需求,提高并发性 缺点 对于一些insert update语句可能需要大量的disk空间,对于mysiam可能没有办法执行并发插入(表锁)

主从复制的三个线程:

  • Binlog dump thread.  The master creates a thread to send the binary log contents to a slave when the slave connects. This thread can be identified in the output of SHOW PROCESSLIST on the master as the Binlog Dumpthread.

    The binary log dump thread acquires a lock on the master's binary log for reading each event that is to be sent to the slave. As soon as the event has been read, the lock is released, even before the event is sent to the slave.

  • Slave I/O thread.  When a START SLAVE statement is issued on a slave server, the slave creates an I/O thread, which connects to the master and asks it to send the updates recorded in its binary logs.

    The slave I/O thread reads the updates that the master's Binlog Dump thread sends (see previous item) and copies them to local files that comprise the slave's relay log.

    The state of this thread is shown as Slave_IO_running in the output of SHOW SLAVE STATUS or asSlave_running in the output of SHOW STATUS.

  • Slave SQL thread.  The slave creates an SQL thread to read the relay log that is written by the slave I/O thread and execute the events contained therein.     

在slave 有2个记录复制数据的文件master.info(记录的是master的一些信息如pos、host等)relay-log.info(记录的是relay-log的一些信息如pos等),在slave关闭时不会丢失。

posted @ 2015-09-09 15:29  scu_2008_hike  阅读(140)  评论(0编辑  收藏  举报