Docker compose 部署 MySQL Exporter
在上一篇文章:Docker Compose部署Prometheus与Grafana 中已经成功部署了 prometheus和grafana,这一篇文章将讲述如何监控mysql
要实现对MySQL的监控并将其数据展示在Grafana上,你需要先部署一个能够从MySQL服务器收集指标的Exporter,然后在Prometheus中配置该Exporter作为数据抓取目标,并在Grafana中创建相应的数据源和仪表板。这里我们使用prometheus-mysql-exporter
作为MySQL的Exporter。
步骤1:部署MySQL Exporter
首先,你需要在Docker Compose配置文件中添加MySQL Exporter的服务定义。编辑docker-compose.yml
,加入以下内容:
mysql_exporter:
image: prom/mysqld-exporter:latest
container_name: mysql_exporter
ports:
- "9104:9104"
command:
- '--collect.engine_innodb_status'
- '--collect.info_schema.processlist'
- '--collect.info_schema.tables'
- '--collect.info_schema.query_response_time'
- '--collect.perf_schema.events_statements'
- '--collect.perf_schema.file_events'
- '--collect.perf_schema.index_iops'
- '--collect.performance_schema'
- '--collect.global_status'
- '--collect.slave_status'
- '--ignore-tables="^(information_schema|performance_schema|mysql)"'
environment:
- DATA_SOURCE_NAME=root:your_mysql_password@(mysql_host:mysql_port)/
restart: always
请注意替换your_mysql_password
、mysql_host
和mysql_port
为你实际的MySQL数据库的凭据和连接信息。如果你的MySQL容器也在这个Docker Compose文件中定义,可以使用服务名称(如db
)作为mysql_host
。
步骤2:更新Prometheus配置
你需要在prometheus.yml
中添加一个新的job来抓取MySQL Exporter暴露的指标。在scrape_configs
部分添加如下内容:
- job_name: 'mysql_exporter'
static_configs:
- targets: ['mysql_exporter:9104']
步骤3:重启服务
保存更改后,重新启动Docker Compose服务以应用新的配置:
docker-compose down && docker-compose up -d
步骤4:配置Grafana数据源和仪表板
确保Prometheus中已经成功抓取到了MySQL Exporter的数据,然后在Grafana中配置MySQL数据源:
- 在Grafana界面选择左侧菜单的“Data Sources”,点击“Add data source”。
- 选择“Prometheus”类型,输入名称(如MySQL Monitoring),并在URL栏填入Prometheus的地址(默认为
http://localhost:9090
)。 - 保存数据源。
接下来,你可以导入或创建针对MySQL监控的Grafana仪表板。Grafana官网的Dashboard Marketplace上有许多现成的MySQL监控面板可供选择。下载.json文件后,在Grafana中导入即可。
MySQL监控的Grafana仪表板建议使用percona公司的 https://grafana.com/grafana/dashboards/7362
步骤5:查看和分析监控数据
导入仪表板后,你就可以在Grafana界面上查看MySQL的各项性能指标,如查询性能、连接数、表锁情况等,从而帮助你更好地管理和优化MySQL数据库。
注意事项
- 确保MySQL服务器允许远程连接或Exporter容器能够直接访问MySQL容器(如果两者在同一网络中)。
- 根据你的MySQL版本和安全策略调整MySQL Exporter的收集参数。
- 考虑到安全性,不要在生产环境中明文存放数据库密码,可以使用Docker Secrets或环境变量等方式管理敏感信息。
通过上述步骤,你就成功地将MySQL监控集成到了你的Prometheus+Grafana监控系统中。