标签搜索

数据库连接问题排查脚本

mrui
2025-09-11 / 0 评论 / 3 阅读 / 正在检测是否收录...
#!/bin/bash
# 数据库连接问题排查脚本

echo "=== 数据库连接分析 ==="

# 分析应用日志中的数据库错误
echo "数据库连接错误统计:"
grep -i "database\|mysql\|connection" /var/log/myapp/error.log | \
grep -E "(timeout|refused|failed|error)" | \
sed 's/.*\[\([0-9-]*\).*/\1/' | \
sort | uniq -c | \
awk '{printf "%s: %d次错误\n", $2, $1}'

# 分析慢查询日志
echo "慢查询TOP 10:"
if [ -f /var/log/mysql/slow.log ]; then
    grep "Query_time" /var/log/mysql/slow.log | \
    awk '{print $3}' | \
    sort -nr | head -10 | \
    awk '{printf "查询时间: %.2f秒\n", $1}'
fi

# 检查连接池状态
echo "当前数据库连接数:"
mysql -e "SHOW STATUS LIKE 'Threads_connected';" 2>/dev/null | \
awk 'NR==2 {print "活跃连接:", $2}'
0

评论 (0)

取消