mysql初識(shí)之日志文件篇
日志文件
1. err日志
error log 記錄mysql在運(yùn)行的過程中所有較為嚴(yán)重的警告和錯(cuò)誤信息,以及mysql server每次啟動(dòng)和關(guān)閉的詳細(xì)信息。系統(tǒng)在默認(rèn)情況下關(guān)閉error log 功能。error log 默認(rèn)存放在data目錄下,默認(rèn)文件名為主機(jī)名.err。error log 通過兩種方式開啟:
1). 啟動(dòng)mysqld時(shí),附加上--log-error參數(shù)
2) . 在my.cnf中配置log-error系統(tǒng)環(huán)境變量
當(dāng)進(jìn)入mysql后執(zhí)行 flush logs命令后,mysql會(huì)將當(dāng)前的錯(cuò)誤日志文件附加-old文件保存,并且創(chuàng)建一個(gè)新的空錯(cuò)誤日志文件,僅僅在指定--log-error的情況下。
注:error log 不全是保存是警告和錯(cuò)誤信息,還保存每次啟動(dòng)和關(guān)閉的信息。
ps啟動(dòng)時(shí)信息:130420 16:19:29 mysqld_safe mysqld from pid file /data0/mysql_data_7706/FZTEST-24178.pid ended130420 16:19:30 mysqld_safe Starting mysqld daemon with databases from /data0/mysql_data_7706error: Found option without preceding group in config file: /data0/mysql_data_7706/my.cnf at line: 1Fatal error in defaults handling. Program aborted130420 16:19:30 [Note] Plugin 'FEDERATED' is disabled.130420 16:19:30 [Note] Plugin 'ndbcluster' is disabled.130420 16:19:30 InnoDB: Initializing buffer pool, size = 8.0M130420 16:19:30 InnoDB: Completed initialization of buffer pool(以下省略) 查看error 日志是否啟動(dòng):mysql> show variables like 'log_error';+---------------+---------------------+| Variable_name | Value |+---------------+---------------------+| log_error | /var/log/mysqld.log |+---------------+---------------------+1 row in set (0.00 sec)關(guān)閉mysql: mysqladmin -uroot -p shutdown
2. 二進(jìn)制日志(binlog)
binlog日志主要記錄mysql的更新內(nèi)容,記錄每一條更新語句的執(zhí)行的時(shí)間、消耗的資源,以及相關(guān)的事務(wù)信息。可以用于實(shí)時(shí)備份,與master/slave復(fù)制。
如何打開:
my.cnf中[mysqld] 節(jié)點(diǎn)上添加
查看binlog是否打開
mysql> show variables like 'log_%';+---------------------------------+-----------------+| Variable_name | Value |+---------------------------------+-----------------+| log_bin | ON |查看binlog內(nèi)容: /usr/local/mysql3306/bin/mysqlbinlog mysql-bin.00002log-bin-index文件作用:記錄目錄所有binlog文件[root@FZTEST-24178 mysql_data_7706]# cat mysql_7706-relay-bin.index./mysql_7706-relay-bin.000032binlog格式說明:[root@localhost ~]# mysqlbinlog /home/mysql/binlog/binlog.000003 | more/*!40019 SET @@session.max_insert_delayed_threads=0*/;/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;DELIMITER /*!*/;# at 4#120330 16:51:46 server id 1 end_log_pos 98 Start: binlog v 4, server v 5.0.45-log created 120330 16:51:46# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.# at 196#120330 17:54:15 server id 1 end_log_pos 294 Query thread_id=3 exec_time=2 error_code=0SET TIMESTAMP=1333101255/*!*/;insert into tt7 select * from tt7/*!*/;# at 294#120330 17:54:46 server id 1 end_log_pos 388 Query thread_id=3 exec_time=28 error_code=0SET TIMESTAMP=1333101286/*!*/;alter table tt7 engine=innodb/*!*/;
解析binlog格式
位置
位于文件中的位置,“at 196”說明“事件”的起點(diǎn),是以第196字節(jié)開始;“end_log_pos 294”說明以第294字節(jié)結(jié)束
時(shí)間戳
事件發(fā)生的時(shí)間戳:“120330 17:54:46”
事件執(zhí)行時(shí)間
事件執(zhí)行花費(fèi)的時(shí)間:"exec_time=28"
錯(cuò)誤碼
錯(cuò)誤碼為:“error_code=0”
服務(wù)器的標(biāo)識(shí)
服務(wù)器的標(biāo)識(shí)id:“server id 1”
其他參數(shù)說明:
1). binlog_do_db:表示記錄指定數(shù)據(jù)庫的二進(jìn)制日志
2).binlog_ignore_db:表示忽略指定的數(shù)據(jù)庫的二進(jìn)制日志
3). max_binlog_cache_size:表示使用binlog時(shí)最大的內(nèi)存值
4). binlog_cache_size
此參數(shù)表示binlog使用的內(nèi)存大小,可以通過狀態(tài)變量binlog_cache_use和binlog_cache_disk_use來幫助測(cè)試。
binlog_cache_use:使用二進(jìn)制日志緩存的事務(wù)數(shù)量
binlog_cache_disk_use:使用二進(jìn)制日志緩存但超過binlog_cache_size值并使用臨時(shí)文件來保存事務(wù)中的語句的事務(wù)數(shù)量
5).max_binlog_size
Binlog最大值,最大和默認(rèn)值是1GB,該設(shè)置并不能嚴(yán)格控制Binlog的大小,尤其是Binlog比較靠近最大值而又遇到一個(gè)比較大事務(wù)時(shí),為了保證事務(wù)的完整性,不可能做切換日志的動(dòng)作,只能將該事務(wù)的所有SQL都記錄進(jìn)當(dāng)前日志,直到事務(wù)結(jié)束
6).sync_binlog
這個(gè)參數(shù)直接影響mysql的性能和完整性
sync_binlog=0:
當(dāng)事務(wù)提交后,Mysql僅僅是將binlog_cache中的數(shù)據(jù)寫入Binlog文件,但不執(zhí)行fsync之類的磁盤 同步指令通知文件系統(tǒng)將緩存刷新到磁盤,而讓Filesystem自行決定什么時(shí)候來做同步,這個(gè)是性能最好的。
sync_binlog=n,在進(jìn)行n次事務(wù)提交以后,Mysql將執(zhí)行一次fsync之類的磁盤同步指令,同志文件系統(tǒng)將Binlog文件緩存刷新到磁盤。
Mysql中默認(rèn)的設(shè)置是sync_binlog=0,即不作任何強(qiáng)制性的磁盤刷新指令,這時(shí)性能是最好的,但風(fēng)險(xiǎn)也是最大的。一旦系統(tǒng)繃Crash,在文件系統(tǒng)緩存中的所有Binlog信息都會(huì)丟失。
查詢?nèi)罩?/p>
查詢?nèi)罩炯从涗浰械牟樵冋Z句的日志,一般建議不開啟,有些query語句比較大,開啟后對(duì)性能的也有較大的影響。一般用于跟蹤某特殊 的性能問題才會(huì)短暫打開功能,默認(rèn)的查詢?nèi)罩疚募麨橹鳈C(jī)名.log。
慢查詢?nèi)罩?/p>
慢查詢?nèi)罩居涗浀氖遣樵冃枰^長(zhǎng)時(shí)間的query,通過在[mysqld] 下添加log-slow-queries=/tmp/slow_log 打開些功能,默認(rèn)文件名是hostname-slow.log默認(rèn)的目錄是數(shù)據(jù)目錄。
分析慢查詢的工具有:msyqlslowdump、mysqlsla
innodb在線日志redo日志
innodb是一個(gè)事務(wù)安全的存儲(chǔ)引擎,其事務(wù)安全性主要是通過在線redo日志和記錄在表空間中的undo信息來保證redo日志中記錄了innodb所做的所有物理變更和事務(wù)信息,通過redo日志和undo信息,innodb保證了在任何情況下的事務(wù)安全性。innodb的redo日志同樣默認(rèn)存放 在數(shù)據(jù)目錄下,可通過innodb_log_group_home_dir來更改設(shè)置日志的存放公交車,通過innodb_log_files_in_group 設(shè)置日志的數(shù)量。
bitsCN.com聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com