mysql日志怎么记录 mysql 日志在哪( 三 )


1.配置信息
--log=[file-name]用来指定错误日志存放的位置 。
如果没有指定[file-name],默认为主机名(hostname)做为文件名,默认存放在DATADIR目录中 。
也可以将log配置到my.cnf文件中,这样就省去了每次在启动mysqld时都手工指定--log.例如:
# The MySQL server
[mysqld]
......
#query-log
log = /var/lib/mysql/query_log.log
......
2.读取查询日志
查询日志是纯文本格可,可以使用OS文本读取工具直接打开查看 。例如:
[mysql@test2]$ tail -n 15 query_log.log
080313 7:58:28 17 Query show tables
080313 8:07:45 17 Quit
080313 10:01:48 18 Connect root@localhost on
080313 10:02:38 18 Query SELECT DATABASE()
18 Init DB test
080313 10:02:42 18 Query show tables
080313 10:03:07 18 Query select * from pet
080313 10:06:26 18 Query insert into pet values('hunter','yxyup','cat','f','1996-04-29',null)
080313 10:06:39 18 Query select * from pet
080313 10:07:13 18 Query update pet set sex='m' where name='hunter'
080313 10:07:38 18 Query delete from pet where name='hunter'
080313 10:13:48 18 Query desc test8
080313 10:14:13 18 Query create table t1(id int,name char(10))
080313 10:14:41 18 Query alter table t1 add sex char(2)
[mysql@test2]$
四.慢查询日志
慢查询日志是记录了执行时间超过参数long_query_time(单位是秒)所设定值的SQL语句日志 。
Note:慢查询日志对于我们发现性能有问题的SQL有很帮助,建议使用并经常分析
1.配置信息
--log-slow-queries=[file-name]用来指定错误日志存放的位置 。
如果没有指定[file-name],默认为hostname-slow.log做为文件名,默认存放在DATADIR目录中 。
也可以将log-slow-queries配置到my.cnf文件中,这样就省去了每次在启动mysqld时都手工指定--log-slow-queries.例如:
# The MySQL server
[mysqld]
......
#slow-query-log
log-slow-queries = /var/lib/mysql/slow_query_log.log
......
2.读取慢查询日志
[mysql@test2]$ cat slow_query_log.log
/usr/local/mysql/bin/mysqld, Version: 5.0.26-standard-log. started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 080313 5:41:46
# User@Host: root[root] @ localhost []
# Query_time: 108 Lock_time: 0 Rows_sent: 0 Rows_examined: 8738
use test;
select count(1) from t1 a, t1 b,t1 c where a.id=b.id and b.name=c.name;
# Time: 080313 5:52:04
# User@Host: root[root] @ localhost []
# Query_time: 583 Lock_time: 0 Rows_sent: 0 Rows_examined: 508521177
select count(1) from t1 a, t1 b where a.id=b.id;
/usr/local/mysql/bin/mysqld, Version: 5.0.26-standard-log. started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 080313 10:39:59
# User@Host: root[root] @ localhost []
# Query_time: 11 Lock_time: 0 Rows_sent: 4537467 Rows_examined: 4537467
use test;
select id from tail;
如果慢查询日志记录很多可以使用mysqldumpslow进行分类汇总
[mysql@test2]$ mysqldumpslow slow_query_log.log
Reading mysql slow query log from slow_query_log.log
Count: 1 Time=583.00s (583s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
select count(N) from t1 a, t1 b where a.id=b.id
Count: 1 Time=108.00s (108s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
select count(N) from t1 a, t1 b,t1 c where a.id=b.id and b.name=c.name
Count: 1 Time=11.00s (11s) Lock=0.00s (0s) Rows=4537467.0 (4537467), root[root]@localhost
select id from tail;
mysql有以下几种日志:
错误日志:-log-err
查询日志:-log
慢查询日志:-log-slow-queries
更新日志:-log-update
二进制日志:-log-bin

推荐阅读