Home  >  Article  >  Database  >  使用MySQL Slow Log来解决MySQL CPU占用高的问题_MySQL

使用MySQL Slow Log来解决MySQL CPU占用高的问题_MySQL

WBOY
WBOYOriginal
2016-06-01 13:24:041219browse

bitsCN.com 但是怎么找到是哪个SQL语句的执行时间过长呢?可以通过MySQL Slow Log来找,详解如下。

首先找到MySQL的配置文件my.cnf,根据不同版本的mysql开启慢查询的配置也不一样


mysql 5.0

[mysqld]
long_query_time = 1
log-slow-queries = /var/log/mysql/slow.log


mysql 5.1

[mysqld]
long_query_time = 1
slow_query_log=1
slow_query_log_file = /var/log/mysql/slow.log


long_query_time 是指执行超过多久的sql会被log下来,这里是1秒。
log-slow-queries和slow_query_log_file 设置把日志写在哪里


把上述参数打开,运行一段时间,就可以关掉了,省得影响生产环境

接下来就是分析了,我这里的文件名字叫 /var/log/mysql/slow.log。
先mysqldumpslow

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn