mysql日志我們可以總結(jié)有以下幾種,錯(cuò)誤日志、查詢?nèi)罩尽⒙樵內(nèi)罩尽⒍M(jìn)制日志了,這些日志每種都有自己的用處了,在mysql中日志也起到非常重要的作用了.
1.錯(cuò)誤日志
錯(cuò)誤日志主要用于定位mysql啟動(dòng)和運(yùn)行中的一些問題,錯(cuò)誤日志是默認(rèn)開啟的,可以通過show global variables查看錯(cuò)誤日志文件的位置:
- mysql> show global variables like '%log_error%';
- +---------------+--------------------------+
- | Variable_name | Value |
- +---------------+--------------------------+
- | log_error | /var/log/mysql/error.log |
- +---------------+--------------------------+
- 1 row in set (0.00 sec)
也可以通過mysql配置文件my.cnf查看錯(cuò)誤日志文件的位置:
# Error log - should be very few entries.
log_error = /var/log/mysql/error.log
常見的錯(cuò)誤日志也包括存儲(chǔ)引擎相關(guān)的信息,常見的格式如下:
- 140716 20:24:11 [Note] Plugin 'FEDERATED' is disabled.
- 140716 20:24:11 InnoDB: The InnoDB memory heap is disabled
- 140716 20:24:11 InnoDB: Mutexes and rw_locks use GCC atomic builtins
- 140716 20:24:11 InnoDB: Compressed tables use zlib 1.2.3.4
- 140716 20:24:11 InnoDB: Initializing buffer pool, size = 128.0M
- 140716 20:24:11 InnoDB: Completed initialization of buffer pool
- 140716 20:24:11 InnoDB: highest supported file format is Barracuda.
- 140716 20:24:11 InnoDB: Waiting for the background threads to start
- 140716 20:24:12 InnoDB: 5.5.37 started; log sequence number 20673572
- 140716 20:24:13 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
- 140716 20:24:13 [Note] - '127.0.0.1' resolves to '127.0.0.1';
- 140716 20:24:13 [Note] Server socket created on IP: '127.0.0.1'.
- 140716 20:24:13 [Note] Event Scheduler: Loaded 0 events
- 140716 20:24:13 [Note] /usr/sbin/mysqld: ready for connections.
2.查詢?nèi)罩?/strong>
查詢?nèi)罩居糜谟涗浰玫脑鰟h查改信息,由于在并發(fā)量大時(shí)會(huì)產(chǎn)生大量信息,所以默認(rèn)是關(guān)閉的,可以通過show global variables查看查詢?nèi)罩镜奈恢靡约笆欠耖_啟.
- mysql> show global variables like '%general_log%';
- +------------------+----------------------------+
- | Variable_name | Value |
- +------------------+----------------------------+
- | general_log | OFF |
- | general_log_file | /var/lib/mysql/mysql.log |
- +------------------+----------------------------+
本例中查詢?nèi)罩臼顷P(guān)閉的,我們可以在my.cnf中開啟查詢?nèi)罩?開啟后會(huì)影響mysql服務(wù)的性能,所以一般只用在開發(fā)環(huán)境中.
- # Be aware that this log type is a performance killer.
- # As of 5.1 you can enable the log at runtime!
- general_log_file = /var/log/mysql/mysql.log
- general_log = 1
查詢?nèi)罩景薽ysql執(zhí)行的每一條sql信息以及會(huì)話信息,日志格式如下:
- 140716 20:33:46 9 Connect user@localhost on domain
- 9 Query select url,url_token from articles where url_md5 = 'c473c205d1ee72cecf2546d332abbbcd'
- 9 Query select title,url from hot_articles limit 60 ,10
- 9 Quit
3.慢查詢?nèi)罩?/strong>
慢查詢?nèi)罩緫?yīng)該是對(duì)調(diào)試程序最有用的日志了,可以通過慢查詢?nèi)罩菊业侥男﹕ql語句是性能瓶頸,一般情況下,正常的web應(yīng)用時(shí)不會(huì)出現(xiàn)大量的慢查詢?nèi)罩镜?因此強(qiáng)烈建議開啟,可以通過show global variables查看慢查詢?nèi)罩镜奈恢靡约笆欠耖_啟.
- mysql> SHOW GLOBAL VARIABLES LIKE '%slow%';
- +---------------------+-------------------------------+ --Vevb.com
- | Variable_name | Value |
- +---------------------+-------------------------------+
- | log_slow_queries | ON |
- | slow_launch_time | 2 |
- | slow_query_log | ON |
- | slow_query_log_file | /var/log/mysql/mysql-slow.log |
- +---------------------+-------------------------------+
慢查詢默認(rèn)記錄超過10秒的查詢語句,可以精確到毫秒:
- mysql> SHOW GLOBAL VARIABLES LIKE '%long_query_tim%';
- +-----------------+-----------+
- | Variable_name | Value |
- +-----------------+-----------+
- | long_query_time | 10.000000 |
- +-----------------+-----------+
可以通過在my.cnf中設(shè)置慢查詢相關(guān)選項(xiàng),比如超時(shí)時(shí)間、記錄無索引查詢等:
- # Here you can see queries with especially long duration
- log_slow_queries = /var/log/mysql/mysql-slow.log
- long_query_time = 10
- log-queries-not-using-indexes
常見的慢查詢?nèi)罩靖袷饺缦?其中包含用戶、查詢耗時(shí)、sql語句、結(jié)果集數(shù)量等信息:
- # Time: 140716 20:47:59
- # User@Host: user[user] @ localhost []
- # Query_time: 12.00012 Lock_time: 0.000136 Rows_sent: 1 Rows_examined: 1
- use test;
- SET timestamp=1405514879;
- select url,url_token from articles where url_md5 = '817563bd7ef4b2a476f1f55d0b558cd1';
4.二進(jìn)制日志
二進(jìn)制日志也叫作變更日志,主要用于記錄修改數(shù)據(jù)或有可能引起數(shù)據(jù)改變的mysql語句,可以在my.cnf中配置二進(jìn)制日志相關(guān)參數(shù),如文件路徑、過期時(shí)間、文件大小等,也可以從二進(jìn)制日志中導(dǎo)出sql,用于恢復(fù)數(shù)據(jù)庫.
- log_bin = /var/log/mysql/mysql-bin.log
- expire_logs_days = 10
- max_binlog_size = 100M
使用show binary log查看mysql產(chǎn)生的bin log信息,包括文件名、文件大小,單位為字節(jié)等.
- mysql> show binary logs;
- +------------------+-----------+
- | Log_name | File_size |
- +------------------+-----------+
- | mysql-bin.000001 | 6252 |
- | mysql-bin.000002 | 295 |
- | mysql-bin.000003 | 126 |
- | mysql-bin.000004 | 107 |
- +------------------+-----------+
- 4 rows in set (0.00 sec)
4.1 查看二進(jìn)制日志
可以通過show master status和show binlog events查詢定位binlog信息:
- #使用show master status查看下一條binlog的偏移起始位置:
- mysql> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000004 | 870 | | |
- +------------------+----------+--------------+------------------+
- #使用show binlog events查詢某條binglog記錄
- #語法:show binlog events [int 'log_file'] [from position] [limit [offset,] row_count]
- #一下是查看一個(gè)完整的事務(wù)執(zhí)行binlog記錄
- mysql> show binlog events in 'mysql-bin.000004' from 870 limit 4/G;
- *************************** 1. row ***************************
- Log_name: mysql-bin.000004
- Pos: 870
- Event_type: Query
- Server_id: 1
- End_log_pos: 941
- Info: BEGIN
- *************************** 2. row ***************************
- Log_name: mysql-bin.000004
- Pos: 941
- Event_type: Intvar
- Server_id: 1
- End_log_pos: 969
- Info: INSERT_ID=277
- *************************** 3. row ***************************
- Log_name: mysql-bin.000004
- Pos: 969
- Event_type: Query
- Server_id: 1
- End_log_pos: 1247
- Info: use `user`; insert into test (title,url,url_md5,url_token,view_time) values('test','www.baidu.com','ae98f26d3b883f80b3eadb8709467607','1438524738','1405517003')
- *************************** 4. row ***************************
- Log_name: mysql-bin.000004
- Pos: 1247
- Event_type: Xid
- Server_id: 1
- End_log_pos: 1274
- Info: COMMIT /* xid=137 */
- 4 rows in set (0.00 sec)
4.2 使用二進(jìn)制日志恢復(fù)數(shù)據(jù)
使用mysql的命令行工具,可以方便的將binlog中的sql語句導(dǎo)出,導(dǎo)出后的sql語句可以方便的用于恢復(fù)數(shù)據(jù)庫,具體格式可以參考mysqlbinlog --help的輸出,下面是兩個(gè)最常用的例子:
- #將mysql中的命令點(diǎn)870到命令點(diǎn)1274之間的sql語句導(dǎo)出到文件中
- mysqlbinlog --start-position=870 --stop-position=1274 mysql-bin.000004 > /tmp/mysql_restore.sql
- #將mysql中的binlog中某段時(shí)間內(nèi)的sql導(dǎo)出,可用于恢復(fù)一段時(shí)間的數(shù)據(jù)
- mysqlbinlog --start-datetime="2012-07-16 00:00:00" --stop-datetime="2012-07-17 00:00:00" mysql-bin.000004 > /tmp/mysql_restore.sql
新聞熱點(diǎn)
疑難解答
圖片精選