MySQL主從複製原理及配置詳細過程以及主從複製叢集自動化部署的實現

來源:互聯網
上載者:User

標籤:blank   target   style   標籤   

Technorati 標籤: 那你魔鬼

一、複製概述

  Mysql內建的複製功能是構建大型,高效能應用程式的基礎。將Mysql的資料分布到多個系統上去,這種分布的機制,是通過將Mysql的某一台主機的資料複製到其它主機(slaves)上,並重新執行一遍來實現的。複製過程中一個伺服器充當主伺服器,而一個或多個其它伺服器充當從伺服器。主伺服器將更新寫入二進位記錄檔,並維護檔案的一個索引以追蹤記錄檔迴圈。這些日誌可以記錄發送到從伺服器的更新。當一個從伺服器串連主伺服器時,它通知主伺服器從伺服器在日誌中讀取的最後一次成功更新的位置。從伺服器接收從那時起發生的任何更新,然後封鎖並等待主伺服器通知新的更新。

請注意當你進行複製時,所有對複製中的表的更新必須在主伺服器上進行。否則,你必須要小心,以避免使用者對主伺服器上的表進行的更新與對從伺服器上的表所進行的更新之間的衝突。

二、MySQL支援複製的類型也就是二進位日誌格式:

  1. 基於語句的複製:  在主伺服器上執行的SQL語句,在從伺服器上執行同樣的語句。MySQL預設採用基於語句的複製,效率比較高
  2. 基於行的複製:把改變的內容複寫過去,而不是把命令在從伺服器上執行一遍. 從mysql5.0開始支援
  3. 混合類型的複製: 預設採用基於語句的複製,一旦發現基於語句的無法精確的複製時,就會採用基於行的複製。

三、複製的特點:

  • 資料分布
  • 負載平衡
  • 備份
  • 高可用性和可用行

四、複製進程的實現:

  Mysql的複製(replication)是一個非同步或半同步的複製,從一個Mysql 執行個體(稱之為Master)複製到另一個Mysql 執行個體(稱之Slave)。實現整個複製操作主要由三個進程完成的,其中兩個進程在Slave(Sql進程和IO進程),另外一個進程在 Master(IO進程)上。
    要實施複製,首先必須開啟Master端的binary log(bin-log)功能,否則無法實現。因為整個複製過程實際上就是Slave從Master端擷取該日誌然後再在自己身上完全順序的執行日誌中所記錄的各種操作。

  具體過程如下:

  • Slave上面的IO進程串連上Master,並請求從指定記錄檔的指定位置(或者從最開始的日誌)之後的日誌內容;
  • Master接收到來自Slave的IO進程的請求後,通過負責複製的IO進程根據請求資訊讀取制定日誌指定位置之後的日誌資訊,返回給Slave 的IO進程。返回資訊中除了日誌所包含的資訊之外,還包括本次返回的資訊已經到Master端的bin-log檔案的名稱以及bin-log的位置;
  • Slave的IO進程接收到資訊後,將接收到的日誌內容依次添加到Slave端的relay-log檔案的最末端,並將讀取到的Master端的 bin-log的檔案名稱和位置記錄到master-info檔案中,以便在下一次讀取的時候能夠清楚的告訴Master“我需要從某個bin-log的哪個位置開始往後的日誌內容,請發給我”;
  • Slave的Sql進程檢測到relay-log中新增加了內容後,會馬上解析relay-log的內容成為在Master端真實執行時候的那些可執行檔內容,並在自身執行。

五、配置過程:

MySQL主從複製配置:

環境:
    master.test.com-->172.16.3.201
    slave.test.com-->172.16.3.203

        master.test.com-->172.16.3.201

1、啟用二進位日誌
        vim /etc/mysql/my.cnf
            # 確保[mysqld]中有如下語句
                log-bin=mysql-bin

            # 儲存退出
 
    2、設定一個在當前叢集中唯一的server-id
        vim /etc/mysql/mys.cnf
            # 定位至如下行
                server-id       = 1
            # 把其值設定為11,也可以設定其他
                server-id       = 11
            # 儲存退出
        上述過程用sed實現:
            sed -i ‘/^server-id/[email protected]@[email protected]‘ /etc/mysql/my.cnf
    3、建立一個有複製許可權的帳號包括[replication slave,replication client]:
        mysql -e "grant replication client,replication slave on *.* to ‘repluser‘@‘172.16.3.203‘ identified by ‘replpass‘; flush privileges;"
    4、service mysqld restart  -->重啟MySQL

 

 

    slave.test.com-->172.16.3.203

1、啟用中繼日誌
            vim /etc/mysql/my.cnf
                # 在裡面的mysqld段中加入如下內容:開啟中繼日誌
                    relay-log = relay-bin
                # 儲存退出
            用sed實現:
                sed -i ‘/\[mysqld\]/a relay-log = relay-bin‘ /etc/mysql/my.cnf
        2、設定一個在當前叢集中唯一的server-id
            vim /etc/mysql/my.cnf
                # 定位至如下行
                    server-id    =1
                # 把其值設定為其21,也可以設定為其他
                    server-id    =21
                # 儲存退出
            # 用sed實現
                sed -i ‘/^server-id/[email protected]@[email protected]‘ /etc/mysql/my.cnf
        3、設定MySQL唯讀
            用sed實現:
                sed -i ‘/\[mysqld\]/a read-only = on‘ /etc/mysql/my.cnf
         4、service mysqld restart
        5、使用有複製許可權帳號串連至主伺服器
            mysql -e "change master to master_host=‘172.16.3.201‘, master_user=‘repluser‘, master_password=‘replpass‘, master_log_file=‘binlog.000033‘, master_log_pos=553, master_connect_retry=5, master_heartbeat_period=2"
        6、啟動從伺服器
            mysql -e "start slave"

 

 可能遇到的報錯:

ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MariaDB error log

解決方案:

該報錯如果出現,就會出現在slave中,處理方法如下:
        MariaDB [(none)]> reset slave;
        MariaDB [(none)]> change master to master_host=‘172.16.3.201‘, master_user=‘repluser‘, master_password=‘replpass‘, master_log_file=‘binlog.000033‘, master_log_pos=245, master_connect_retry=5, master_heartbeat_period=2;
        Query OK, 0 rows affected (0.01 sec)
        MariaDB [(none)]> start slave;
        Query OK, 0 rows affected (0.01 sec)
        MariaDB [(none)]> show slave status\G
        *************************** 1. row ***************************
                       Slave_IO_State: Waiting for master to send event
                          Master_Host: 172.16.3.201
                          Master_User: repluser
                          Master_Port: 3306
                        Connect_Retry: 5
                      Master_Log_File: binlog.000033
                  Read_Master_Log_Pos: 245
                       Relay_Log_File: relay-bin.000002
                        Relay_Log_Pos: 526
                Relay_Master_Log_File: binlog.000033
                     Slave_IO_Running: Yes
                    Slave_SQL_Running: Yes
                      Replicate_Do_DB:
                  Replicate_Ignore_DB:
                   Replicate_Do_Table:
               Replicate_Ignore_Table:
              Replicate_Wild_Do_Table:

 

自動化部署MySQL主從複製叢集【指令碼實現】:

說明:通過shell指令碼實現自動化部署MySQL主從複製叢集,這裡我就不解釋每一步的意義了,在前面的介紹,有每一步驟的詳細解釋,我這個指令碼僅供大家參考,經過了我的親測,可以自動化部署,如有哪裡不足還請大家多多給些意見,我也是新手,大家一起學習,嘿嘿

master_slave.sh

#!/bin/bash if [[ $# -eq 0 ]]; then        echo "Usage: /bin/bash `basename $0` master_ip slave_ip"        exit 1fimaster_ip=$1slave_ip=$2sed -i ‘/^server-id/[email protected]@[email protected]‘ /etc/mysql/my.cnf mysql -e "grant replication client,replication slave on *.* to ‘repluser‘@"${slave_ip}" identified by ‘replpass‘; flush privileges;"service mysqld restartcat > test_master_slave.sh << EOF#!/bin/bash sed -i ‘/\[mysqld\]/a relay-log = relay-bin‘ /etc/mysql/my.cnf sed -i ‘/^server-id/[email protected]@[email protected]‘ /etc/mysql/my.cnf sed -i ‘/\[mysqld\]/a read-only = on‘ /etc/mysql/my.cnf service mysqld restart mysql -e "change master to master_host=‘172.16.3.101‘, master_user=‘repluser‘, master_password=‘replpass‘, master_log_file=‘binlog‘, master_log_pos=55, master_connect_retry=5, master_heartbeat_period=2" mysql -e "start slave"EOFmaster_log=`mysql -e "show master status" | grep ‘bin‘ | awk ‘{print $1}‘`master_log_position=`mysql -e "show master status" | grep ‘bin‘ | awk ‘{print $2}‘`sed -i "/binlog/[email protected]@${master_log}@" test_master_slave.shsed -i "/log_pos/[email protected]@${master_log_position}@" test_master_slave.shsed -i "/master_host/[email protected]@${master_ip}@" test_master_slave.shecho ${master_log}echo ${master_log_position} scp test_master_slave.sh node2:/rootssh node2 "/bin/bash /root/test_master_slave.sh"

 

該指令碼用到了shell的基本知識,還有sed、awk等的很基礎的知識;

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.