暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL FAQ:MySQL 开启慢查询日志的方法

原创 eygle 2021-11-18
553

MySQL 默认是没有开启慢查询日志的,可以通过命令行或者修改 my.cnf 来开启。开启后对性能有一定的影响,生产环境不建议开启。我的版本是 5.7.35,下面分别介绍两种开启方式。

[root@enmotech eygle]# mysql -hlocalhost -uenmo -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 140609
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

复制

命令行

  1. 首先是进入到 MySQL 的服务中
  2. 查看下关于慢查询的配置: show variables like ‘slow_query_log%’;
  3. 查看默认超时时间:show variables like ‘long_query_time%’;
    image.png

第一行可以看到配置为 OFF,没有开启;第二行是慢查询日志的保存路径,默认值就可以不修改。默认的超时时间配置
超时时间是 10 秒。

在线修改

可以将超时时间修改为 1 秒: set global long_query_time=1;
然后开启慢查询监控: set global slow_query_log=1;
退出 MySQL 并重新进入,测试一下:select sleep(5);
检查日志。

/usr/sbin/mysqld, Version: 5.7.35 (MySQL Community Server (GPL)). started with:
Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock
Time                 Id Command    Argument
# Time: 2021-11-18T04:00:53.815999Z
# Query_time: 5.000231  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1637208053;
select sleep(5);

复制

修改配置文件

修改 my.cnf,在 [mysqld] 添加下面配置

slow_query_log=1
slow_query_log_file=/var/lib/mysql/slow-log.log
long_query_time=1
复制

zCloud 纳管

现在,用 zCLoud 纳管了这个 MySQL 数据库看看效果:
ES 20211118 at 12.19.27.png

性能监控:
image.png

云和恩墨 zCLoud ,欢迎体验。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论