MySQL主從介紹..
12月14日任務
17.1 MySQL主從介紹
17.2 準備工做
17.3 配置主
17.4 配置從
17.5 測試主從同步
一.mysql主從介紹mysql
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- 簡單點說,就是兩臺機器實時同步。
- MySQL主從又叫作Replication、AB複製。簡單講就是A和B兩臺機器作主從後,在A上寫數據,另一臺B也會跟着寫數據,二者數據實時同步的
- MySQL主從是基於binlog的,主上須開啓binlog才能進行主從。
- 主從過程大體有3個步驟
- 1)主將更改操做記錄到binlog裏
- 2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏
- 3)從根據relaylog裏面的sql語句按順序執行
- 主上有一個log dump線程,用來和從的I/O線程傳遞binlog
- 從上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另一個SQL線程用來把relaylog裏面的sql語句落地執行
- 主要就是經過中繼日誌記錄同步
- 應用場景:
- 一:作數據備份,若是主掛了,則能夠立馬開啓歷來提供服務
- 二:作讀庫,在主上寫入數據,客戶端直接在從上讀取數據,減輕主的壓力
二.準備工做linux
- 準備兩臺機器,給兩個機器都安裝好mysql,而且啓動,
- 下載地址:r.aminglinux.com 上有
三.配置主sql
![](http://static.javashuo.com/static/loading.gif)
示例一:數據庫
- 修改my.cnf,增長server-id=130和log_bin=aminglinux1
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- 把mysql庫備份並恢復成aming庫,做爲測試數據
- mysqldump -uroot mysql > /tmp/mysql.sql 備份一下
![](http://static.javashuo.com/static/loading.gif)
- mysql -uroot -e 「create database aming」 建立一個新的用戶
![](http://static.javashuo.com/static/loading.gif)
- mysql -uroot -e 「create database aming」 mysql -uroot aming < /tmp/mysql.sql 作一下數據恢復
![](http://static.javashuo.com/static/loading.gif)
- 建立用做同步數據的用戶
- mysql -uroot -p aminglinux 登入到mysql裏
![](http://static.javashuo.com/static/loading.gif)
- 執行命令:grant replication slave on *.* to 'repl'@slave_ip identified by 'password'; 建立一個用來同步數據的用戶
![](http://static.javashuo.com/static/loading.gif)
- 執行命令:flush tables with read lock; 鎖定數據,不讓它寫
- show master status;查看錶,記住箭頭指的兩個信息
![](http://static.javashuo.com/static/loading.gif)
- quit 退出mysql
- ls 查看一下mysql目錄下有哪些目錄
![](http://static.javashuo.com/static/loading.gif)
- 將從上沒有的這些目錄作一下備份,一會在從上從tmp目錄下拷貝過去
![](http://static.javashuo.com/static/loading.gif)
四.配置從服務器
![](http://static.javashuo.com/static/loading.gif)
示例一:ide
- 編輯my.cnf,配置server-id=132,要求和主不同
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- 把主上aming庫同步到從上 ,將主上的/tmp/目錄下的sql文件拷貝到本機/tmp/目錄下
![](http://static.javashuo.com/static/loading.gif)
- 能夠先建立aming庫,而後把主上的/tmp/mysql.sql拷貝到從上,而後導入aming庫
- 進去mysql裏
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx, 定義同步文件等信息,這一步是實現主從的主要操做
![](http://static.javashuo.com/static/loading.gif)
- start slave; 執行該命令
- 執行命令show slave sstatus\G 查看是否啓動了
![](http://static.javashuo.com/static/loading.gif)
- 還要到主上執行 unlock tables 解鎖數據,恢復寫權限
![](http://static.javashuo.com/static/loading.gif)
五.測試主從同步測試
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
示例一:ui
- 從上執行mysql -uroot
- show slave stauts\G
- 看是否有
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- 還需關注 Seconds_Behind_Master: 0 //爲主從延遲的時間
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- 執行命令show slave sstatus\G 查看是否啓動了
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
示例二:幾個配置參數介紹spa
- 主服務器上
- binlog-do-db= //僅同步指定的庫 ,定義要同步的庫
- binlog-ignore-db= //忽略指定庫 ,定義忽略同步的庫
- 從服務器上
- replicate_do_db= 定義要同步的庫
- replicate_ignore_db= 定義忽略同步的庫
- replicate_do_table= 定義要同步的表
- replicate_ignore_table= 定義忽略同步的表
- 建議使用下面這兩個參數來定義,不要用上面兩個參數來定義,上面的兩個參數容易致使數據不一致,有可能讓主從斷開。
- replicate_wild_do_table= //如aming.%, 支持通配符%
- replicate_wild_ignore_table=
示例三:測試主從線程
![](http://static.javashuo.com/static/loading.gif)
- select count(*) from db; 查看數據庫
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- 到從上 登錄 mysql -uroot aming
- select count(*) from db; 查看數據庫,能夠看得出來已經被清空了
![](http://static.javashuo.com/static/loading.gif)
- 主上繼續drop table db; 刪除數據庫
- 從上查看db表,會發現也一樣被刪除了
歡迎關注本站公眾號,獲取更多信息