在运维工作中,Ubuntu数据库监控是保障业务连续性的关键环节。一旦数据库出现异常(如连接数激增、CPU占用过高、磁盘空间不足等),若不能及时发现和处理,可能导致服务中断甚至数据丢失。本文将从零开始,教你如何在 Ubuntu 系统上为 MySQL 数据库配置一套简单但高效的监控告警系统,即使你是 Linux 小白也能轻松上手。

在开始之前,请确保你已满足以下条件:
我们将使用开源监控生态中最流行的组合:Prometheus(时序数据库+告警引擎)、Node Exporter(采集系统指标)、MySQL Exporter(采集数据库指标)。这套方案轻量、高效,非常适合中小规模部署。
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gztar xvfz node_exporter-1.7.0.linux-amd64.tar.gzcd node_exporter-1.7.0.linux-amd64sudo cp node_exporter /usr/local/bin/创建 systemd 服务:
sudo tee /etc/systemd/system/node_exporter.service <启用并启动服务:
sudo useradd -rs /bin/false node_exportersudo systemctl daemon-reloadsudo systemctl enable node_exportersudo systemctl start node_exporter首先在 MySQL 中创建一个只读监控用户:
mysql -u root -p-- 在 MySQL 命令行中执行CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'strongpassword';GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';FLUSH PRIVILEGES;EXIT;下载并安装 MySQL Exporter:
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.0/mysqld_exporter-0.15.0.linux-amd64.tar.gztar xvfz mysqld_exporter-0.15.0.linux-amd64.tar.gzcd mysqld_exporter-0.15.0.linux-amd64sudo cp mysqld_exporter /usr/local/bin/创建配置文件 .my.cnf(位于 exporter 用户家目录或 /etc/mysqld_exporter/):
sudo mkdir -p /etc/mysqld_exportersudo tee /etc/mysqld_exporter/.my.cnf <创建 systemd 服务:
sudo tee /etc/systemd/system/mysqld_exporter.service <启动服务:
sudo useradd -rs /bin/false mysqld_exportersudo systemctl daemon-reloadsudo systemctl enable mysqld_exportersudo systemctl start mysqld_exporterPrometheus 负责拉取指标并触发告警。
sudo apt updatesudo apt install -y prometheus编辑配置文件 /etc/prometheus/prometheus.yml:
global: scrape_interval: 15sscrape_configs: - job_name: 'node' static_configs: - targets: ['localhost:9100'] - job_name: 'mysql' static_configs: - targets: ['localhost:9104']重启 Prometheus:
sudo systemctl restart prometheus在 /etc/prometheus/ 目录下创建告警规则文件 alert.rules.yml:
groups:- name: mysql-alerts rules: - alert: MySQLDown expr: up{job="mysql"} == 0 for: 1m labels: severity: critical annotations: summary: "MySQL instance down" description: "MySQL has been down for more than 1 minute." - alert: HighCPUUsage expr: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85 for: 5m labels: severity: warning annotations: summary: "High CPU usage detected" description: "CPU usage is above 85% for 5 minutes." - alert: LowDiskSpace expr: (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100 < 10 for: 10m labels: severity: critical annotations: summary: "Low disk space on root partition" description: "Available disk space is less than 10%."然后在 prometheus.yml 中引用该规则文件:
rule_files: - "alert.rules.yml"重启 Prometheus 使配置生效。
Prometheus 本身不发送通知,需配合 Alertmanager。安装并配置 Alertmanager:
sudo apt install -y alertmanager编辑 /etc/alertmanager/alertmanager.yml(以 Gmail 为例):
global: smtp_smarthost: 'smtp.gmail.com:587' smtp_from: 'your-email@gmail.com' smtp_auth_username: 'your-email@gmail.com' smtp_auth_password: 'your-app-password'route: receiver: 'email-notifications'receivers:- name: 'email-notifications' email_configs: - to: 'admin@example.com'最后,在 Prometheus 配置中添加 Alertmanager 地址(默认 9093 端口):
alerting: alertmanagers: - static_configs: - targets: ['localhost:9093']重启服务:
sudo systemctl restart alertmanager prometheus打开浏览器访问:
http://你的服务器IP:9090http://你的服务器IP:9093在 Prometheus 的 “Alerts” 页面,你可以看到所有激活的告警规则。模拟一次故障(如停止 MySQL 服务),几分钟后应收到邮件通知。
通过以上步骤,你已经成功搭建了一套完整的 Ubuntu数据库监控 和 MySQL监控告警 系统。这套方案不仅能实现 系统性能监控,还能对数据库健康状态进行实时 数据库健康检查,极大提升了运维效率和系统稳定性。
建议定期优化告警阈值,避免“告警疲劳”。后续你还可以集成 Grafana 实现可视化面板,让监控数据一目了然。
本文由主机测评网于2025-12-05发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025123261.html