Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1728481
  • 博文数量: 163
  • 博客积分: 10591
  • 博客等级: 上将
  • 技术积分: 1980
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-08 18:17
文章分类

全部博文(163)

文章存档

2018年(1)

2012年(1)

2011年(47)

2010年(58)

2009年(21)

2008年(35)

分类: Mysql/postgreSQL

2010-03-31 11:41:56

一.错误日志

  错误日志在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


二.二进制日志

  二进制日志也通常被称为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 COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

  [mysql@test2]$

  可以看出,三条DML操作和两条DDL都记录到了binlog中了,而select并没有记录。


三.查询日志

  查询日志记录了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 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;

阅读(971) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~