Home  >  Article  >  Database  >  MYSQL5.7.9开启慢查询日志的技巧_MySQL

MYSQL5.7.9开启慢查询日志的技巧_MySQL

WBOY
WBOYOriginal
2016-05-27 13:45:151263browse

用MYSQL 5.7.9 作为ZABBIX 2.4.7 的监控数据库. 前段时间开启了慢查询日志, 后来发现慢查询日志膨胀到了700M

查看最后100条 大部分都是 0.1 秒的 后来想改, 以前是动态设置的 set global slow_query_log=1; 方式的 .

然后想直接用配置文件/etc/my.cnf 配慢查询

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at % of total RAM for dedicated server, else %.
innodb_buffer_pool_size = M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /LANMP/mysql
datadir = /MYSQLDATA/data
port = 
# server_id = .....
socket = /tmp/mysql.sock
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = M
sort_buffer_size = M
read_rnd_buffer_size = M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character_set_server=utf
init_connect='SET NAMES utf'
slow_query_log = 
slow_query_log_file = /MYSQLDATA/mysql_slow_query.log
long_query_time = 
log_queries_not_using_indexes = ON

反复启动了好多次 service mysqld restart

mysql> show variables like '%query%';
+------------------------------+---------------------------------+
| Variable_name | Value |
+------------------------------+---------------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| long_query_time | 5.000000 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | ON |
| slow_query_log_file | /MYSQLDATA/mysql_slow_query.log |
+------------------------------+---------------------------------+
13 rows in set (0.00 sec)

老是OFF 状态 而其他的 LONG_QUERY_TIME 得到了改变 5秒

用动态方式设置 报错

mysql> set global slow_query_log=1;
ERROR 29 (HY000): File '/MYSQLDATA/mysql_slow_query.log' not found (Errcode: 13 - Permission denied)

对我已经把它给删了, 为傻不自己创建个?

只好自己touch 一个

还是报错 ?

ps -ef |grep mysqld 发现 还有个mysql用户启动了mysql

看来 是用mysql用户读取慢查询日志的 用户权限问题啦

代码如下:


show variables like '%slow%';

执行结果会把是否开启慢查询、慢查询的秒数、慢查询日志等信息打印在屏幕上。

第三步:执行一次慢查询操作

其实想要执行一次有实际意义的慢查询比较困难,因为在自己测试的时候,就算查询有20万条数据的海量表,也只需要0.几秒。我们可以通过如下语句代替:

代码如下:


SELECT SLEEP(10);

第四步:查看慢查询的数量

通过如下sql语句,来查看一共执行过几次慢查询:

show global status like '%slow%';

mysql日志的配置:

注意:这些日文件在mysql重启的时候才会生成#记录所有sql语句

log=E:/mysqllog/mysql.log
#记录数据库启动关闭信息,以及运行过程中产生的错误信息
log-error=E:/mysqllog/myerror.log
# 记录除select语句之外的所有sql语句到日志中,可以用来恢复数据文件
log-bin=E:/mysqllog/bin
#记录查询慢的sql语句
log-slow-queries=E:/mysqllog/slow.log 
#慢查询时间
long_query_time=0.5

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