mysql 主從複製原理html
Mysql 的 binlog 日誌重現的原理。binlog 會記錄下全部修改了數據庫的SQL 語句insert,update,delete,create,alter,drop table, grant 等等。mysql
主從複製的原理其實就是把主服務器上的 bin 日誌複製到從服務器上執行一遍,這樣從服務器上的數據就和主服務器上的數據相同了。sql
複製流程圖:數據庫
主從複製配置過程:服務器
主節點:ide
1. 啓用二進制日誌。測試
2. 爲當前節點設置一個全局惟一的server_id。ui
3. 建立有複製權限的用戶帳號 REPLIACTION SLAVE ,REPLIATION,CLIENT。編碼
從節點:spa
1. 啓動中繼日誌。
2. 爲當前節點設置一個全局惟一的server_id。
3. 使用有複製權限的用戶帳號鏈接至主節點,並啓動複製線程。
Mysql多實例搭建(Windows環境)
複製安裝成功的mysql實例
修改my.ini文件
[client]# 端口號port=3307[mysql]# 編碼格式default-character-set=gbk[mysqld]# 端口號port=3307# 配置根目錄basedir="D:/ProgrammingTool/DataManagementTool/mysql-5.6.42-winx64 - 2/"# 配置數據存儲根目錄datadir="D:/ProgrammingTool/DataManagementTool/mysql-5.6.42-winx64 - 2/data/"character-set-server=gbkdefault-storage-engine=MyISAMsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES# 配置最大鏈接數max_connections=512
依此類推配置第二個實例,第三個實例。
2. 安裝mysql服務
以管理員身份運行cmd窗口,在mysql根目錄bin文件下執行安裝服務
mysqld install {服務名稱} --defaults-file="{根目錄下ini文件}"eg: mysqld install MYSQL2 --defaults-file="D:/ProgrammingTool/DataManagementTool/mysql-5.6.42-winx64 -2/my.default.ini"
3. 啓動mysql服務
4. 測試鏈接
主從複製參數配置流程
1. 在主服務器上,必須開啓二進制日誌機制和配置一個獨立的ID
2. 在每個從服務器上,配置一個惟一的ID,建立一個用來專門複製主服務器數據的帳號
3. 在開始複製進程前,在主服務器上記錄二進制文件的位置信息
4. 若是在開始複製以前,數據庫中已經有數據,就必須先建立一個數據快照(能夠使用mysqldump導出數據庫,或者直接複製數據文件)
5. 配置從服務器要鏈接的主服務器的IP地址和登錄受權,二進制日誌文件名和位置
配置主庫ini參數
# For advice on how to change settings please see# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the# *** default location during install, and will be replaced if you# *** upgrade to a newer version of MySQL.[mysqld]# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.# basedir = .....# datadir = .....# port = .....# server_id = .....#=========主從複製關鍵配置===================== #主庫和從庫須要不一致,配一個惟一的ID編號,1至32。手動設定server_id=1log_slave_updates=1#二進制文件存放路徑,存放在根目錄datalog_bin=master-bin# 設置同步數據庫binlog-do-db=office_dev# 設置忽略數據庫binlog-ignore-db=boos# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2Msql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
配置從庫ini參數
[client]# 端口號port=3307[mysql]# 編碼格式default-character-set=gbk[mysqld]# 端口號port=3307# 配置根目錄basedir="D:/ProgrammingTool/DataManagementTool/mysql-5.6.42-winx64 - 2/"basedir="D:/ProgrammingTool/DataManagementTool/mysql-5.6.42-winx64 - 2/"# 配置數據存儲根目錄datadir="D:/ProgrammingTool/DataManagementTool/mysql-5.6.42-winx64 - 2/data/"character-set-server=gbkdefault-storage-engine=MyISAMsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES# 配置最大鏈接數max_connections=512server_id=2log-bin=mysql-binreplicate-do-db=office_dev主庫執行語句建立擁有複製權限用戶GRANT REPLICATION SLAVE ON *.* TO '{用戶名}'@'{主庫ip地址}' identified by '{密碼}';eg: GRANT REPLICATION SLAVE ON *.* TO 'slave'@'127.0.0.1' identified by'123456';FLUSH PRIVILEGES; # 刷新數據庫
查看主庫參數:
show master status;
設置從庫鏈接到主庫
1. STOP SLAVE; # 中止主從複製2. change master to master_host='127.0.0.1', master_user='slave',master_password='root96077',master_log_file='master-bin.000004',master_log_pos=2657;注:master_host:主服務器Ubuntu的ip地址master_log_file: 前面查詢到的主服務器日誌文件名master_log_pos: 前面查詢到的主服務器日誌文件位置3. START SLAVE; # 啓動主從複製4. 使用 SHOW SLAVE STATUS 查看鏈接參數