MySQL 主從複製

來源:互聯網
上載者:User

標籤:ica   語句   password   hang   status   ssl   server   date   backup   

瞭解MySQL主從複製:
MySQL的複製功能是構建大型,高效能應用程式的基礎。主從複製是指一台伺服器充當主要資料庫伺服器(master),另一台或多台伺服器充當從資料庫伺服器(slave),主伺服器中的資料自動複製到從伺服器之中。MySQL主從複製的基礎是主伺服器對資料庫修改並記錄二進位日誌,從伺服器通過主伺服器的二進位日誌自動執行更新。

複製解決的問題:

  • 資料分布 (Data distribution)
  • 負載平衡 (Load balancing)
  • 高可用及容錯 (High availability and failover)
  • 備份(Backups)

MySQL複製的類型:

  • SBR(statement based replicate):基於語句的複製,在主伺服器上執行的SQL語句,在從伺服器上執行同樣的語句。
  • RBR(row based replicate):基於行的複製,將主伺服器上改變的內容複寫過去。
  • MIX(mixed):預設採用基於語句的複製,一旦發現基於語句的無法精確的複製時,就會採用基於行的複製。

MySQL主從複製原理:

  1. master將改變的內容記錄到二進位日誌中(binary log),這些記錄叫做二進位日誌事件(binary log events);
  2. slave通過 IO thread 接收master的binary log,並寫入到slave的 realay log(中繼日誌)中,SQL thread 從realay log 讀取事件,並更新slave資料。

 

MySQL主從配置:

master配置:
禁用GTID:gtid_mode=OFF
server_id:103306 (建議IP後兩位加資料庫連接埠號碼)
log_bin:log_bin=ON
binlog_format:binlog_format=ROW (可選:mixed|statement )
建立複製帳號:

grant replication slave on *.* to ‘repl‘@‘%‘ identified by ‘password‘;


查看master狀態:

show master status\G;

查看Binlog Dump:

show processlist\G;

  Command: Binlog Dump
  State: Master has sent all binlog to slave; waiting for more updates

 

slave配置:
禁用GTID:gtid_mode=OFF
server_id:113306 (建議IP後兩位加資料庫連接埠號碼)
log_bin:log_bin=ON
log_slave_updates:log_slave_updates=ON
串連主庫:

CHANGE MASTER TO MASTER_HOST=‘192.168.100.10‘, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘password‘, MASTER_PORT=3306, MASTER_LOG_FILE=‘mysql-bin.000002‘, MASTER_LOG_POS=3141;

開啟同步:

start slave;

其他命令:

stop slave:關閉同步start slave io_thread:開啟IO線程,stop 關閉start slave sql_thread:開始SQL線程,stop 關閉

查看slave狀態:

show slave status\G;

  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
IO thead 和 SQL thead線程:

show processlist\G;

  Command: Connect
  State: Waiting for master to send event
  Command: Connect
  State: Slave has read all relay log; waiting for more updates

MySQL 主從複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.