在现代IT运维中,Ubuntu数据库监控是保障服务稳定性的关键环节。无论是个人开发者还是企业运维人员,都需要一套可靠、易用的监控方案来实时掌握数据库运行状态。本文将从零开始,教你如何在Ubuntu系统上搭建轻量级但功能强大的数据库监控体系,即使你是Linux小白也能轻松上手。
数据库是大多数Web应用的核心组件。一旦出现性能瓶颈、连接数爆满或磁盘空间不足等问题,可能导致整个服务瘫痪。通过MySQL性能监控,你可以提前发现异常,及时优化配置,避免业务中断。

在安装专业监控工具前,我们可以先利用Ubuntu自带命令快速查看系统和数据库负载情况。
htop # 实时查看进程和资源占用(需安装:sudo apt install htop)df -h # 查看磁盘空间free -h # 查看内存使用情况iostat -x 2 # 查看磁盘I/O(需安装 sysstat 包)如果你已安装MySQL,可通过以下命令查看当前连接数、查询速率等:
mysql -u root -p -e "SHOW STATUS LIKE 'Threads_connected';"mysql -u root -p -e "SHOW GLOBAL STATUS LIKE 'Questions';"mysqladmin -u root -p status为了实现长期、可视化的Prometheus数据库监控,我们推荐使用 Prometheus(数据采集) + Grafana(可视化展示)组合。这套方案是当前最流行的开源监控栈之一。
MySQL Exporter 是一个将MySQL指标暴露给Prometheus的小程序。
# 下载并解压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-amd64# 创建MySQL监控用户(在MySQL中执行)CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'strongpassword';GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';FLUSH PRIVILEGES;# 启动 exporter(替换密码)./mysqld_exporter \ --config.my-cnf="[client]user=exporterpassword=strongpassword" \ --web.listen-address=:9104# 安装 Prometheussudo apt updatesudo apt install prometheus -y# 编辑配置文件sudo nano /etc/prometheus/prometheus.yml在配置文件末尾添加以下 job:
scrape_configs: - job_name: 'mysql' static_configs: - targets: ['localhost:9104']重启 Prometheus 服务:
sudo systemctl restart prometheuswget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.listsudo apt updatesudo apt install grafana -ysudo systemctl enable grafana-serversudo systemctl start grafana-server访问 http://你的服务器IP:3000,默认账号密码为 admin/admin。添加 Prometheus 为数据源(URL: http://localhost:9090),然后导入官方MySQL仪表盘(ID: 7362)即可看到漂亮的监控图表。
对于喜欢终端操作的用户,可以尝试 mytop —— 类似 top 命令的MySQL实时监控工具。
sudo apt install mytop -ymytop -u root -p它会实时显示当前SQL查询、连接数、查询速率等信息,非常适合快速排查慢查询问题。
通过本文介绍的三种方式,你可以根据自身需求选择合适的系统资源监控工具。对于生产环境,强烈建议部署 Prometheus + Grafana 方案,它不仅能监控MySQL,还能扩展到Redis、Nginx、主机指标等,形成完整的可观测性体系。
记住,监控不是目的,而是保障服务高可用的手段。定期查看告警、分析趋势,才能真正发挥Ubuntu数据库监控的价值。
希望这篇教程对你有所帮助!如有疑问,欢迎在评论区交流。
本文由主机测评网于2025-12-05发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025123312.html