十五大原理之零四:MYSQL主從同步原理

主從同步又可以稱爲主從複製!本人習慣稱爲主從複製

一、MySQL主從複製(同步)原理過程詳細描述:

1.Slave服務器上執行start salve,開啓主從複製開關。

2.此時,Slave服務器的IO線程會通過Master上授權的複製用戶權限請求連接Master

服務器,並請求從指定Binlog日誌文件的指定位置(日誌文件名和位置就是配置主從複製服務時執行change master命令時指定的)之後發生Binlog日誌內容;

3.Master服務器接收到來自Slave服務器的IO線程的請求後,Master服務器上負責複製的IO線程根據Slave服務器的IO線程請求的信息讀取指定Binlog日誌文件指定位置之後的Binlog日誌信息,然後返回給Slave端的IO線程。返回的信息中除了Binlog日誌內容外,還有本次返回日誌內容後在Master服務端的新的Binlog文件名稱以及在Binlog中的下一個指定更新位置;

4.當Slave服務器的IO線程獲取到來自Master服務器上IO線程發送日誌內容及日誌文件及位置點後,將Binlog日誌內容依次寫入到Slave端自身的Relay Log(即中繼日誌)文件(MySQL-relay-bin.xxxxxx)的最末端,並將新的Binlog文件名和位置記錄到master-info文件中,以便下一次讀取Master端新Binlog日誌時能夠告訴Master服務器需要從新的Binlog日誌的哪個位置開始請求新的Binlog日誌內容。

5.Slave服務器端的SQL線程會實時的檢測本地Relay log中新增加的日誌內容,然後及時的把log文件中的內容解析成在Master端曾經執行的SQL語句的內容,並在自身Slave服務器上按語句的順序執行應用這些SQL語句,應用完畢後清理應用過的日誌。

6.經過了上面的過程,就可以確保在Master端和Slave端執行了同樣的SQL語句,當複製狀態正常的情況下,Mater端和Slave端的數據是完全一樣的,Mysql的同步機制是有一些特殊的情況的。

主從複製原理圖如下:

wKiom1Zu5vni1hg-AAECd_gPYZM693.png主從複製原理重點小結:

1.主從複製是異步的、邏輯的SQL語句級的複製。PXC

2.同步時,主庫有一個IO線程,從庫有兩個線程,IO和SQL線程。

3.實現主從複製的必要條件,主庫要開啓binglog功能。

4.binlog文件只記錄對數據庫有更新的SQL語句(來自主數據庫內容的變更)。

二、主從同步實踐(雙機同步)

1.環境準備

VMware Workstaion 兩臺 

Master:10.0.0.3

slave:10.0.0.103

linux Centos 6.7

mysql 5.3.27

2.二進制安裝數據庫

Master和slave數據庫安裝一致:

[root@master ~]#tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz

[root@master ~]#mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32

[root@master ~]#ln -s /application/mysql-5.5.32/ /application/mysql

[root@master ~]#useradd mysql -s /sbin/nologin -M

[root@master ~]#chown -R mysql.mysql /application/mysql/

初始化數據庫(創建管理數據庫的管理數據)

[root@master ~]# cd /application/mysql

[root@master mysql]#/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql

[root@master ~]#\cp /application/mysql/support-files/my-small.cnf  /etc/my.cnf

[root@master ~]#\cp /application/mysql/support-files/mysql.server 

啓動數據庫

[root@master ~]#/etc/init.d/mysqld

[root@master ~]#sed -i 's#/usr/local/mysql#/application/mysql#g'  /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

[root@master ~]#/etc/init.d/mysqld start

[root@master ~]#echo 'PATH="/application/mysql/bin:$PATH"' >>/etc/profile

[root@master ~]#source /etc/profile

[root@master ~]#mysqladmin -uroot password#設置數據庫用戶密碼

slave安裝同上。

3.Master服務器

vim /etc/my.cnf

#skip-networking

server-id       = 3 #一般修改爲主機ip地址的後8位

# Uncomment the following if you want to log updates

log-bin=mysql-bin #主數據庫開啓log日誌記錄增量以及變化內容以便同步到從庫

重啓數據庫:

[root@master ~]#/etc/init.d/mysqld restart

登錄數據庫授權一個同步從庫的用戶的權限:

mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by 'oldboy123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

mysql> show grants for 'rep'@'10.0.0.%'

    -> ;

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

| Grants for [email protected].%                                                                                               |

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

| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'10.0.0.%' IDENTIFIED BY PASSWORD '*FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515' |

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

鎖表,記錄log位置信息以便從庫讀取

mysql> flush table with read lock;

Query OK, 0 rows affected (0.00 sec)


mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000005 |      333 |              |                  |

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

1 row in set (0.00 sec)

mysql> flush table with read lock;

Query OK, 0 rows affected (0.02 sec)

由於兩臺是新安裝的數據庫,不存在數據,可以不用鎖表、備份導入數據。

待從庫配置完畢,解鎖

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)



4.slave服務器

vim /etc/my.cnf

#skip-networking

server-id       = 103

# Uncomment the following if you want to log updates

#log-bin=mysql-bin #可以不開啓

重啓數據庫:

[root@slave ~]# /etc/init.d/mysqld restart

登錄數據庫:

mysql> CHANGE MASTER TO  

    -> MASTER_HOST='10.0.0.3', 

    -> MASTER_PORT=3306,

    -> MASTER_USER='rep', 

    -> MASTER_PASSWORD='oldboy123', 

    -> MASTER_LOG_FILE='mysql-bin.000005',

    -> MASTER_LOG_POS=333;

Query OK, 0 rows affected (0.06 sec)

開啓從庫:

mysql> start slave;

Query OK, 0 rows affected (0.09 sec)

查看從庫狀態:

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 10.0.0.3

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 1240

               Relay_Log_File: slave-relay-bin.000003

                Relay_Log_Pos: 587

        Relay_Master_Log_File: mysql-bin.000005

             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: 1240

              Relay_Log_Space: 1134

              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: 3

1 row in set (0.00 sec)


5.測試:

首先查看下主、從庫數據庫都是一致

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

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

3 rows in set (0.01 sec)

在主庫上創建一個數據庫wumin:

mysql> create database wumin;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;       

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| wumin              |

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

4 rows in set (0.00 sec)

從庫中查看到主庫新創建的wumin已複製到從庫

mysql> show databases;    

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| wumin              |

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

4 rows in set (0.00 sec)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章