🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now β†’
Menu

Categories

MySQL/MariaDB Health Monitoring with Python: Connections, Slow Queries, Replication (Free Tool)

MySQL/MariaDB Health Monitoring with Python: Connections, Slow Queries, Replication (Free Tool)

Why MySQL Health Monitoring Matters

MySQL and MariaDB power the majority of web applications. Without proper monitoring, you will not notice connection pool exhaustion, slow query accumulation, or replication lag until your application starts timing out.

dargslan-mysql-health is a free Python CLI tool that gives you instant visibility into your MySQL/MariaDB server health β€” connection usage, slow queries, replication status, and database sizes β€” all without external dependencies.

Install dargslan-mysql-health

pip install dargslan-mysql-health

Requires the mysql CLI client to be installed on your system (included in mysql-client or mariadb-client packages).

Quick Health Report

dargslan-mysql report

Displays server version, uptime, connection statistics, slow query info, and any detected issues.

Health Checks

Connection Monitoring

dargslan-mysql connections

Shows current connections, max connections, max used connections, and usage percentage. Alerts when usage exceeds 80% (warning) or 95% (critical).

Slow Query Analysis

dargslan-mysql slow

Checks if the slow query log is enabled, the long_query_time threshold, and total slow query count. Alerts when slow queries exceed 1000.

Database Sizes

dargslan-mysql databases

Lists all databases with their size in MB, sorted by size descending. Useful for identifying which databases are consuming the most disk space.

Replication Status

dargslan-mysql replication

Checks if replication is configured, IO/SQL thread status, seconds behind master, and last error. Critical alerts for broken replication.

Custom Connection

# Connect to remote server
dargslan-mysql report -H 10.0.0.5 -P 3306 -u admin -p secretpass

# Or use environment variables
export MYSQL_USER=admin
export MYSQL_PASSWORD=secretpass
dargslan-mysql report

Python API

from dargslan_mysql_health import MySQLHealth

mh = MySQLHealth(host="localhost", user="root")

# Full audit
issues = mh.audit()
for issue in issues:
    print(f"[{issue['severity'].upper()}] {issue['message']}")

# Connection status
conn = mh.connection_status()
print(f"Connections: {conn['current_connections']}/{conn['max_connections']} ({conn['usage_percent']}%)")

# Database sizes
for db in mh.database_sizes():
    print(f"{db['database']}: {db['size_mb']} MB")

# Server info
info = mh.server_info()
print(f"MySQL {info['version']}, uptime: {info['uptime_human']}")

# Replication
repl = mh.replication_status()
if repl['configured']:
    print(f"IO: {repl['io_running']}, SQL: {repl['sql_running']}")
    print(f"Lag: {repl['seconds_behind']}s")

Severity Levels

CheckSeverityThreshold
Connection usageWARNING>80%
Connection usageCRITICAL>95%
Slow query log offINFOAlways
High slow queriesWARNING>1000
Replication brokenCRITICALIO/SQL not running
Replication lagWARNING>60 seconds

Monitoring Automation

# Add to crontab for hourly checks
0 * * * * dargslan-mysql issues >> /var/log/mysql-health.log 2>&1

# Or use in Python monitoring script
import json
mh = MySQLHealth()
issues = mh.audit()
if any(i["severity"] == "critical" for i in issues):
    # Send alert (email, Slack, PagerDuty, etc.)
    send_alert("MySQL CRITICAL", json.dumps(issues))

Download the Free Cheat Sheet

Get the complete MySQL Health Checker Cheat Sheet PDF with all CLI commands and Python API reference.

Master Database Administration

Deepen your MySQL/MariaDB skills with our database administration eBooks. Check out all 20+ free Python CLI tools for Linux sysadmins.

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.