慢日志

配置慢日志是为了帮我们分析 mysql 的瓶颈点。

配置慢日志

MariaDB [(none)]> show variables like 'slow%';
+---------------------+--------------------+
| Variable_name       | Value              |
+---------------------+--------------------+
| slow_launch_time    | 2                  |
| slow_query_log      | OFF                |这里表示slow日志没有开启
| slow_query_log_file | localhost-slow.log |这里表示slow日志文件名称
+---------------------+--------------------+
3 rows in set (0.033 sec)


MariaDB [(none)]> show variables like 'datadir';
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| datadir       | /data/mysql/ |     这里是slow日志存放的路径
+---------------+--------------+
1 row in set (0.007 sec)


MariaDB [(none)]> show variables like 'long%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |          这里定义的是超过响应时间超过10秒会记录日志
+-----------------+-----------+
1 row in set (0.004 sec)

配置 慢日志

打开my.conf 配置文件,增加以下内容

slow_query_log = ON											#打开slow日志
slow_query_log_file = /data/mysql/bbs-slow.log				#定义slow日志名字
long_query_time = 2											#响应时间超过2秒就记录。

## 重启数据库


# 使用select语句模拟慢查询。
select sleep(5);

# 查询慢日志
cat /data/mysql/bbs-slow.log
/usr/local/mysql/bin/mysqld, Version: 10.3.12-MariaDB-log (MariaDB Server). started with:
Tcp port: 0 Unix socket: /tmp/mysql.sock
Time Id Command Argument
# Time: 190225 4:32:49
# User@Host: root[root] @ localhost []
# Thread_id: 9 Schema: QC_hit: No
# Query_time: 5.007798 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
# Rows_affected: 0 Bytes_sent: 63
SET timestamp=1551087169;
select sleep(5);

补充关于MySQL的一个命令

  • show processlist ; 用于查看数据库中的查询队列,相当于查看系统的进程,可以比较好的了解数据库是否处于忙碌状态
  • show full processlist ;跟上一个命令一样,这个指令会将队列中的查询语句完整的列出来。