技术开发 频道

MySQL日志管理

  二.二进制日志

  二进制日志也通常被称为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)

0
相关文章