任何一种数据库中,都有各种各样的日志。Mysql也不例外,在Mysql中有4种不同的日志、分别错误日志、二进制日志、查询日志和慢查询日志。这些日志记录着Mysql数据库不同方面的踪迹。下文将介绍这4种不同的日志作用和用途。
一.错误日志
错误日志在Mysql数据库中很重要,它记录着mysqld启动和停止,以及服务器在运行过程中发生的任何错误的相关信息。
1.配置信息
--log-error=[file-name]用来指定错误日志存放的位置。
如果没有指定[file-name],默认hostname.err做为文件名,默认存放在DATADIR目录中。
也可以将log-error配置到my.cnf文件中,这样就省去了每次在启动mysqld时都手工指定--log-error.例如:
[mysql@test2]$ vi /etc/my.cnf
# The MySQL server
[mysqld]
....
log-error = /var/lib/mysql/test2_mysqld.err
.....
2.错误信息样板
080313 05:21:55 mysqld started
080313 5:21:55 InnoDB: Started; log sequence number 0 43655
080313 5:21:55 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.26-standard-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Edition - Standard (GPL)
080313 5:24:13 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown
080313 5:24:13 InnoDB: Starting shutdown...
080313 5:24:16 InnoDB: Shutdown completed; log sequence number 0 43655
080313 5:24:16 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
080313 05:24:16 mysqld ended
080313 05:24:47 mysqld started
080313 5:24:47 InnoDB: Started; log sequence number 0 43655
080313 5:24:47 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.0.26-standard-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Edition - Standard (GPL)
080313 5:33:49 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown
三.查询日志
查询日志记录了clinet的所有的语句。
Note:由于log日志记录了数据库所有操作,对于访问频繁的系统,此种日志会造成性能影响,建议关闭。
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 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
# : 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
# : 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
# : 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
在mysql的安装目录下,打开my.ini,在后面加上上面的参数,保存后重启mysql服务就行了。
例如:
#Enter a name for the binary log. Otherwise a default name will be used.
#log-bin=
#Enter a name for the query log file. Otherwise a default name will be used.
#log=
#Enter a name for the error log file. Otherwise a default name will be used.
log-error=
#Enter a name for the update log file. Otherwise a default name will be used.
#log-update=
查看日至:
1. 首先确认你日志是否启用了
mysql>show variables like 'log_bin';
如果启用了,即ON
那日志文件就在mysql的安装目录的data目录下
cat/tail 日志文件名
2. 怎样知道当前的日志
mysql> show master status;
3. 查看从某一段时间到某一段时间的日志
mysqlbinlog --start-datetime='2008-01-19 00:00:00' --stop-datetime='2008-01-30 00:00:00' /var/log/mysql/mysql-bin.000006 > mysqllog1.log
附录:
//显示所有本机上的二进制日志
mysql> SHOW MASTER LOGS;
//删除所有本机上的二进制日志
mysql> RESET MASTER;
//删除所有创建时间在binary-log.xxx之前的二进制日志
mysql> PURGE MASTER LOGS TO 'binary-log.xxx';
//只保留最近6天的日志,之前的都删掉
find /var/intra -type f -mtime +6 -name "*.log" -exec rm -f {} ;
//用键盘左上角(也就是Esc下面)那个键包围起来,说明是命令。-1d是昨天,以此类推-1m是上个月等等
day=`/bin/date -v -1d +%Y%m%d`;
//给文件改名
mv xxx.log xxx-${day}.log;
//这里还要加上数据库的用户名密码,作用是更新日志(包括二进制日志和查询日志等等)
mysqladmin flush-logs
二进制日志也通常被称为binlog,它记当着所有的DDL和DML,但不包括数据查询语句。
1.配置信息
--log-bin=[file-name]用来指定错误日志存放的位置。
如果没有指定[file-name],默认为主机名后面跟-bin做为文件名,默认存放在DATADIR目录中。
也可以将log-bin配置到my.cnf文件中,这样就省去了每次在启动mysqld时都手工指定--log-bin.例如:
# The MySQL server
[mysqld]
......
log-bin = /var/lib/mysql/log-bin
......
2.查看blnlog
由于binlog以是binary方式存取,不能直接查看,需要用mysql提供的mysqlbinlog工具查看。
3.删除binlog
(1).用reset master命令删除所有日志,新日志重新从000001开始编号
(2).用purge master logs to 'mysq-bin.******' 命令可以删除指定编号前的所有日志
(3).用purge master logs to before 'YYYY-MM-DD HH24:MI:SS'命令可以删除'YYYY-MM-DD HH24:MI:SS'之前的产生的所有日志
(4).可以在my.cnf中指定--expire_logs_days=#,此参数设置了binlog日志的过期天数
4.测试案例
[mysql@test2]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 18 to server version: 5.0.26-standard-log
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> use test;
Database changed
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+--------+---------+------+------------+------------+
8 rows in set (0.06 sec)
mysql> insert into pet values('hunter','yxyup','cat','f','1996-04-29',null);
Query OK, 1 row affected (0.03 sec)
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| hunter | yxyup | cat | f | 1996-04-29 | NULL |
+----------+--------+---------+------+------------+------------+
9 rows in set (0.00 sec)
mysql> update pet set sex='m' where name='hunter';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> delete from pet where name='hunter';
Query OK, 1 row affected (0.00 sec)
[mysql@test2]$ mysqlbinlog log-bin.000002
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
# at 4
#080313 7:52:47 server id 1 end_log_pos 98 Start: binlog v 4, server v 5.0.26-standard-log created 080313 7:52:47
# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
# at 98
#080313 10:06:26 server id 1 end_log_pos 229 Query thread_id=18 exec_time=0 error_code=0
use test;
SET TIMESTAMP=1205373986;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1;
SET @@session.sql_mode=0;
/*!C latin1 */;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8;
insert into pet values('hunter','yxyup','cat','f','1996-04-29',null);
# at 229
#080313 10:07:13 server id 1 end_log_pos 334 Query thread_id=18 exec_time=0 error_code=0
SET TIMESTAMP=1205374033;
update pet set sex='m' where name='hunter';
# at 334
#080313 10:07:38 server id 1 end_log_pos 432 Query thread_id=18 exec_time=0 error_code=0
SET TIMESTAMP=1205374058;
delete from pet where name='hunter';
# at 432
#080313 10:14:13 server id 1 end_log_pos 532 Query thread_id=18 exec_time=0 error_code=0
SET TIMESTAMP=1205374453;
create table t1(id int,name char(10));
# at 532
#080313 10:14:41 server id 1 end_log_pos 625 Query thread_id=18 exec_time=0 error_code=0
SET TIMESTAMP=1205374481;
alter table t1 add sex char(2);
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET ;
[mysql@test2]$
可以看出,三条DML操作和两条DDL都记录到了binlog中了,而select并没有记录。
---★ 本文转摘自『IT学习者』→