Mysql 设置 慢SQL时间并触发邮件

Mysql 设置 慢SQL时间,并触发邮件

1. 临时设置(会话级别)
sql 复制代码
SET SESSION long_query_time = 2; -- 设置慢查询时间为2秒
2. 全局设置
这样所有会话都会受到影响:
sql 复制代码
SET GLOBAL long_query_time = 2; -- 设置慢查询时间为2秒
3. 检查当前设置
sql 复制代码
SHOW VARIABLES LIKE 'long_query_time';
-- 或者
select @@long_query_time;

以上的问题,MySQL 服务重启后 失效。

4. 配置文件配置

为了确保设置在服务器重启后仍然有效,可以在 MySQL 配置文件(如 my.cnf 或 my.ini)中添加以下行:

ini 复制代码
[mysqld]
slow_query_log = 1
slow_query_log_file = /path/to/your/slow-query.log
long_query_time = 2  # 设置慢查询时间为2秒
log_queries_not_using_indexes = 1  # 可选:记录未使用索引的查询
5. 重启MySQL服务

保存配置文件并重启MySQL服务以使更改生效。

6. 编写Python 触发邮件代码
python 复制代码
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 配置邮件发送信息
SMTP_SERVER = 'smtp.example.com'
SMTP_PORT = 587
SMTP_USER = '[email protected]'
SMTP_PASSWORD = 'your-email-password'
FROM_EMAIL = '[email protected]'
TO_EMAIL = '[email protected]'

# 慢查询日志文件路径
SLOW_QUERY_LOG_FILE = '/path/to/your/slow-query.log'

# 记录上次读取的位置
last_position = 0

def send_email(subject, body):
    msg = MIMEMultipart()
    msg['From'] = FROM_EMAIL
    msg['To'] = TO_EMAIL
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain'))
    
    with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
        server.starttls()
        server.login(SMTP_USER, SMTP_PASSWORD)
        server.sendmail(FROM_EMAIL, TO_EMAIL, msg.as_string())

def monitor_slow_query_log():
    global last_position
    with open(SLOW_QUERY_LOG_FILE, 'r') as file:
        file.seek(last_position)
        while True:
            line = file.readline()
            if not line:
                time.sleep(1)  # 等待1秒后再次检查
                continue
            last_position = file.tell()
            if 'Query_time:' in line:
                query_time = float(line.split('Query_time:')[1].split()[0])
                if query_time > 2:  # 假设慢查询时间阈值为2秒
                    slow_query = line.strip()
                    send_email('Slow Query Detected', f'Slow Query: {slow_query}')

if __name__ == '__main__':
    monitor_slow_query_log()

确保你已经安装了所需的Python库(如 smtplib 和 email),然后运行脚本:

7. 运行Python 代码
python 复制代码
python monitor_slow_query_log.py
8. 使用系统工具

如果你不想编写脚本,可以使用一些系统工具来监控文件变化并发送邮件。例如,使用 inotifywait 和 mail 命令:

安装 inotify-tools

在Linux上安装 inotify-tools:

shell 复制代码
sudo apt-get install inotify-tools
9. 创建 monitor_slow_query.sh脚本
shell 复制代码
#!/bin/bash

SLOW_QUERY_LOG_FILE="/path/to/your/slow-query.log"
SMTP_SERVER="smtp.example.com"
SMTP_PORT=587
SMTP_USER="[email protected]"
SMTP_PASSWORD="your-email-password"
FROM_EMAIL="[email protected]"
TO_EMAIL="[email protected]"

inotifywait -m -e modify "$SLOW_QUERY_LOG_FILE" |
while read path action file; do
    tail -n 1 "$SLOW_QUERY_LOG_FILE" | grep 'Query_time:' | while read line; do
        query_time=$(echo $line | awk '{print $2}' | cut -d':' -f2)
        if (( $(echo "$query_time > 2" | bc -l) )); then
            echo "Slow Query: $line" | mail -s "Slow Query Detected" -S smtp="$SMTP_SERVER:$SMTP_PORT" -S smtp-auth=login -S smtp-auth-user="$SMTP_USER" -S smtp-auth-password="$SMTP_PASSWORD" -S from="$FROM_EMAIL" "$TO_EMAIL"
        fi
    done
done

执行脚本

shell 复制代码
chmod +x monitor_slow_query.sh
./monitor_slow_query.sh
相关推荐
zhou1853 分钟前
MySQL保姆级安装教程(附资源包+5分钟极速配置+环境变量调试技巧)
java·python·mysql·php
野木香1 小时前
mysql8常用sql语句
数据库·sql·mysql
rylshe13141 小时前
spark sql基本操作
sql·spark
_extraordinary_2 小时前
MySQL 事务(二)
android·数据库·mysql
rylshe13142 小时前
在scala中sparkSQL连接mysql并添加新数据
开发语言·mysql·scala
fmdpenny2 小时前
SQL中联表的运用
数据库·sql
不剪发的Tony老师2 小时前
互联网SQL面试题:用户会话时长分析
数据库·sql
睡觉z2 小时前
Shell编程之正则表达式与文本处理器
数据库·mysql·正则表达式
Dreams_l3 小时前
MySQL初阶:查询进阶
数据库·mysql
鸿蒙布道师6 小时前
鸿蒙NEXT开发动画案例5
android·ios·华为·harmonyos·鸿蒙系统·arkui·huawei