mysql主從複製

來源:互聯網
上載者:User

標籤:process   thread   日誌記錄   下一步   master   

主從複製有利於 資料庫結構描述的健壯性  提升訪問速度   易於維護管理

主從互為備份M  ----  S        一主多從  非同步同步

650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/80/43/wKiom1c8K-Cij7jyAABUrjWR9Ok242.gif" style="float:none;" title="1.gif" alt="wKiom1c8K-Cij7jyAABUrjWR9Ok242.gif" />

該過程的第一部分就是master記錄二進位日誌。在每個事務更新資料完成之前,master在二日誌記錄這些改變。MySQL將事務串列的寫入二進位日誌,即使事務中的語句都是交叉執行的。在事件寫入二進位日誌完成後,master通知儲存引擎提交事務。

下一步就是slave將master的binary log拷貝到它自己的中繼日誌。首先,slave開始一個背景工作執行緒——I/O線程。I/O線程在master上開啟一個普通的串連,然後開始binlog dump process。Binlog dump process從master的二進位日誌中讀取事件,如果已經跟上master,它會睡眠並等待master產生新的事件。I/O線程將這些事件寫入中繼日誌。

SQL slave thread(SQL從線程)處理該過程的最後一步。SQL線程從中繼日誌讀取事件,並重放其中的事件而更新slave的資料,使其與master中的資料一致。只要該線程與I/O線程保持一致,中繼日誌通常會位於OS的緩衝中,所以中繼日誌的開銷很小。

此外,在master中也有一個背景工作執行緒:和其它MySQL的串連一樣,slave在master中開啟一個串連也會使得master開始一個線程。複製過程有一個很重要的限制——複製在slave上是序列化的,也就是說master上的並行更新操作不能在slave上並行操作。

 mysql -uroot -p123456  -S /data/3306/mysql.sock

 mysql> show variables like ‘log_bin%‘;

+---------------------------------+-------+

| Variable_name                   | Value |

+---------------------------------+-------+

| log_bin                         | ON    |

server-id = 不一樣

mysql> grant replication  slave  on *.*  to ‘rep‘@‘192.168.10.%‘  identified  by ‘123456‘;

mysql> flush privileges;

mysql> flush  table with read lock;   //讀鎖

mysql> show master status/logs;

+------------------+-----------+

| Log_name         | File_size |

+------------------+-----------+

| mysql-bin.000005 |       338 |

+------------------+-----------+

mysqldump  -uroot -p123456  -S /data/3306/mysql.sock  -A -B  --events  --master-data=2   > /opt/rep.sql

mysql> unlock  tables;  //解鎖 

# mysql -uroot -p123456  -S /data/3307/mysql.sock  < /opt/rep.sql

 

 mysql -uroot -p123456  -S /data/3307/mysql.sock

CHANGE MASTER TO

MASTER_HOST=‘192.168.10.10‘,

MASTER_PORT=3306,

MASTER_USER=‘rep‘,

MASTER_PASSWORD=‘123456‘,

MASTER_LOG_FILE=‘mysql-bin.000005‘,

MASTER_LOG_POS=417;

/data/3307/data/master.info               //產生的檔案

mysql> start slave;

mysql> show slave status\G;

      Slave_IO_Running: Yes

      Slave_SQL_Running: Yes

relay-bin.index

/data/3307/relay-bin.000001

/data/3307/relay-bin.000002

relay-log.info                    /中繼日誌

427

mysql-bin.000005

591

650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/80/43/wKiom1c8K_aQ3eZYAAFJ_MtNFnA526.png" title="2.png" alt="wKiom1c8K_aQ3eZYAAFJ_MtNFnA526.png" />

--master-data=1


本文出自 “何全” 部落格,請務必保留此出處http://hequan.blog.51cto.com/5701886/1774806

mysql主從複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.