一、查询日志的概念:

查询日志记录MySQL中所有的query,通过"--log[=file_name]"来打开该功能。由于记录了所有的query,包括所有的select,体积比较大,开启后对性能也有比较大的影响,所以请大家慎用该功能。一般只用于跟踪某些特殊的sql性能问题才会短暂打开该功能。默认的查询日志文件名为:hostname.log. 
  To enable the general query log as of MySQL 5.1.6,start mysqld with the --log option,and optionally use --log-ouput to specify the log output destination as described in Section 5.11.1,"Server Log Tables",Before 5.1.6,enable the general query log file with the --log[=file_name]or -l[file_name] option.If no file_name value is give,the default name is host_name,log in the data directory.
  Server restarts and log flushing do not cause a new general query log file to be generated (although flushingcloses and reopens it).On Unix,you can rename the file and create a new one by using the following commands:

shell> my host_name.log host_name-old.log
  shell> mysqladmin flush-logs
  shell> cp host_name-old.log backup-directory
  shell> rm host_name-old.log

二、实验部分:

----默认情况下查看是否启用查询日志:
[root@node4 mysql5.5]# service mysql start
Starting MySQL.... [ OK ]
[root@node4 mysql5.5]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.22-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like '%log';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| back_log | 50 |
| general_log | OFF |
| innodb_locks_unsafe_for_binlog | OFF |
| log | OFF |
| relay_log | |
| slow_query_log | OFF |
| sync_binlog | 0 |
| sync_relay_log | 0 |
+--------------------------------+-------+
8 rows in set (0.00 sec)
----备注:log和general_log这两个参数是兼容的。而默认的情况下查询日志是不开启的
----使用下面的命令是开启查询日志
mysql> set global log=1;
Query OK, 0 rows affected, 1 warning (0.03 sec) mysql> show variables like '%log';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| back_log | 50 |
| general_log | ON |
| innodb_locks_unsafe_for_binlog | OFF |
| log | ON |
| relay_log | |
| slow_query_log | OFF |
| sync_binlog | 0 |
| sync_relay_log | 0 |
+--------------------------------+-------+
8 rows in set (0.00 sec)
----其中log参数是过时的,在启动选项中使用log参数的话,会在err日志中显示出来。
----修改my.cnf文件,添加log的参数设置
[root@node4 mysql5.5]# vi my.cnf
[root@node4 mysql5.5]# cat ./my.cnf |grep '^log='
log=/tmp/mysqlgen.log
----清空err日志
[root@node4 mysql5.5]# cat /dev/null > /tmp/mysql3306.err
[root@node4 mysql5.5]# ll /tmp/mysql3306.err
-rw-rw---- 1 mysql root 0 Jul 31 07:50 /tmp/mysql3306.err
[root@node4 mysql5.5]# service mysql start
Starting MySQL... [ OK ]
----启动数据库后查看err日志的内容
[root@node4 mysql5.5]# cat /tmp/mysql3306.err
130731 07:51:32 mysqld_safe Starting mysqld daemon with databases from /opt/mysql5.5/data
130731 7:51:32 [Warning] The syntax '--log' is deprecated and will be removed in a future release. Please use '--general-log'/'--general-log-file' instead.
130731 7:51:33 InnoDB: The InnoDB memory heap is disabled
130731 7:51:33 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
130731 7:51:33 InnoDB: Compressed tables use zlib 1.2.3
130731 7:51:33 InnoDB: Initializing buffer pool, size = 128.0M
130731 7:51:33 InnoDB: Completed initialization of buffer pool
130731 7:51:33 InnoDB: highest supported file format is Barracuda.
130731 7:51:33 InnoDB: Waiting for the background threads to start
130731 7:51:34 InnoDB: 1.1.8 started; log sequence number 1625855
130731 7:51:34 [Note] Event Scheduler: Loaded 0 events
130731 7:51:34 [Note] /opt/mysql5.5/bin/mysqld: ready for connections.
Version: '5.5.22-log' socket: '/tmp/mysql.sock' port: 3306 Source distribution
----使用最新的参数
----general_log和general_log_file。 [root@node4 mysql5.5]# service mysql stop
Shutting down MySQL. [ OK ]
[root@node4 mysql5.5]# vi my.cnf
[root@node4 mysql5.5]# cat ./my.cnf |grep '^general'
general_log = 1
general_log_file = /tmp/mysqlgen.log
[root@node4 mysql5.5]# service mysql start
Starting MySQL... [ OK ]
[root@node4 mysql5.5]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.22-log Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show variables like '%log';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| back_log | 50 |
| general_log | ON |
| innodb_locks_unsafe_for_binlog | OFF |
| log | ON |
| relay_log | |
| slow_query_log | OFF |
| sync_binlog | 0 |
| sync_relay_log | 0 |
+--------------------------------+-------+
8 rows in set (0.04 sec) mysql> show variables like '%file';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| ft_stopword_file | (built-in) |
| general_log_file | /tmp/mysqlgen.log |
| init_file | |
| local_infile | ON |
| pid_file | /tmp/mysql3306.pid |
| relay_log_info_file | relay-log.info |
| slow_query_log_file | /opt/mysql5.5/data/node4-slow.log |
+---------------------+-----------------------------------+
7 rows in set (0.00 sec)
----在上面的操作中可以看到已经启用查询日志,并且文件目录是/tmp/mysqlgen.log。
----查询日志记录了哪些东西?
进行下面的查询
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test2 |
+--------------------+
5 rows in set (0.08 sec) mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec) mysql> use test2;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_test2 |
+-----------------+
| course |
| jack |
| sc |
| student |
| t |
| teacher |
+-----------------+
6 rows in set (0.07 sec) mysql> drop table t;
Query OK, 0 rows affected (0.13 sec) mysql> select * from sc;
Empty set (0.04 sec) ----可以看到上面的操作都记录在了mysqlgen.log里面。
[root@node4 ~]# tail -f /tmp/mysqlgen.log
/opt/mysql5.5/bin/mysqld, Version: 5.5.22-log (Source distribution). started with:
Tcp port: 3306 Unix socket: /tmp/mysql.sock
Time Id Command Argument
130731 7:55:41 1 Query show databases
130731 7:55:56 1 Query SELECT DATABASE()
1 Init DB test
130731 7:55:59 1 Query show tables
130731 7:56:19 1 Query SELECT DATABASE()
1 Init DB test2
130731 7:56:23 1 Query show tables
130731 7:56:27 1 Query drop table t
130731 7:56:39 1 Query select * from sc

Mysql query log的更多相关文章

  1. mysql general log 查看mysql 运行历史

    我们有时候须要查看mysql的运行历史,比方我们做sql优化的时候,起码要知道运行的sql是什么.框架通常会帮我们拼装sql,所以在程序中不一定能够打印出sql,这个时候就须要mysql的genera ...

  2. MySQL:动态开启慢查询日志(Slow Query Log)

    前言 在开发中,高效能的程序 也包括 高效能的查询,所以优化SQL也是程序员必要技能之一.要优化就必须要有慢日志记录才可以知道哪些查询慢,然后反向去修改 慢日志设置方式 写入文件 写入数据库 实践操作 ...

  3. Mysql slow query log

    一.概念部分:  顾名思义,慢查询日志中记录的是执行时间较长的query,也就是我们常说的slow query,通过设--log-slow-queries[=file_name]来打开该功能并设置记录 ...

  4. mysql中slow query log慢日志查询分析

    在mysql中slow query log是一个非常重要的功能,我们可以开启mysql的slow query log功能,这样就可以分析每条sql执行的状态与性能从而进行优化了. 一.慢查询日志 配置 ...

  5. mysql慢查询Slow Query Log和未使用索引(Not Using Indexes)查询配置和使用

    mysql的“慢查询”指的是超过了允许的最大查询时间(long_query_time)的sql语句,而“未使用索引”查询顾名思义就是查询语句没有使用到索引的sql语句. 慢查询配置和使用 在msyql ...

  6. MySQL 一般查询日志(General Query Log)

    与大多数关系型数据库,日志文件是MySQL数据库的一个重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志.慢查询日志,等等. 这些日志能够帮助我们定位mysqld ...

  7. MySQL 通用查询日志(General Query Log)

      同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等.这些日志可以帮助我们定位mysql ...

  8. MySQL专题 2 数据库优化 Slow Query log

    MySQL Server 有四种类型的日志——Error Log.General Query Log.Binary Log 和 Slow Query Log. 第一个是错误日志,记录 mysqld 的 ...

  9. MySQL 慢查询日志(Slow Query Log)

    同大多数关系型数据库一样.日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件.通常包含错误日志文件,二进制日志,通用日志.慢查询日志.等等.这些日志能够帮助我们定位mysqld内 ...

随机推荐

  1. JBOSS的安全配置 .

    JBoss版本:JBoss 4.2.2.GA jboss默认配置了以下服务:JMX ConsoleJBoss Web Console为了安全起见,需要用户通过授权进行访问. 一.Java 鉴别与授权服 ...

  2. Android大图片裁剪终极解决方案(上:原理分析)

    转载声明:Ryan的博客文章欢迎您的转载,但在转载的同时,请注明文章的来源出处,不胜感激! :-)  http://my.oschina.net/ryanhoo/blog/86842 约几个月前,我正 ...

  3. .NET Framework Execution Was Aborted By Escalation Policy

    错误#1 09:31 2015/1/26上班查看ERRORLOG发现下面错误信息字面上理解是有内存压力,中午的时候ERRORLOG频繁报下面错误问题核实,一台服务器上安装两个实例,其中一个实例设置了最 ...

  4. Linux 安装php

    安装libxml2 下载解压 libxml2-2.6.32.tar.gz 安装 ./configure --prefix=/usr/local/libxml2makesudo make install ...

  5. 在一个Label上设置多种颜色字体

    #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL ...

  6. 前端构建工具gulpjs

    gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非常简单,学习起来很容易,而且gulpjs使用的是nodejs中stream来读取和操作数据,其速 ...

  7. 当As3遇见Swift(二)

    字符串:String 都是用String来表示,都是值类型,在传递过程中都会进行拷贝. 计算字符数量 As3: str.length Swift: countElements(str) 数组:Arra ...

  8. G面经prepare: set difference

    给你A{1,2,3,4,4,5}, B{2,4},求A-B={1,3,4,5},很简单. visit 1 只用一个HashMap package TwoSets; import java.util.* ...

  9. 树形DP(01组合背包The Ghost Blows Light HDU4276)

    题意:有n个房间,之间用n-1条道路连接,每个房间都有一个定时炸弹,在T时间后会一起爆炸,第i个房间有pi价值的珠宝,经过每条道路都需要花费一定的时间,一个人从1房间开始 ,从n房间出去,保证再不炸死 ...

  10. ACM之Java速成(2)

    acm中Java的应用 Chapter I. Java的优缺点各种书上都有,这里只说说用Java做ACM-ICPC的特点: (1) 最明显的好处是,学会Java,可以参加Java Challenge ...