一、查询日志的概念:

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

二、实验部分:

  1. ----默认情况下查看是否启用查询日志:
  2. [root@node4 mysql5.5]# service mysql start
  3. Starting MySQL.... [ OK ]
  4. [root@node4 mysql5.5]# mysql
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 1
  7. Server version: 5.5.22-log Source distribution
  8.  
  9. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  10.  
  11. Oracle is a registered trademark of Oracle Corporation and/or its
  12. affiliates. Other names may be trademarks of their respective
  13. owners.
  14.  
  15. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  16. mysql> show variables like '%log';
  17. +--------------------------------+-------+
  18. | Variable_name | Value |
  19. +--------------------------------+-------+
  20. | back_log | 50 |
  21. | general_log | OFF |
  22. | innodb_locks_unsafe_for_binlog | OFF |
  23. | log | OFF |
  24. | relay_log | |
  25. | slow_query_log | OFF |
  26. | sync_binlog | 0 |
  27. | sync_relay_log | 0 |
  28. +--------------------------------+-------+
  29. 8 rows in set (0.00 sec)
  30. ----备注:loggeneral_log这两个参数是兼容的。而默认的情况下查询日志是不开启的
  31. ----使用下面的命令是开启查询日志
    mysql> set global log=1;
  32. Query OK, 0 rows affected, 1 warning (0.03 sec)
  33.  
  34. mysql> show variables like '%log';
  35. +--------------------------------+-------+
  36. | Variable_name | Value |
  37. +--------------------------------+-------+
  38. | back_log | 50 |
  39. | general_log | ON |
  40. | innodb_locks_unsafe_for_binlog | OFF |
  41. | log | ON |
  42. | relay_log | |
  43. | slow_query_log | OFF |
  44. | sync_binlog | 0 |
  45. | sync_relay_log | 0 |
  46. +--------------------------------+-------+
  47. 8 rows in set (0.00 sec)
  48. ----其中log参数是过时的,在启动选项中使用log参数的话,会在err日志中显示出来。
    ----修改my.cnf文件,添加log的参数设置
  49. [root@node4 mysql5.5]# vi my.cnf
  50. [root@node4 mysql5.5]# cat ./my.cnf |grep '^log='
  51. log=/tmp/mysqlgen.log
  52. ----清空err日志
    [root@node4 mysql5.5]# cat /dev/null > /tmp/mysql3306.err
  53. [root@node4 mysql5.5]# ll /tmp/mysql3306.err
  54. -rw-rw---- 1 mysql root 0 Jul 31 07:50 /tmp/mysql3306.err
  55. [root@node4 mysql5.5]# service mysql start
  56. Starting MySQL... [ OK ]
  57. ----启动数据库后查看err日志的内容
    [root@node4 mysql5.5]# cat /tmp/mysql3306.err
  58. 130731 07:51:32 mysqld_safe Starting mysqld daemon with databases from /opt/mysql5.5/data
  59. 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.
  60. 130731 7:51:33 InnoDB: The InnoDB memory heap is disabled
  61. 130731 7:51:33 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
  62. 130731 7:51:33 InnoDB: Compressed tables use zlib 1.2.3
  63. 130731 7:51:33 InnoDB: Initializing buffer pool, size = 128.0M
  64. 130731 7:51:33 InnoDB: Completed initialization of buffer pool
  65. 130731 7:51:33 InnoDB: highest supported file format is Barracuda.
  66. 130731 7:51:33 InnoDB: Waiting for the background threads to start
  67. 130731 7:51:34 InnoDB: 1.1.8 started; log sequence number 1625855
  68. 130731 7:51:34 [Note] Event Scheduler: Loaded 0 events
  69. 130731 7:51:34 [Note] /opt/mysql5.5/bin/mysqld: ready for connections.
  70. Version: '5.5.22-log' socket: '/tmp/mysql.sock' port: 3306 Source distribution
  71. ----使用最新的参数
  72. ----general_log和general_log_file。
  73.  
  74. [root@node4 mysql5.5]# service mysql stop
  75. Shutting down MySQL. [ OK ]
  76. [root@node4 mysql5.5]# vi my.cnf
  77. [root@node4 mysql5.5]# cat ./my.cnf |grep '^general'
  78. general_log = 1
  79. general_log_file = /tmp/mysqlgen.log
  80. [root@node4 mysql5.5]# service mysql start
  81. Starting MySQL... [ OK ]
  82. [root@node4 mysql5.5]# mysql
  83. Welcome to the MySQL monitor. Commands end with ; or \g.
  84. Your MySQL connection id is 1
  85. Server version: 5.5.22-log Source distribution
  86.  
  87. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  88.  
  89. Oracle is a registered trademark of Oracle Corporation and/or its
  90. affiliates. Other names may be trademarks of their respective
  91. owners.
  92.  
  93. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  94.  
  95. mysql> show variables like '%log';
  96. +--------------------------------+-------+
  97. | Variable_name | Value |
  98. +--------------------------------+-------+
  99. | back_log | 50 |
  100. | general_log | ON |
  101. | innodb_locks_unsafe_for_binlog | OFF |
  102. | log | ON |
  103. | relay_log | |
  104. | slow_query_log | OFF |
  105. | sync_binlog | 0 |
  106. | sync_relay_log | 0 |
  107. +--------------------------------+-------+
  108. 8 rows in set (0.04 sec)
  109.  
  110. mysql> show variables like '%file';
  111. +---------------------+-----------------------------------+
  112. | Variable_name | Value |
  113. +---------------------+-----------------------------------+
  114. | ft_stopword_file | (built-in) |
  115. | general_log_file | /tmp/mysqlgen.log |
  116. | init_file | |
  117. | local_infile | ON |
  118. | pid_file | /tmp/mysql3306.pid |
  119. | relay_log_info_file | relay-log.info |
  120. | slow_query_log_file | /opt/mysql5.5/data/node4-slow.log |
  121. +---------------------+-----------------------------------+
  122. 7 rows in set (0.00 sec)
  123. ----在上面的操作中可以看到已经启用查询日志,并且文件目录是/tmp/mysqlgen.log。
    ----查询日志记录了哪些东西?
  124. 进行下面的查询
    mysql> show databases;
  125. +--------------------+
  126. | Database |
  127. +--------------------+
  128. | information_schema |
  129. | mysql |
  130. | performance_schema |
  131. | test |
  132. | test2 |
  133. +--------------------+
  134. 5 rows in set (0.08 sec)
  135.  
  136. mysql> use test;
  137. Database changed
  138. mysql> show tables;
  139. Empty set (0.00 sec)
  140.  
  141. mysql> use test2;
  142. Database changed
  143. mysql> show tables;
  144. +-----------------+
  145. | Tables_in_test2 |
  146. +-----------------+
  147. | course |
  148. | jack |
  149. | sc |
  150. | student |
  151. | t |
  152. | teacher |
  153. +-----------------+
  154. 6 rows in set (0.07 sec)
  155.  
  156. mysql> drop table t;
  157. Query OK, 0 rows affected (0.13 sec)
  158.  
  159. mysql> select * from sc;
  160. Empty set (0.04 sec)
  161.  
  162. ----可以看到上面的操作都记录在了mysqlgen.log里面。
    [root@node4 ~]# tail -f /tmp/mysqlgen.log
  163. /opt/mysql5.5/bin/mysqld, Version: 5.5.22-log (Source distribution). started with:
  164. Tcp port: 3306 Unix socket: /tmp/mysql.sock
  165. Time Id Command Argument
  166. 130731 7:55:41 1 Query show databases
  167. 130731 7:55:56 1 Query SELECT DATABASE()
  168. 1 Init DB test
  169. 130731 7:55:59 1 Query show tables
  170. 130731 7:56:19 1 Query SELECT DATABASE()
  171. 1 Init DB test2
  172. 130731 7:56:23 1 Query show tables
  173. 130731 7:56:27 1 Query drop table t
  174. 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. 前端实战——前端效果accordition的实现

    一.bootstrap实现 1)水平折叠组件 使用panel和collaspe组件 <!doctype html> <html lang="zh-hans"> ...

  2. Go prepare statment超过mysql最大数

    mysql_stmt_prepare failed! error(1461)Can't create more than max_prepared_stmt_count statements (cur ...

  3. Swing 顶层容器

    顶层容器值得是容纳其他容器的容器组件,包括JFrame类,JWindows类,JDialog类,JApplet等.Swing中的顶层容器类Swing提供三个顶层容器类:JFrame,JDialog和J ...

  4. 网卡流量查看软件bmon

    bmon 时 linux下最常用的查看网络带宽的工具,debian下直接进行安装即可 apt-get install bmon redhat下可以在这里寻找到合适版本的rpm包,安装完毕后执行bmon ...

  5. Oracle sqlldr导入导出txt数据文件详解

    一.sqlldr导入txt 1.预备 a).txt文件 这里要保存成无签名的UTF-8 b).oracle建表 2.编写控制文件input_test.ctl LOAD DATA CHARACTERSE ...

  6. linux 缺少动态连接库.so--cannot open shared object file: No such file or directory

    error while loading shared libraries的解決方法  执行行程式時,如此遇到像下列這種錯誤: ./tests: error while loading shared l ...

  7. From Disk partition to PostgreSQL installation

    From Disk partition to PostgreSQLinstallation [root@compute mnt]# fdisk /dev/sdb Welcome to fdisk (u ...

  8. Lintcode: Route Between Two Nodes in Graph

    Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...

  9. JAVA-封装-静态属性

    1.使用 1.static 2.用来修饰属性.方法.内部类.代码块 3.称为类属性,静态属性,类方法,静态方法 3.不需要实例化,直接用类名或静态成员名调用 2.特点 1.静态属性对于类的所有实例是共 ...

  10. cookie 使用笔记

    参考书<JSP Web 开发案例教程> index.jsp页面 dologin.jsp页面 welcome.jsp页面 页面显示 点击提交