一、主服務器 配置 my.ini 或 my.conf 作如下修改:
# Binary Logging. ===========================開始===========================================
log-bin=mysql-bin-1
binlog-format=Mixed# Error Logging.
log-error="LAPTOP-LPN2JC8I.err"# Server Id.
server-id=1#當sync_binlog =N (N>0) ,MySQL 在每寫 N次 二進制日誌binary log時,會使用fdatasync()函數將它的寫二進制日誌binary log同步到磁盤中去。sync_binlog 的默認值是0,像操作系統刷其他文件的機制一樣,MySQL不會同步到磁盤中去而是依賴操作系統來刷新binary log。
sync_binlog=1
#同步數據庫
binlog-do-db=db1
binlog-do-db=db2#mysql複製模式,三種:SBR(基於sql語句複製),RBR(基於行的複製),MBR(混合模式複製)
#混合模式複製
binlog_format=MIXED
#binlog過期清理時間
expire_logs_days=7
#binlog每個日誌文件大小
max_binlog_size=20M
#===============================結束===========================================
二、從服務器: 配置 my.ini 或 my.conf 作如下修改
# Binary Logging. ===============================開始===========================================
log-bin=mysql-bin-1
binlog-format=Mixed# Error Logging.
log-error="LAPTOP-LPN2JC8I.err"# Server Id.
server-id=2#當sync_binlog =N (N>0) ,MySQL 在每寫 N次 二進制日誌binary log時,會使用fdatasync()函數將它的寫二進制日誌binary log同步到磁盤中去。sync_binlog 的默認值是0,像操作系統刷其他文件的機制一樣,MySQL不會同步到磁盤中去而是依賴操作系統來刷新binary log。
sync_binlog=1
#同步數據庫名
binlog-do-db=db1
binlog-do-db=db2#mysql複製模式,三種:SBR(基於sql語句複製),RBR(基於行的複製),MBR(混合模式複製)
#混合模式複製
binlog_format=MIXED
#binlog過期清理時間
expire_logs_days=7
#binlog每個日誌文件大小
max_binlog_size=20M
#======================結束===========================================
三、master 主服務器 操作:打開命令行:
grant replication slave on *.* to 'username'@'%' identified by 'password'; // % 可以替換成對應從服務器的ip (授權給對應ip 的從服務器的登錄用戶)
show master status; // 查看 主服務器的狀態
四、slave 從服務器的 操作: 打開命令行界面:
change master to
master_host='master的ip',
master_port=3306,
master_user='username',
master_password='password',
master_log_file='mysql-bin-1.000001', // master 上的binlog 名稱
master_log_pos=154;// 對應master 上的pos
start slave;
Query OK, 0 rows affected (0.04 sec)
show slave status;
五、修改主服務器的db1,或 db2 中的數據就會同步 到 從服務器的數據庫