2009年4月14日 星期二

Live Backups of MySQL Using Replication

Live Backups of MySQL Using Replication

當repliaction 運作起來時, 當一個sql 在master 執行時, MySQL將會把他們和log position紀錄在binary log (bin.log) 裡, 在slave 那邊會透過IO thread 讀取binary log 來找變動的地方, 如果找到變動的地方, 就會把它copy 到 relay.log裡, 然後它會紀錄新的位置(copy到哪)到master.info 裡, 這些都是在slave server 端
然後slave 會再次利用相同的IO thread 做check, 當slave server 偵測到自己relay log 的變動, 將會透過SQL thread 執行在relay log 裡的動作
作為一個安全守護者, slave 也會透過SQL thread 比對它自己和master 的資料, 如果資料不一致, replication 的動作變會停止而 錯誤訊息將會紀錄在error log 裡, 如果資料一致, 那copy 到哪之新的資料點將會紀錄在slave 的 relay-log.info 裡, 繼續偵測下次的變動



基於安全性的理由, 應該在另外建一個user 來做這件事 replication




這個指定了bin.log 要放在哪和檔名, 當然要確認目錄存在和 mysql 這個使用者有權限存取該目錄
當 server 重起或是log flush, 對於.log 結尾的file(也就是像上面那個), 會被自動取代成index number 的 file (ex: 000001) 當作新的file log name




像這邊log-bin-index 指定的這個file, log-bin.index 是用來紀錄現在用了哪些binary log, 當server restart 或者是log flushed 時, 這個同時 log file 改變的名字會記在這
log-error 用來指定error 要存的地方


對於master server binary log 變動做的relay log 是為了performance 考量
relay-log-info-file 便是指定要存slave 和 master 變動位置這種file 的name 和位置
relay log index 是用來紀錄現在有用來做replicating 的file name



如果想要只有root 能 access 的話, 我們可以改變max_connections 的值

雖然把值設成0 表示所有的連線都不允許, 只剩一個連線保留給root, 要看是不是還有其他的連線就用 SHOW PROCESSLIST, 要結束該連線就用KILL



要備份資料可以用mysqldump

extended-insert 參數將會create multiple-row INSERT 來讓備份快點
master-data 會lock 住整個table 不讓人家寫, 可是可以讀, 還有這個參數會加下面幾行在dump 出來file的結尾
當這個file 在 slave server 執行時, slave 將會執行該file 該位置後的SQL statement



START SLAVE;
After this statement is run, the slave will connect to the master and get the changes it missed since the backup. From there, it will stay current by continuously checking the binary log as outlined before.

STOP SLAVE;
slave server 知道停下來的位置, 所以我們可以在這個時候做slave server 的backup 動作, 完成之後在START SLAVE;

沒有留言: