======Mysql Replication 相關概述====== Replication 主要是由Master Server 將寫入的事件(新增,刪除及修改) 記錄在Master Binary Log中,Slave 由(I/O threads) 再讀取Master Binary Log 相關SQL語句 ,送到Slave Relay Log(中繼日誌檔)暫時存放,再由Slave (SQL threads)讀取 Relay Log 來重新執行 SQL相關語句。 ===Mysql Replication可以應用於=== * Backup 數據備份 * failover/HA(High Availability) 故障移轉及高可用性 * Load balancing 負載平衡(利用 主-多個Slave 分均分散讀取的壓力) ===Replication 特性=== 基本上以下所列的都是 **非同步(asynchronous,異步)** replication - 標準Master->Slave - dual Master Master<->Master ( 當另一方是Master 時自己就是Slave ; 當自已是Master時 另一方就是Slave) ===版本Master-Slave=== * 最好的情況下,Master與Slave兩者使用相同版本。 * 若不是情況下,能遵守以下規則。 *Slave 版本要比Master新 (因為 當新版本Slave 在讀取 舊版本Master binlog時,除了新的特性或與語法外,還能相容於舊版本相關格式或過時的語法) ======Mysql Replication配置====== 假定以下LAB環境狀況: * Master Mysql:ldap2.Qoop.com 192.168.0.170 () * Slave Mysql:ldap3.Qoop.com 192.168.0.171 (只允許 Master 讀寫資料庫資料。不允許外部Client端寫入Slave Mysql,但有條件方式使Client端只讀取 Slave Mysql) * 在做Mysql Replication時,必須確定Master 和 Slave 資料庫內容資料要一致。 ===Master Mysql=== - 要開啟binlog - 指定server-id值 (通常是 1) vim /etc/my.cnf ~略~ server-id=1 log-bin=mysql-bin(二進制日誌) ~略 要給Slave一個帳號可以存取Master GRANT RELOAD, SUPER, REPLICATION SLAVE ON *.* TO 'backup171'@'192.168.0.171' IDENTIFIED BY 'qwedcxza' 備份 Master 伺服器的資料庫。 mysqldump --all-databases --lock-all-tables >master.sql 記錄 Master 伺服器 binary log 的位置序號master > SHOW MASTER STATUS; File: mysql-bin.000008 Position: 176146 ===Slave Mysql=== - Slave 沒有限定一定要開啟binlog(這邊範例不打開) - 在未開始replication時,資料庫內容要和Master 一致。將備份檔複製到 Slave 伺服器並且回存到 Slave 伺服器。mysql -uroot < master.sql - 設定 /etc/my.cnf (以下 基本參數是設定 Slave Replication,不一定要寫在my.cnf設定檔 方法一) server-id = 2 #不要與Master值一樣就好了 relay-log=mysql-relay-bin #中繼日誌 log-slave-updates=1 #指定更新紀錄到slave binlog read-only=1 #只允許來自從服務器線程或具有SUPER權限的用戶的更新。可以確保從服務器不接受來自客戶的更新 master-port=3306 #指定要與Master 連接的阜口 replicate-do-db=zabbix2 #指定要與Master 哪個資料庫複寫 master-user=backup171 #Slave連接Master的帳號 master-password=qwedcxza #Slave連接Master的密碼 master-connect-retry=60 #複製從I/O線程 狀態下,與Master無法連接或連接錯誤,指定多少秒後再重新連接Master 除了寫在/etc/my.cnf以外,也可以使用Mysql Command方式 方法二 change master to master_host='192.168.0.170', ->master_user='backup171', ->master_password='qwedcxza', ->master_log_file='mysql-bin.000008', ->master_log_pos=176146; ======註 Show slave status 顯示slave數據複寫一些重要訊息====== * **Slave_IO_Running**slave的I/O thread是否已經連線到Master主機。有3種值(Yes,No,Connecting) * **Slave_SQL_Running**Slave SQL thread 是否已經在執行(讀取relay log並執行讀取到相關SQL) * **Master_Log_File**Slave I/O thread正在讀取 Master當前 Binary Log檔案名稱 * **Read_Master_Log_Pos**當前Master中Binary Log已被Slave I/O thread 讀取到的位置 * **Relay_Log_File**SQL thread 正在讀取當前 Relay Log 檔案名稱 * **Relay_Log_Position**Slave SQL thread 讀取並執行目前 Relay Log 的位置 * **Relay_Master_Log_File**記載了包含最近一個被 Slave SQL thread執行的事件的 Binary Log 位置 ======參考資料====== 1.MySQL Replication(Master Slave負載平衡) http://homeserver.com.tw/%E7%86%B1%E9%96%80%E6%9B%B8%E7%B1%A4/mysql-replication/ 2.Instructions for setting up MySQL Replication(Netman) http://www.study-area.org/tips/mysql_replication.htm 3.Mysql雙機備援+heartbeat http://www.360doc.com/content/11/0413/22/2054285_109433883.shtml 4.MySQL 設定 Replication (Master - Slave) http://blog.longwin.com.tw/2008/03/mysql_replication_master_slave_set_2008/ 5.[MySQL] 實做 MySQL Master-Master Replication 同步 http://blog.wu-boy.com/2008/12/mysql-%E5%AF%A6%E5%81%9A-mysql-master-master-replication-%E5%90%8C%E6%AD%A5/ 6.MySQL Master-Master Replication Manager(1) - 簡介 http://blog.longwin.com.tw/2008/10/mysql-master-replication-manager-mmm-intro-2008/ 7.MySQL Hierarchical Replication & Global Group IDs https://code.google.com/p/google-mysql-tools/wiki/GlobalTransactionIds 8.Mysql HA http://www.360doc.com/content/07/0531/10/15084_530610.shtml 9.MySQL Replication同步複製的延時監控 http://blog.csdn.net/kronus/article/details/7346248 10.http://www.360doc.com/relevant/109419362_more.shtml 11.高性能Mysql主從架構的複製原理及配置 http://www.360doc.com/content/13/0409/18/891660_277200925.shtml 12.清理 ZABBIX 的歷史數據 http://www.bitbi.biz/%E6%B8%85%E7%90%86-zabbix-%E7%9A%84%E5%8E%86%E5%8F%B2%E6%95%B0%E6%8D%AE-5582/#more-5582 13.MySQL中的複製 http://dev.mysql.com/doc/refman/5.1/zh/replication.html 14.mysql Replication 原理分析 http://zhaolinjnu.blog.sohu.com/103459345.html 15.Galera Cluster for MySQL Multi-master Replication http://blog.wu-boy.com/2013/03/galera-cluster-for-mysql-multi-master-replication/ 16.mysql-mmm http://mysql-mmm.org/mmm1:installation 17.MySQL半同步Semi-sync原理介紹 http://www.orczhou.com/index.php/2011/07/why-and-how-mysql-5-5-semi-sync-replication/ 18. maatkit----mysql管理工具 http://diyoki.blog.51cto.com/2158750/1156627 19.EffectiveMySQL-replication-techniques http://effectivemysql.com/book/replication-techniques/