文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>mysql 开启慢查询命令【转】

mysql 开启慢查询命令【转】

时间:2011-01-06  来源:dhb133

mysql> select @@global.log_output;
+---------------------+
| @@global.log_output |
+---------------------+
| FILE                |
+---------------------+
1 row in set (0.00 sec)

注意在MySQL5.1就开始支持把慢查询的日志记录放到mysq.slow_log中,但需要设置log_output变量值为TABLE:
mysql> set @@global.log_output='TABLE';
Query OK, 0 rows affected (0.00 sec)

mysql> desc mysql.slow_log;
+----------------+------------------+------+-----+-------------------+-----------------------------+
| Field          | Type             | Null | Key | Default           | Extra                       |
+----------------+------------------+------+-----+-------------------+-----------------------------+
| start_time     | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_host      | mediumtext       | NO   |     | NULL              |                             |
| query_time     | time             | NO   |     | NULL              |                             |
| lock_time      | time             | NO   |     | NULL              |                             |
| rows_sent      | int(11)          | NO   |     | NULL              |                             |
| rows_examined  | int(11)          | NO   |     | NULL              |                             |
| db             | varchar(512)     | NO   |     | NULL              |                             |
| last_insert_id | int(11)          | NO   |     | NULL              |                             |
| insert_id      | int(11)          | NO   |     | NULL              |                             |
| server_id      | int(10) unsigned | NO   |     | NULL              |                             |
| sql_text       | mediumtext       | NO   |     | NULL              |                             |
+----------------+------------------+------+-----+-------------------+-----------------------------+
11 rows in set (0.01 sec)

注意,文件记录和写表记录只会有一种生效。


修改之前:
mysql> show variables like '%slow%';
+---------------------+------------------------------------------------+
| Variable_name       | Value                                          |
+---------------------+------------------------------------------------+
| log_slow_queries    | OFF                                            |
| slow_launch_time    | 2                                              |
| slow_query_log      | OFF                                            |
| slow_query_log_file | /usr/local/mysql-5.1.36/var/localhost-slow.log |
+---------------------+------------------------------------------------+
mysql>
4 rows in set (0.00 sec)

注意这个时候localhost-slow.log文件是不存在的:
mysql> system cat /usr/local/mysql-5.1.36/var/localhost-slow.log
cat: /usr/local/mysql-5.1.36/var/localhost-slow.log: No such file or directory

修改slow_query_log的方法:
mysql> warnings;
Show warnings enabled.
mysql> set @@global.log_slow_queries=ON;
Query OK, 0 rows affected, 1 warning (0.01 sec)
Warning (Code 1287): The syntax '@@log_slow_queries' is deprecated and will be removed in MySQL 7.0. Please use '@@slow_query_log' instead
注意已经不再使用log_slow_queries参数,用slow_query_log替代,这样修改slow_query_log时,log_slow_queries也会被隐性地修改:

mysql> set @@global.slow_query_log=ON;
Query OK, 0 rows affected (0.00 sec)

修改之后:
mysql> show variables like '%slow%';
+---------------------+------------------------------------------------+
| Variable_name       | Value                                          |
+---------------------+------------------------------------------------+
| log_slow_queries    | ON                                             |
| slow_launch_time    | 2                                              |
| slow_query_log      | ON                                             |
| slow_query_log_file | /usr/local/mysql-5.1.36/var/localhost-slow.log |
+---------------------+------------------------------------------------+
4 rows in set (0.00 sec)

这时localhost-slow.log文件已经建立:
mysql> system cat /usr/local/mysql-5.1.36/var/localhost-slow.log
/usr/local/mysql-5.1.36/libexec/mysqld, Version: 5.1.36-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                 Id Command    Argument

分析时我们可以直接查询慢查询日志,也可以通过mysqldumpslow命令(推荐)来解析这个文件:
[root@localhost var]# mysqldumpslow localhost-slow.log
Reading mysql slow query log from localhost-slow.log
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), 0users@0hosts

我们可以通过sleep函数来做简单的测试:
如:select sleep(11);

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载