mysql初识之日志文件篇(一)

2014-11-24 14:01:20 · 作者: · 浏览: 1
mysql初识之日志文件篇
日志文件
1. err日志
error log 记录mysql在运行的过程中所有较为严重的警告和错误信息,以及mysql server每次启动和关闭的详细信息。系统在默认情况下关闭error log 功能。error log 默认存放在data目录下,默认文件名为主机名.err。error log 通过两种方式开启:
1). 启动mysqld时,附加上--log-error参数
2) . 在my.cnf中配置log-error系统环境变量
当进入mysql后执行 flush logs命令后,mysql会将当前的错误日志文件附加-old文件保存,并且创建一个新的空错误日志文件,仅仅在指定--log-error的情况下。
注:error log 不全是保存是警告和错误信息,还保存每次启动和关闭的信息。
ps启动时信息:
130420 16:19:29 mysqld_safe mysqld from pid file /data0/mysql_data_7706/FZTEST-24178.pid ended
130420 16:19:30 mysqld_safe Starting mysqld daemon with databases from /data0/mysql_data_7706
error: Found option without preceding group in config file: /data0/mysql_data_7706/my.cnf at line: 1
Fatal error in defaults handling. Program aborted
130420 16:19:30 [Note] Plugin 'FEDERATED' is disabled.
130420 16:19:30 [Note] Plugin 'ndbcluster' is disabled.
130420 16:19:30  InnoDB: Initializing buffer pool, size = 8.0M
130420 16:19:30  InnoDB: Completed initialization of buffer pool
(以下省略)

 查看error 日志是否启动:
mysql> show variables like 'log_error';
+---------------+---------------------+
| Variable_name | Value                        |
+---------------+---------------------+
| log_error          | /var/log/mysqld.log |
+---------------+---------------------+
1 row in set (0.00 sec)

关闭mysql:  mysqladmin -uroot -p shutdown 

2. 二进制日志(binlog)
binlog日志主要记录mysql的更新内容,记录每一条更新语句的执行的时间、消耗的资源,以及相关的事务信息。可以用于实时备份,与master/slave复制。
如何打开:
my.cnf中[mysqld] 节点上添加
查看binlog是否打开
mysql> show variables  like 'log_%';
+---------------------------------+-----------------+
| Variable_name                   | Value           |
+---------------------------------+-----------------+
| log_bin                             | ON              |

查看binlog内容:
 /usr/local/mysql3306/bin/mysqlbinlog mysql-bin.00002

log-bin-index文件作用:
记录目录所有binlog文件
[root@FZTEST-24178 mysql_data_7706]# cat mysql_7706-relay-bin.index
./mysql_7706-relay-bin.000032

binlog格式说明:
[root@localhost ~]# mysqlbinlog  /home/mysql/binlog/binlog.000003  | more
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#120330 16:51:46 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 120330 1
6:51:46
# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
# at 196
#120330 17:54:15 server id 1  end_log_pos 294   Query   thread_id=3     exec_time=2     error_code=0
SET TIMESTAMP=1333101255/*!*/;
insert into tt7 select * from tt7/*!*/;
# at 294
#120330 17:54:46 server id 1  end_log_pos 388   Query   thread_id=3     exec_time=28    error_code=0
SET TIMESTAMP=1333101286/*!*/;
alter table tt7 engine=innodb/*!*/;

解析binlog格式
位置
位于文件中的位置,“at 196”说明“事件”的起点,是以第196字节开始;“end_log_pos 294”说明以第294字节结束
时间戳
事件发生的时间戳:“120330 17:54:46”
事件执行时间
事件执行花费的时间:"exec_time=28"
错误码
错误码为:“error_code=0”
服务器的标识
服务器的标识id:“server id 1”
其他参数说明:
1). binlog_do_db:表示记录指定 数据库的二进制日志
2).binlog_ignore_db:表示忽略指定的数据库的二进制日志
3). max_binlog_cache_size:表示使用binlog时最大的内存值
4). binlog_cache_size
此参数表示binlog使用的内存大小,可以通过状态变量binlog_cache_use和binlog_cache_disk_use来帮助测试。
binlog_cache_use:使用二进制日志