MySQL 5.6 Replication

來源:互聯網
上載者:User

   開啟mysql首頁,滿篇介紹mysql5.6版本有多好,多牛。後來瀏覽了5.6的更新說明,說是強化了replication,還有人測試開啟replication對效能影響不大,不像以前,影響效能明顯。反而效能更好?那個叫mysql中國的網站測試說的。官網有說多線程啥的進行複製,好吧。我信了。

   但是安裝網上老的配置方法配置主從模式失敗,服務起不來,說找不到pid什麼檔案,錯誤已經忘啦~~不好意思。    於是乎,在官方下載最新的安裝文檔...全英文...一口一口的啃。    終於在1個小時前配置好了,是雙主互備模式。master==master.
   整理下配置方法。
   安裝mysql5.6.9(源碼下載那個網站沒有提供最新的5.6.10版本,而我又不想裝RPM包,你懂的)。安裝在這裡略過,只要看解壓后里面的INSTALL檔案安裝提示來就可以了。
   我把mysql安裝到了/usr/local/mysql目錄,裝完之後,有個my.cnf在/usr/local/mysql目錄下面。

   這個就是設定檔了,開啟一看,裡面就有一行...


-----------------下面我們開始配置-------------


   兩台伺服器:mysql-m1    192.168.0.140               mysql-m2    192.168.0.141

   開啟mysql-m1的my.cnf檔案,添加如下代碼:
binlog-format=ROW    log-slave-updates=true    gtid-mode=on        # GTID only    enforce-gtid-consistency=true   # GTID only    master-info-repository=TABLE    relay-log-info-repository=TABLE    sync-master-info=1    slave-parallel-workers=2    binlog-checksum=CRC32    master-verify-checksum=1    slave-sql-verify-checksum=1    binlog-rows-query-log_events=1server-id=1    report-port=3306    port=3306    log-bin=binlog    report-host=192.168.0.140
肯定有人好奇,為啥要加這些代碼?好吧,我也不知道,官方就這麼說的。開玩笑了)。我把個個參數的意思原汁原味的寫出來:•  binlog-format: row-based replication is selected in order to test all of the MySQL 5.6 optimisations•  log-slave-updates, gtid-mode, enforce-gtid-consistency, report-port and report-host: used to enable Global Transaction IDs and meet the associated prerequisites•  master-info-repository and relay-log-info-repository: are turned on to enable the crash-safe binlog/slave functionality (storing the information in transactional tables rather than flat files)•  sync-master-info: set to 1 to ensure that no information is lost•  slave-parallel-workers: sets the number of parallel threads to be used for applying received replication events when this server acts as a slave. A value of 0 would turn off themultithreaded slave functionality; if the machine has a lot of cores and you are using many databases within the server then you may want to increase this value in order to better exploitmulti-threaded replication•  binlog-checksum,  master-verify-checksum  and slave-sql-verify-checksum: used to enable all of the replication checksum checks•  binlog-rows-query-log-events: enables informational log events (specifically, the original SQL query) in the binary log when using row-based replication –  this  makes troubleshooting simpler•  log-bin: The server cannot act as a replication master unless binary logging is enabled. If you wish to enable a slave to assume the role of master at some point in the future (i.e. in the event of a failover or switchover), you also need to configure binary logging. Binary logging must also be enabled on the slave(s) when using Global Transaction IDs.•  server-id: The server_id variable must be unique amongst all servers in the replication topology and is represented by a positive integer value from 1 to 232
好了,上面的參數都知道什麼意思了吧。
接下來,我們同樣設定第二台伺服器:
binlog-format=ROWlog-slave-updates=truegtid-mode=on        # GTID onlyenforce-gtid-consistency=true   # GTID onlymaster-info-repository=TABLErelay-log-info-repository=TABLEsync-master-info=1slave-parallel-workers=2binlog-checksum=CRC32master-verify-checksum=1slave-sql-verify-checksum=1binlog-rows-query-log_events=1server-id=2report-port=3306port=3306log-bin=binlogreport-host=192.168.0.141注意,server-id=2,另外,report-host也改下。
這兩個設定檔改好之後重啟伺服器。
重啟完伺服器之後,登入第二台伺服器mysql-m2登入mysql

mysql -u root -p


輸入完使用者名稱和密碼之後:


> CHANGE MASTER TO MASTER_HOST=192.168.0.140, MASTER_USER='repl_user', MASTER_PASSWORD='billy';
> START SLAVE;
這樣主從模式就做好了主-----》從
-----------------------------------我們在第一台伺服器上設定可遠程登入賬戶:先登入mysql伺服器:>Grant all privileges on *.* to 'admin'@'%' identified by '123456' with grant option;紅色字型分別為賬戶和密碼。同樣的,第二台伺服器也這麼操作。
然後,我們在主伺服器mysql-m1)的test資料庫下面建立一個表測試同步情況:登入mysql伺服器:mysql -u root -p>use test;(裝好後,mysql預設內建)。>create table abc(a int,b int,c int);
建立好後插入資料。>insert into abc values(1,2,3);多執行幾次然後select * from abc;查看資料插入進去了沒有。(我後面有自己插入了幾行)。
mysql> select * from acc;+------+------+------+| a    | b    | c    |+------+------+------+|    1 |    2 |    3 ||    1 |    2 |    3 ||    1 |    2 |    3 ||    1 |    2 |    3 ||    1 |    2 |    3 ||    1 |    2 |    3 ||    2 |    2 |    2 ||    2 |    2 |    2 ||    2 |    2 |    2 |+------+------+------+登入mysql-m2,查看是否有資料同步過來。同步過來了就是ok的了。
-------------------------------------官方的文檔只說了主從模式,我查了一下,要做雙主模式,必須開啟log-slave-updates=true這個選項。
我看了看兩台伺服器的設定檔都有這個。
然後呢,我自己試了一下。
登入主伺服器---mysql-m1
登入mysql  ----mysql -u root -p輸入密碼
執行:> CHANGE MASTER TO MASTER_HOST=192.168.0.141, MASTER_USER='admin', MASTER_PASSWORD='123456';
> START SLAVE;

沒想到,真的就可以,沒報錯。
>show slave status\G;
兩台伺服器都能查詢出來資訊。
===================總結=================官方這個文檔我是明白了。它讓每個slave都有當master的機會,如果一個master宕機了,執行:> CHANGE MASTER TO MASTER_HOST=192.168.0.*, MASTER_USER='repl_user', MASTER_PASSWORD='billy';
> START SLAVE;這個操作,只要換個IP地址,可以把任何一台從機變成主機,當主機啟動之後,再執行:> CHANGE MASTER TO MASTER_HOST=192.168.0.MASTER_IP, MASTER_USER='repl_user', MASTER_PASSWORD='billy';
> START SLAVE;這樣主從切換來回自如。
不過,我真的不知道類似於heartbeat的功能有木有~~~~我不像業務中斷,難道要在master上面做heartbeat?

本文出自 “勇攀高峰” 部落格,謝絕轉載!

相關文章

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.