在慢查询里保留注释部分

前面在一个朋友的项目里面分析慢查询日志的时,看到注释也记录在日志里面,而且注释内容比较巧妙,如/* module_name-function_name*/,在SQL里面嵌入了使用这个SQL的模块和函数名称,这在调查SQL出处的时候非常方便。用别的办法比如全代码搜索,然后一个个看也可以做到。当时对注释是如何记录到日志的没明白怎么做到的,今天看到这篇文章时,才注意到-c这个参数。

文档里是这么说明的,默认是--skip-comments,不把注释发送到服务器

  -c, --comments      Preserve comments. Send comments to the server. The
                      default is --skip-comments (discard comments), enable
                      with --comments.

使用默认参数登陆后运行一个慢查询

[root@brucetest6 devdb6]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.5.20-log MySQL Community Server (GPL)
 
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> select /* this is test */ sleep(4);
+----------+
| sleep(4) |
+----------+
|        0 |
+----------+
1 row in set (4.00 sec)

记录在slow log里的是这样的

# Time: 130220 10:37:54
# User@Host: root[root] @ localhost []
# Query_time: 4.000347  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1361327874;
select  sleep(4);

带上-c参数后再执行慢查询

[root@brucetest6 devdb6]# mysql -uroot -p -c
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.5.20-log MySQL Community Server (GPL)
 
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> select /* this is test */ sleep(5);
+----------+
| sleep(5) |
+----------+
|        0 |
+----------+
1 row in set (5.00 sec)

慢日志里记录是这样的

# Time: 130220 10:49:09
# User@Host: root[root] @ localhost []
# Query_time: 5.000332  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1361328549;
select /* this is test */ sleep(5);

嗯,注释被记录下来了。也就是说,只要发送到server的SQL文本,都会被记录到日志里面。

原文地址:https://www.cnblogs.com/zuoxingyu/p/2918241.html