MySQL?binlog格式之Row和Statement語句詳解
MySQL binlog記錄
binlog記錄MySQL的所有修改操作,包括DML操作(create/update/delete)以及DDL操作,必須是已提交的事務(wù)。
binlog可以在從庫中進(jìn)行重放,以實(shí)現(xiàn)MySQL數(shù)據(jù)的高可用:
- master節(jié)點(diǎn)將數(shù)據(jù)修改操作寫入本機(jī)的binlog;
- slave節(jié)點(diǎn)上的I/O線程讀取master節(jié)點(diǎn)的binlog,并寫入到本地的relay-log;
- slave節(jié)點(diǎn)上的SQL線程讀取relay-log并進(jìn)行重放;
binlog格式有:statement、row、mixed;
> show variables like 'binlog_format'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | ROW | +---------------+-------+ 1 row in set (0.001 sec)
statement格式
statement記錄的是執(zhí)行的sql語句,也就是主庫上執(zhí)行了什么語句,binlog中就記錄什么語句。
statement格式的優(yōu)點(diǎn):
- 由于僅記錄sql語句,日志記錄量較少,可以節(jié)約磁盤和網(wǎng)絡(luò)I/O;
statement格式的缺點(diǎn):
- 對于特定的函數(shù),比如UUID(),user()這些非確定性函數(shù),在主備服務(wù)器上的執(zhí)行結(jié)果不同,可能造成主備數(shù)據(jù)不一致;
- 生產(chǎn)環(huán)境中一般不使用。
statement格式的問題
statement格式可能導(dǎo)致主備服務(wù)器數(shù)據(jù)的不一致,比如下表t:
mysql> CREATE TABLE `t` ( `id` int(11) NOT NULL, `a` int(11) DEFAULT NULL, `t_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `a` (`a`), KEY `t_modified`(`t_modified`) ) ENGINE=InnoDB; insert into t values(1,1,'2018-11-13'); insert into t values(2,2,'2018-11-12'); insert into t values(3,3,'2018-11-11'); insert into t values(4,4,'2018-11-10'); insert into t values(5,5,'2018-11-09');
當(dāng)執(zhí)行sql語句:
mysql> delete from t where a>=4 and t_modified<='2018-11-10' limit 1;
設(shè)置binlog_format=statement,如果主庫和從庫上在執(zhí)行上述sql語句時,選擇了不同的索引,則會刪除不同的數(shù)據(jù):
- 在主庫上:如果delete操作使用索引a,那么將刪除a=4這一行;
- 在從庫上:如果delete操作使用索引t_modified,那么將刪除t_modified='2018-11-09',也就是a=5這一行;
row格式
row記錄的是每一行記錄的增/刪/改操作,若一條sql語句修改了1000條記錄,row格式的日志將會分別記錄1000條記錄的修改,而statement僅記錄一條sql語句。
row格式的優(yōu)點(diǎn):
- 主從復(fù)制安全,可以保證主備服務(wù)器數(shù)據(jù)完全一致;
- 若誤操作修改了數(shù)據(jù),同時沒有備份恢復(fù),可以通過分析二進(jìn)制日志,通過對記錄做反向操作,達(dá)到恢復(fù)數(shù)據(jù)的目的;
mixed格式
mixed格式下,根據(jù)sql語句特點(diǎn),由系統(tǒng)決定某個修改使用row還是statement格式進(jìn)行存儲。
若sql語句可能引起主備不一致,那么使用row格式,否則使用statement格式。
以上就是MySQL binlog格式之Row和Statement語句詳解的詳細(xì)內(nèi)容,更多關(guān)于MySQL binlog格式的資料請關(guān)注腳本之家其它相關(guān)文章!
- MySQL 將文件導(dǎo)入數(shù)據(jù)庫(load data Statement)
- MySQL?如何將查詢結(jié)果導(dǎo)出到文件(select?…?into?Statement)
- MySQL?Prepared?Statement?預(yù)處理的操作方法
- The MySQL server is running with the --read-only option so it cannot execute this statement
- MySQL:Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEM
- 詳解JSP中的語句對象Statement操作MySQL的使用實(shí)例
- 解決mysql報錯You must reset your password using ALTER USER statement before executing this statement問題
相關(guān)文章
MySQL字段時間類型該如何選擇實(shí)現(xiàn)千萬數(shù)據(jù)下性能提升10%~30%
這篇文章主要介紹了MySQL字段的時間類型該如何選擇?才能實(shí)現(xiàn)千萬數(shù)據(jù)下性能提升10%~30%,主要概述datetime、timestamp與整形時間戳相關(guān)的內(nèi)容,并在千萬級別的數(shù)據(jù)量中測試它們的性能,最后總結(jié)出它們的特點(diǎn)與使用場景2023-10-10MYSQL數(shù)據(jù)庫Innodb?引擎mvcc鎖實(shí)現(xiàn)原理
這篇文章主要介紹了MYSQL數(shù)據(jù)庫Innodb?引擎mvcc鎖實(shí)現(xiàn)原理,但是mvcc?的實(shí)現(xiàn)原理是什么呢?下文我們就來實(shí)例說明來mvcc?的實(shí)現(xiàn)原理,感興趣的小伙伴可以參考一下2022-05-05