單向,雙向,環形,級聯,一主多從 php
雙機複製的5種情形
一、異步主從(默認常規)
二、雙寫(前段程序對兩個數據庫同時寫,必須兩邊都落實,程序才返回成功)
三、利用外掛軟件實現實時主庫Binlog日誌抓取,從而能夠在當機的時候補全從庫
四、谷歌開發的半同步插件
五、DRBDjava
主從讀寫分離
一、經過程序實現(性能,效率最佳,推薦)
php,java等程序能夠經過設置多個鏈接文件輕鬆實現主從讀寫分離。
二、經過軟件實現讀寫分離
MySQL-proxy, Amoeba等代理軟件也能夠實現讀寫分離功能,可是最好仍是程序實現。
三、開發dbproxy
讀寫分離邏輯圖展現:mysql
主從同步原理:
其實是異步的,即老是主庫寫完日誌,日誌纔可能被從庫應用。
master slave
線程:IO 線程:IO/SQL
開啓log-bin 須要設置的啓動參數:CHANGE MASTER TO
怎刪改會去寫log-bin MASTER_HOST='192.168.1.111'
MASTER_PORT=3308
MASTER_USER='alrinrep'
MASTER_PASSWORD='password123'
MASTER_LOG_FILE='mysql-bin.000047'
MASTER_LOG_POS='5632'
開啓同步方式:start slavesql
從庫IO向主庫發起複製請求,主庫開始對從庫認證,經過後主庫IO根據從庫 日誌要求開始發送bin-log到從庫relay-log。一個週期後,從庫更新master.info ,並從新向主庫發起請求。 SQL線程從relay-log發現新數據,並寫到slave的數據文件和日誌系統。 相關語句:--master-data=1, CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000020', MASTER_LOG_POS=1191; 創建相關的帳號,即alrinrep 並授予應有的權限
MySQL主從複製原理過程
一、Slave服務器上執行start slave,開啓主從複製開關
二、Slave服務器的IO線程會經過在Master上受權的複製用戶權限請求鏈接Master服務器,並請求從指定的bin-log日誌文件指定位置
(change master to 命令相關參數)以後發送binlog日誌
三、Master服務器接受到來自Slave服務器的IO線程的請求後,Master服務器上負責複製IO線程根據Slave服務器的IO線程請求的信息
讀取指定binlog日誌文件指定位置以後的binlog日誌信息,而後返回給Slave端的IO線程。返回的信息中除了binlog日誌的內容外,
還有本次返回日誌內容後在Master服務器端的新的binlog文件名稱以及在binlog中的下一個指定更新位置
四、當Slave服務器的IO線程獲取到來自Master服務器上的IO線程發送的日誌以及日誌文件及位置點後,將binlog日誌內容一次寫入到
slave端的Relay log(中繼日誌)文件(MySQL-relay-bin.xxxxxx)的最後端,並將新的binlog文件名和位置記錄到master-info文件
中,以便下一次讀取Master端新Binlog日誌時可以告訴Master服務器須要重新binlog日誌的那個文件哪一個文件開始新的binlog內容。
五、Slave服務器端的SQL線程會實時的檢測本地relay log中新增的日誌內容,而後及時把LOG文件中的內容解析成MASTER端曾經執行過
的SQL語句,並在自身SLAVE服務器上安順序執行這些SQL。應用玩後清理應用過的日誌。
6,通過了上述過程,就能夠確保MASTER和SLAVE執行了一樣的SQL。在複製正常的狀況下,MASTER和SLAVE數據是徹底一致的。數據庫
0,環境:3308/3309同機,IP爲192.168.199.151
1,修改my.cnf,檢查server-id和log-bin,主從server-id必須不一樣。主庫必須開啓bin-log
2,添加複製帳號和對應的 replication slave 權限後端
mysql> select user,host,password from user; +-------+-----------+-------------------------------------------+ | user | host | password | +-------+-----------+-------------------------------------------+ | root | localhost | *A0F874BC7F54EE086FCE60A37CE7887D8B31086B | | alrin | % | *A0F874BC7F54EE086FCE60A37CE7887D8B31086B | +-------+-----------+-------------------------------------------+ 2 rows in set (0.00 sec)
mysql> grant replication slave on *.* to alrinrep@'192.168.199.%' identified by 'password123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0.00 sec) mysql>
3,主庫上作備份。先鎖表,再備份服務器
mysql> flush table with read lock; Query OK, 0 rows affected (0.03 sec mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000004 | 423 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
新開session窗口後執行備份:
[root@localhost backup]# mysqldump -uroot -ppassword123 -S /data/mysqldata/3308/mysql.sock -A -B --events --master-data=2| gzip > /data/mysqldata/backup/rep.sql.gz
session
備份後檢查:
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000004 | 423 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
解鎖表:
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) mysql>
4,從庫操做異步
mysql> change master to -> MASTER_HOST='192.168.199.151', -> MASTER_PORT=3308, -> MASTER_USER='alrinrep', -> MASTER_PASSWORD='password123', -> MASTER_LOG_FILE='mysql-bin.000004', -> MASTER_LOG_POS=423; Query OK, 0 rows affected, 2 warnings (0.05 sec)
5,開啓複製ide
mysql> start slave; Query OK, 0 rows affected (0.25 sec)
6,檢查slave狀態:
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.199.151 Master_User: alrinrep Master_Port: 3308 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 423 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000004 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: 423 Relay_Log_Space: 456 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 Master_UUID: 16b758c5-2455-11e9-8fc6-080027339667 Master_Info_File: /data/mysqldata/3309/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) mysql>
7,嘗試在主庫DDL,檢查複製狀態
主庫:
mysql> create database alrin; Query OK, 1 row affected (0.00 sec) 從庫: mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | alrin | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
命令彙總:
1,grant replication slave on . to alrinrep@'192.168.199.%' identified by 'password123';2,flush table with read lock;3,show master status;4,unlock tables;5,change master to MASTER_HOST='192.168.199.151',MASTER_PORT=3308,MASTER_USER='alrinrep', MASTER_PASSWORD='password123', MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=423;6,start slave;7,show slave status\G