MySQL主從複製

來源:互聯網
上載者:User

標籤:mysql主從複製

一、實驗環境

系統:centos6.5

軟體:mysql-5.5.32.tar.gz 

二、實驗步驟

  1. 主從複製基本概念及其原理

   Mysql的 Replication 是一個非同步複製過程,從一個 Mysql instace(我們稱之為 Master)複製到另一個 Mysql instance(我們稱之 Slave)。在 Master 與 Slave 之間的實現整個複製過程主要由三個線程來完成,其中兩個線程(Sql線程和IO線程)在 Slave 端,另外一個線程(IO線程)在 Master 端。

  要實現 MySQL 的 Replication ,首先必須開啟 Master 端的Binary Log(mysql-bin.xxxxxx)功能,否則無法實現。因為整個複製過程實際上就是Slave從Master端擷取該日誌然後再在自己身上完全 順序的執行日誌中所記錄的各種操作。開啟 MySQL 的 Binary Log 可以通過在啟動 MySQL Server 的過程中使用 “—log-bin” 參數選項,或者在 my.cnf 設定檔中的 mysqld 參數組([mysqld]標識後的參數部分)增加 “log-bin” 參數項。

  MySQL 複製的基本過程如下:

  1)Slave 上面的IO線程串連上 Master,並請求從指定記錄檔的指定位置(或者從最開始的日誌)之後的日誌內容;

   2) Master 接收到來自 Slave 的 IO 線程的請求後,通過負責複製的 IO 線程根據請求資訊讀取指定日誌指定位置之後的日誌資訊,返回給 Slave 端的 IO 線程。返回資訊中除了日誌所包含的資訊之外,還包括本次返回的資訊在 Master 端的 Binary Log 檔案的名稱以及在 Binary Log 中的位置;

  3)Slave 的 IO 線程接收到資訊後,將接收到的日誌內容依次寫入到 Slave 端的Relay Log檔案(mysql-relay-bin.xxxxxx)的最末端,並將讀取到的Master端的bin-log的檔案名稱和位置記錄到master- info檔案中,以便在下一次讀取的時候能夠清楚的高速Master“我需要從某個bin-log的哪個位置開始往後的日誌內容,請發給我”

   4)Slave 的 SQL 線程檢測到 Relay Log 中新增加了內容後,會馬上解析該 Log 檔案中的內容成為在 Master 端真實執行時候的那些可執行檔 Query 語句,並在自身執行這些 Query。這樣,實際上就是在 Master 端和 Slave 端執行了同樣的 Query,所以兩端的資料是完全一樣的。

上文參考:http://machael.blog.51cto.com/829462/239112/

650) this.width=650;" alt="\"\"" src="%5C" />

    2.主從複製實驗步驟

本實驗採用的是多執行個體實現主從複製。本實驗需要兩台MySQL資料庫伺服器Master和slave,Master為主伺服器,slave為從伺服器,初始狀態時,Master和slave中的資料資訊相同,當Master中的資料發生變化時,slave也跟著發生相應的變化,使得master和slave的資料資訊同步,達到備份的目的。

mysql主從同配置步驟:
1)準備兩台資料庫環境,或者單台多執行個體環境,能正常啟動和登入。
2)配置my.cnf檔案,主庫配置log-bin和server-id參數,從庫配置server-id,不能和主庫及其他庫一樣,一般不開啟從庫log-bin功能。注意:配置參數後要重啟生效。 

master:log-bin = /data/3306/mysql-bin

              server-id=1

slave:      server-id=3 

3)登入主庫增加用於從庫串連主庫同步的賬戶例如:rep,並授權replication slave 同步的許可權

grant replication slave on *.* to ‘rep‘@‘10.0.0.%‘ identified by ‘chen‘;
flush privileges;

4)登入主庫整個庫鎖表 flush table with read lock,然後查看show master status查看binlog的位置參數狀態

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      639 |              |                  |
+------------------+----------+--------------+------------------+

5)新視窗linux命令列備份或匯出原有的資料庫資料,並拷貝到從庫所在的伺服器目錄如果資料量很大,並且允許停機,可以停機打包,而不用mysqldump
6)解鎖主庫,unlock tables;
7)把主庫匯出的原有資料恢複到從庫
8)根據主庫的show master status查看binlog的位置狀態,在從庫執行change master to .....
9)從庫開啟同步開關,start slave  
start slave 
10)從庫show slave status\\G 檢查同步狀態,並在主庫進行更新測試。
mysql> show slave status\\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.2
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 639
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 425
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB: mysql
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 639
              Relay_Log_Space: 575
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

mysql>

完成上訴步驟後即可實現資料的主從同步,即在主庫建立庫表,在從庫可可是即時同步。 
如需瞭解MySQL多執行個體可點擊:http://purify.blog.51cto.com/10572011/1795031

本文出自 “叫醒你的不是鬧鐘而是夢想” 部落格,請務必保留此出處http://purify.blog.51cto.com/10572011/1795297

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.