1.MySQL Server 有四種類型的日志——Error Log、General Query Log、Binary Log 和 Slow Query Log。
? ?1.1.第一個是錯誤日志,記錄 mysqld 的一些錯誤。
? 1. 2.第二個是一般查詢?nèi)罩荆涗?mysqld 正在做的事情,比如客戶端的連接和斷開、來自客戶端每條 Sql Statement 記錄信息;如果你想準確知道客戶端到底傳了什么給服務(wù)端,這個日志就非常管用了,不過它非常影響性能。
? 1.3.第三種就是 Binlog 了,包含了一些事件,這些事件描述了數(shù)據(jù)庫的改動,如建表、數(shù)據(jù)改動等,也包括一些潛在改動,比如DELETE FROM ? ? ? ran WHERE bing = luan,然而一條數(shù)據(jù)都沒被刪掉的這種情況.
? ?1.4.第四個是慢查詢?nèi)罩荆涗浺恍┎樵儽容^慢的 SQL 語句——這種日志非常常用,主要是給開發(fā)者調(diào)優(yōu)用的。
2.通常情況 MySQL 是默認關(guān)閉 Binlog 的 所以要自己啟用binlog 啟用的過程就是修改配置文件my.cnf了。
? ?配置log-bin和log-bin-index的值,如果沒有則自行加上去.
? ? log-bin=master-bin? ? log-bin-index=master-bin.index ?這里的log-bin是指以后生成各 Binlog 文件的前綴,比如上述使用master-bin,那么文件就將會是master-bin.000001、master-bin.000002等。而這里的log-bin-index則指 binlog index 文件的名稱,這里我們設(shè)置為master-bin.index。
log_bin ?設(shè)置此參數(shù)表示啟用binlog功能,并指定路徑名稱
log_bin_index ?設(shè)置此參數(shù)是指定二進制索引文件的路徑與名稱
binlog_do_db ?此參數(shù)表示只記錄指定數(shù)據(jù)庫的二進制日志
binlog_ignore_db 此參數(shù)表示不記錄指定的數(shù)據(jù)庫的二進制日志
max_binlog_cache_size 此參數(shù)表示binlog使用的內(nèi)存最大的尺寸
binlog_cache_size 此參數(shù)表示binlog使用的內(nèi)存大小,可以通過狀態(tài)變量binlog_cache_use和binlog_cache_disk_use來幫助測試。
binlog_cache_use:使用二進制日志緩存的事務(wù)數(shù)量?
binlog_cache_disk_use:使用二進制日志緩存但超過binlog_cache_size值并使用臨時文件來保存事務(wù)中的語句的事務(wù)數(shù)量
max_binlog_size ?Binlog最大值,最大和默認值是1GB,該設(shè)置并不能嚴格控制Binlog的大小,尤其是Binlog比較靠近最大值而又遇到一個比較大事務(wù)時,為了保證事務(wù)的完整性,不可能做切換日志的動作,只能將該事務(wù)的所有SQL都記錄進當前日志,直到事務(wù)結(jié)束
sync_binlog ?這個參數(shù)直接影響mysql的性能和完整性
sync_binlog=0:
當事務(wù)提交后,Mysql僅僅是將binlog_cache中的數(shù)據(jù)寫入Binlog文件,但不執(zhí)行fsync之類的磁盤??????? 同步指令通知文件系統(tǒng)將緩存刷新到磁盤,而讓Filesystem自行決定什么時候來做同步,這個是性能最好的。
sync_binlog=n,在進行n次事務(wù)提交以后,Mysql將執(zhí)行一次fsync之類的磁盤同步指令,同志文件系統(tǒng)將Binlog文件緩存刷新到磁盤。
Mysql中默認的設(shè)置是sync_binlog=0,即不作任何強制性的磁盤刷新指令,這時性能是最好的,但風險也是最大的。一旦系統(tǒng)繃Crash,在文件系統(tǒng)緩存中的所有Binlog信息都會丟失
binlog的刪除 ?binlog的刪除可以手工刪除或自動刪除
自動刪除binlog ?通過binlog參數(shù)(expire_logs_days )來實現(xiàn)mysql自動刪除binlog
手動刪除 mysql> reset master;? //刪除master的binlog
mysql> reset slave;??? //刪除slave的中繼日志
mysql> purge master logs before '2012-03-30 17:20:00';? //刪除指定日期以前的日志索引中binlog日志文件
mysql> purge master logs to 'binlog.000002';?? //刪除指定日志文件的日志索引中binlog日志文件
3. binlog 內(nèi)容
#170803 18:40:13 server id 2? end_log_pos 349718 CRC32 0x1f9bcdae Querythread_id=13931exec_time=0error_code=0SET TIMESTAMP=1501756813/*!*/;COMMIT/*!*/;# at 349718#170803 18:40:13 server id 2? end_log_pos 349807 CRC32 0xb104f75d Querythread_id=13931exec_time=0error_code=0SET TIMESTAMP=1501756813/*!*/;BEGIN/*!*/;# at 349807#170803 18:40:13 server id 2? end_log_pos 353501 CRC32 0xb1c9cef5 Querythread_id=13931exec_time=0error_code=0SET TIMESTAMP=1501756813/*!*/;UPDATE `cmf_theme_file`? SET `theme`='simpleboot3',`action`='portal/Page/index',`file`='portal/contact',`name`='聯(lián)系我們頁',`more`='{\"vars\":{\"baidu_map_info_window_text\":{\"title\":\"\\u767e\\u5ea6\\u5730\\u56fe\\u6807\\u6ce8\\u6587\\u5b57\",\"name\":\"baidu_map_info_window_text\",\"value\":\"ThinkCMF\\u5730\\u5740\\uff1a\\u4e0a\\u6d77\\u5e02\\u5f90\\u6c47\\u533a\\u659c\\u571f\\u8def2601\\u53f7<\\/span>\",\"type\":\"text\",\"tip\":\"\\u767e\\u5ea6\\u5730\\u56fe\\u6807\\u6ce8\\u6587\\u5b57,\\u652f\\u6301\\u7b80\\u5355html\\u4ee3\\u7801\",\"rule\":[]},\"company_location\":{\"title\":\"\\u516c\\u53f8\\u5750\\u6807\",\"value\":\"\",\"type\":\"location\",\"tip\":\"\",\"rule\":{\"require\":true}},\"address_cn\":{\"title\":\"\\u516c\\u53f8\\u5730\\u5740\",\"value\":\"\\u4e0a\\u6d77\\u5e02\\u5f90\\u6c47\\u533a\\u659c\\u571f\\u8def0001\\u53f7\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"address_en\":{\"title\":\"\\u516c\\u53f8\\u5730\\u5740\\uff08\\u82f1\\u6587\\uff09\",\"value\":\"NO.0001 Xie Tu Road, Shanghai China\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"email\":{\"title\":\"\\u516c\\u53f8\\u90ae\\u7bb1\",\"value\":\"catman@thinkcmf.com\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"phone_cn\":{\"title\":\"\\u516c\\u53f8\\u7535\\u8bdd\",\"value\":\"021 1000 0001\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"phone_en\":{\"title\":\"\\u516c\\u53f8\\u7535\\u8bdd\\uff08\\u82f1\\u6587\\uff09\",\"value\":\"+8621 1000 0001\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"qq\":{\"title\":\"\\u8054\\u7cfbQQ\",\"value\":\"478519726\",\"type\":\"text\",\"tip\":\"\\u591a\\u4e2a QQ\\u4ee5\\u82f1\\u6587\\u9017\\u53f7\\u9694\\u5f00\",\"rule\":{\"require\":true}}}}',`config_more`='{\"vars\":{\"baidu_map_info_window_text\":{\"title\":\"\\u767e\\u5ea6\\u5730\\u56fe\\u6807\\u6ce8\\u6587\\u5b57\",\"name\":\"baidu_map_info_window_text\",\"value\":\"ThinkCMF\\u5730\\u5740\\uff1a\\u4e0a\\u6d77\\u5e02\\u5f90\\u6c47\\u533a\\u659c\\u571f\\u8def2601\\u53f7<\\/span>\",\"type\":\"text\",\"tip\":\"\\u767e\\u5ea6\\u5730\\u56fe\\u6807\\u6ce8\\u6587\\u5b57,\\u652f\\u6301\\u7b80\\u5355html\\u4ee3\\u7801\",\"rule\":[]},\"company_location\":{\"title\":\"\\u516c\\u53f8\\u5750\\u6807\",\"value\":\"\",\"type\":\"location\",\"tip\":\"\",\"rule\":{\"require\":true}},\"address_cn\":{\"title\":\"\\u516c\\u53f8\\u5730\\u5740\",\"value\":\"\\u4e0a\\u6d77\\u5e02\\u5f90\\u6c47\\u533a\\u659c\\u571f\\u8def0001\\u53f7\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"address_en\":{\"title\":\"\\u516c\\u53f8\\u5730\\u5740\\uff08\\u82f1\\u6587\\uff09\",\"value\":\"NO.0001 Xie Tu Road, Shanghai China\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"email\":{\"title\":\"\\u516c\\u53f8\\u90ae\\u7bb1\",\"value\":\"catman@thinkcmf.com\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"phone_cn\":{\"title\":\"\\u516c\\u53f8\\u7535\\u8bdd\",\"value\":\"021 1000 0001\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"phone_en\":{\"title\":\"\\u516c\\u53f8\\u7535\\u8bdd\\uff08\\u82f1\\u6587\\uff09\",\"value\":\"+8621 1000 0001\",\"type\":\"text\",\"tip\":\"\",\"rule\":{\"require\":true}},\"qq\":{\"title\":\"\\u8054\\u7cfbQQ\",\"value\":\"478519726\",\"type\":\"text\",\"tip\":\"\\u591a\\u4e2a QQ\\u4ee5\\u82f1\\u6587\\u9017\\u53f7\\u9694\\u5f00\",\"rule\":{\"require\":true}}}}',`description`='聯(lián)系我們頁模板文件',`is_public`=0,`list_order`=10? WHERE? `theme` = 'simpleboot3'? AND `file` = 'portal/contact'
/*!*/;
# at 353501
#170803 18:40:13 server id 2? end_log_pos 353591 CRC32 0xc21eaf1c Query thread_id=13931 exec_time=0 error_code=0
SET TIMESTAMP=1501756813/*!*/;
COMMIT
/*!*/;
# at 353591
#170803 18:40:13 server id 2? end_log_pos 353680 CRC32 0xa04760af Query thread_id=13931 exec_time=0 error_code=0
SET TIMESTAMP=1501756813/*!*/;
BEGIN
位置
位于文件中的位置,“at 353591”說明“事件”的起點,是以第353591字節(jié)開始;“end_log_pos 353680”說明以第353680字節(jié)結(jié)束
時間戳
事件發(fā)生的時間戳:“170803 18:40:13”
事件執(zhí)行時間
事件執(zhí)行花費的時間:"exec_time=0"
錯誤碼
錯誤碼為:“error_code=0”
服務(wù)器的標識
服務(wù)器的標識id:“server id 2”
4.mysql binlog 事件
QUERY_EVENT?
二進制日志中最常見的事件 ?QUERY_EVENT以文本的形式來記錄事務(wù)的操作。
FORMAT_DESCRIPTION_EVENT 它是binlog文件中的第一個事件,而且,該事件只會在binlog中出現(xiàn)一次。MySQL根據(jù)FORMAT_DESCRIPTION_EVENT的定義來解析其它事件。它通常指定了MySQL Server的版本,binlog的版本,該binlog文件的創(chuàng)建時間
ROWS_EVENT?
對于ROW格式的binlog,所有的DML(數(shù)據(jù)操作語言)語句都是記錄在ROWS_EVENT中。ROWS_EVENT分為三種:WRITE_ROWS_EVENT,UPDATE_ROWS_EVENT,DELETE_ROWS_EVENT,分別對應(yīng)insert,update和delete操作。
對于insert操作,WRITE_ROWS_EVENT包含了要插入的數(shù)據(jù)
對于update操作,UPDATE_ROWS_EVENT不僅包含了修改后的數(shù)據(jù),還包含了修改前的值。
對于delete操作,僅僅需要指定刪除的主鍵(在沒有主鍵的情況下,會給定所有列)
對于QUERY_EVENT事件,是以文本形式記錄DML操作的。而對于ROWS_EVENT事件,并不是文本形式,所以在通過mysqlbinlog查看基于ROW格式的binlog時,需要指定-vv --base64-output=decode-rows。
XID_EVENT
在事務(wù)提交時,不管是STATEMENT還是ROW格式的binlog,都會在末尾添加一個XID_EVENT事件代表事務(wù)的結(jié)束。該事件記錄了該事務(wù)的ID,在MySQL進行崩潰恢復時,根據(jù)事務(wù)在binlog中的提交情況來決定是否提交存儲引擎中狀態(tài)為prepared的事務(wù)。
ROTATE_EVENT
當binlog文件的大小達到max_binlog_size的值或者執(zhí)行flush logs命令時,binlog會發(fā)生切換,這個時候會在當前的binlog日志添加一個ROTATE_EVENT事件,用于指定下一個日志的名稱和位置。
STOP_EVENT
當MySQL數(shù)據(jù)庫停止時,會在當前的binlog末尾添加一個STOP_EVENT事件表示數(shù)據(jù)庫停止。
備注:?
sql語句分為3類 (DML DDL ?DCL)?
DDL is Data Definition Language statements. Some examples:數(shù)據(jù)定義語言,用于定義和管理SQL數(shù)據(jù)庫中的所有對象的語言
1.CREATE - to create objects in the database 創(chuàng)建
2.ALTER - alters the structure of the database 修改
3.DROP - delete objects from the database 刪除
4.TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
DML is Data Manipulation Language statements. Some examples:數(shù)據(jù)操作語言,SQL中處理數(shù)據(jù)等操作統(tǒng)稱為數(shù)據(jù)操縱語言
1.SELECT - retrieve data from the a database 查詢
2.INSERT - insert data into a table 添加
3.UPDATE - updates existing data within a table 更新
4.DELETE - deletes all records from a table, the space for the records remain 刪除
DCL is Data Control Language statements. Some examples:數(shù)據(jù)控制語言,用來授予或回收訪問數(shù)據(jù)庫的某種特權(quán),并控制數(shù)據(jù)庫操縱事務(wù)發(fā)生的時間及效果,對數(shù)據(jù)庫實行監(jiān)視等
COMMIT - save work done 提交
SAVEPOINT - identify a point in a transaction to which you can later roll back 保存點
ROLLBACK - restore database to original since the last COMMIT 回滾
SET TRANSACTION - Change transaction options like what rollback segment to use 設(shè)置當前事務(wù)的特性,它對后面的事務(wù)沒有影響