1. 錯誤日志
錯誤日志主要用於定位mysql啟動和運行中的一些問題,錯誤日志是默認開啟的,可以通過show global variables查看錯誤日志文件的位置:
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查看錯誤日志文件的位置:
# Error log - should be very few entries.
log_error = /var/log/mysql/error.log
常見的錯誤日志也包括存儲引擎相關的信息,常見的格式如下:
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. 查詢日志
查詢日志用於記錄所用的增刪查改信息,由於在並發量大時會產生大量信息,所以默認是關閉的。可以通過show global variables查看查詢日志的位置以及是否開啟。
mysql> show global variables like '%general_log%';
+------------------+----------------------------+
| Variable_name | Value |
+------------------+----------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/mysql.log |
+------------------+----------------------------+
本例中查詢日志是關閉的。我們可以在my.cnf中開啟查詢日志,開啟後會影響mysql服務的性能,所以一般只用在開發環境中。
# 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
查詢日志包括了mysql執行的每一條sql信息以及會話信息,日志格式如下:
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. 慢查詢日志
慢查詢日志應該是對調試程序最有用的日志了,可以通過慢查詢日志找到哪些sql語句是性能瓶頸。一般情況下,正常的web應用時不會出現大量的慢查詢日志的,因此強烈建議開啟。可以通過show global variables查看慢查詢日志的位置以及是否開啟。
mysql> SHOW GLOBAL VARIABLES LIKE '%slow%';
+---------------------+-------------------------------+
| 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 |
+---------------------+-------------------------------+
慢查詢默認記錄超過10秒的查詢語句,可以精確到毫秒:
mysql> SHOW GLOBAL VARIABLES LIKE '%long_query_tim%';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
可以通過在my.cnf中設置慢查詢相關選項,比如超時時間、記錄無索引查詢等:
# 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
常見的慢查詢日志格式如下,其中包含用戶、查詢耗時、sql語句、結果集數量等信息:
# 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. 二進制日志
二進制日志也叫作變更日志,主要用於記錄修改數據或有可能引起數據改變的mysql語句,可以在my.cnf中配置二進制日志相關參數,如文件路徑、過期時間、文件大小等。也可以從二進制日志中導出sql,用於恢復數據庫。
log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
使用show binary log查看mysql產生的bin log信息,包括文件名、文件大小(單位為字節)等。
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 查看二進制日志
可以通過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]
#一下是查看一個完整的事務執行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 使用二進制日志恢復數據
使用mysql的命令行工具,可以方便的將binlog中的sql語句導出,導出後的sql語句可以方便的用於恢復數據庫。具體格式可以參考mysqlbinlog --help的輸出,下面是兩個最常用的例子:
#將mysql中的命令點870到命令點1274之間的sql語句導出到文件中
mysqlbinlog --start-position=870 --stop-position=1274 mysql-bin.000004 > /tmp/mysql_restore.sql
#將mysql中的binlog中某段時間內的sql導出,可用於恢復一段時間的數據
mysqlbinlog --start-datetime="2012-07-16 00:00:00" --stop-datetime="2012-07-17 00:00:00" mysql-bin.000004 > /tmp/mysql_restore.sql