MySQLl主從複製原理和配置(多實例間)

一. 爲什麼要對MySQL做主從同步複製

1.MySQL主從方案主要作用

  1.讀寫分離,使數據庫能支撐更大的併發

  在報表中尤其重要。由於部分報表sql語句非常的慢,導致鎖表,影響前臺服務。如果前臺使用master,報表使用slave,那麼報表sql將不會造成前臺鎖,保證了前臺速度

  2.發揚不同表引擎的優點

  目前Myisam表的查詢速度比innodb略快,而寫入併發innodb比myIsam要好。那麼,我們可以使用innodb作爲master,處理高併發寫入,使用master作爲slave,接受查詢。或在myisam slave中建立全文索引,解決innodb無全文索引的弱點。

  3.熱備

  slave和master的數據“準實時”同步。


2.複製技術能夠解決的問題

  MySQL複製技術有以下一些特點:
  1.數據分佈 (Data distribution )
  2.負載平衡(load balancing)
  3.備份(Backups) 
  4.高可用性和容錯行 High availability and failover

二、MySQL主從同步複製原理

MySQL之間數據複製的基礎是二進制日誌文件(binary log file)。MySQL的主從複製是一個異步的複製過程,數據將從一個Mysql數據庫(我們稱之爲Master)複製到另一個Mysql數據庫(我們稱之爲Slave),在Master與Slave之間實現整個主從複製的過程是由三個線程參與完成的。其中有兩個線程(SQL線程和IO線程)在Slave端,另一個線程(I/O線程)在Master端。 Master啓用二進制日誌後,它的數據庫中所有操作都會以“事件”的方式記錄在二進制日誌中,slave通過一個I/O線程與主服務器保持通信,並監控master的二進制日誌文件的變化,如果發現master二進制日誌文件發生變化(binlog文件只記錄對數據庫有更改的SQL語句,不記錄任何查詢語句 ),則會把變化複製到自己的中繼日誌中,然後slave的一個SQL線程會把相關的“事件”執行到自己的數據庫中,以此實現從數據庫和主數據庫的一致性,也就實現了主從複製。

1)在Slave 服務器上執行sart slave命令開啓主從複製開關,開始進行主從複製。

2)此時,Slave服務器的IO線程會通過在master上已經授權的複製用戶權限請求連接master服務器,並請求從執行binlog日誌文件的指定位置(日誌文件名和位置就是在配置主從複製服務時執行change
master命令指定的)之後開始發送binlog日誌內容

3)Master服務器接收到來自Slave服務器的IO線程的請求後,其上負責複製的IO線程會根據Slave服務器的IO線程請求的信息分批讀取指定binlog日誌文件指定位置之後的binlog日誌信息,然後返回給Slave端的IO線程。返回的信息中除了binlog日誌內容外,還有在Master服務器端記錄的IO線程。返回的信息中除了binlog中的下一個指定更新位置。

4)當Slave服務器的IO線程獲取到Master服務器上IO線程發送的日誌內容、日誌文件及位置點後,會將binlog日誌內容依次寫到Slave端自身的Relay Log(即中繼日誌)文件(mysql-relay-bin.xxx)的最末端,並將新的binlog文件名和位置記錄到master-info文件中,以便下一次讀取master端新binlog日誌時能告訴Master服務器從新binlog日誌的指定文件及位置開始讀取新的binlog日誌內容

5)Slave服務器端的SQL線程會實時檢測本地Relay Log 中IO線程新增的日誌內容,然後及時把Relay LOG 文件中的內容解析成sql語句,並在自身Slave服務器上按解析SQL語句的位置順序執行應用這樣sql語句,並在relay-log.info中記錄當前應用中繼日誌的文件名和位置點。
1. Master 將改變記錄到二進制日誌中。
2. Slave 將 Master 的二進制日誌拷貝到它的中繼日誌( Relay_log )
3. Slave 重做中繼日誌中的事件,將改變反映它自己的數據


三、多實例主從搭建

多實例搭建見上一篇點擊打開鏈接

[root@localhost data]# ps -ef |grep mysql
root     17456 29867  0 17:07 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3306/my.cnf
mysql    17678 17456  0 17:07 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid --socket=/data/mysql3306/data/mysql3306.sock --port=3306
root     17885 29867  0 17:12 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3307/my.cnf
mysql    18107 17885  0 17:12 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql3307/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql3307/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid --socket=/data/mysql3307/data/mysql3307.sock --port=3307
root     18296 29867  0 17:26 pts/0    00:00:00 grep mysql
主庫開啓Binlog功能
[root@localhost data]# grep log-bin /data/mysql3306/my.cnf
log-bin=/data/mysql3306/data/mysql-bin
設置server-id
[root@localhost data]# grep server_id /data/mysql3306/my.cnf
server_id=3306
[root@localhost data]# grep server_id /data/mysql3307/my.cnf
server_id=3307
主庫授權slave訪問的用戶
mysql> grant replication slave on *.* to 'repl'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
| repl | 127.0.0.1            |
| root | localhost             |
| root | localhost.localdomain |
+------+-----------------------+
5 rows in set (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
從庫開啓複製
mysql> change master to
    -> master_host='127.0.0.1',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.16 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 127.0.0.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 690
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           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: 690
              Relay_Log_Space: 460
              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: 3306
                  Master_UUID: 4ca6cf25-5756-11e8-9709-005056a56f27
             Master_Info_File: /data/mysql3307/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)
Slave_IO_Running:Yes,這是I/O線程狀態,I/O線程負載從從庫去主庫讀取binlog日誌,並寫入從庫的中繼日誌中,狀態爲Yes表示I/O線程工作正常。
Slave_SQL_Running:Yes 這個是SQL線程狀態,SQL線程負載讀取中繼日誌(relay-log)中的數據並轉換爲SQL語句應用到從庫數據庫中,狀態爲Yes表示I/O線程工作正常.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章