prometheus之监控mysql

@lianst  December 31, 2019

软件环境

软件下载地址:https://prometheus.io/download/

  • server端:192.168.119.119:9090
  • mysqld_exporter:0.12.1
  • node_exporter:0.18.1

安装部署

[root@mysql01 opt]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@mysql01 opt]# wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz # node配置部署忽略
[root@mysql01 opt]# cd mysqld_exporter-0.12.1.linux-amd64

mysql授权

GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'monitor'@'localhost' identified by '123456';
flush privileges;

配置启动exporter

mysqld_exporter启动的时候需要读取mysql授权用户的一个配置文件,所以我们要先创建一下这个配置文件。

[root@mysql01 mysqld_exporter-0.12.1.linux-amd64]# vim .my.cnf
[client]
user=monitor
password=123456

尝试使用这个配置文件启动一下,mysqld_exporter默认会读取当前目录下的.my.cnf配置文件,看到能正常监听9104端口就可以了。

[root@mysql01 mysqld_exporter-0.12.1.linux-amd64]# ./mysqld_exporter 
INFO[0000] Starting mysqld_exporter (version=0.12.1, branch=HEAD, revision=48667bf7c3b438b5e93b259f3d17b70a7c9aff96)  source="mysqld_exporter.go:257"
INFO[0000] Build context (go=go1.12.7, user=root@0b3e56a7bc0a, date=20190729-12:35:58)  source="mysqld_exporter.go:258"
INFO[0000] Enabled scrapers:                             source="mysqld_exporter.go:269"
INFO[0000]  --collect.global_variables                   source="mysqld_exporter.go:273"
INFO[0000]  --collect.slave_status                       source="mysqld_exporter.go:273"
INFO[0000]  --collect.global_status                      source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.query_response_time    source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.innodb_cmp             source="mysqld_exporter.go:273"
INFO[0000]  --collect.info_schema.innodb_cmpmem          source="mysqld_exporter.go:273"
INFO[0000] Listening on :9104                            source="mysqld_exporter.go:283"

配置server端

进入server端所在服务器,修改prometheus.yml配置文件,添加一个job配置项,然后重新启动prometheus server端。

[root@devops prometheus-2.15.1.linux-amd64]# vim prometheus.yml
...
 - job_name: mysql
    static_configs:
      - targets: ['192.168.119.136:9104']
        labels:
          instance: pre-product_mysql_192.168.119.136
[root@devops prometheus-2.15.1.linux-amd64]# ./prometheus 
level=info ts=2019-12-30T22:34:59.983Z caller=main.go:294 msg="no time or size retention was set so using the default time retention" duration=15d
level=info ts=2019-12-30T22:34:59.983Z caller=main.go:330 msg="Starting Prometheus" version="(version=2.15.1, branch=HEAD, revision=8744510c6391d3ef46d8294a7e1f46e57407ab13)"
level=info ts=2019-12-30T22:34:59.983Z caller=main.go:331 build_context="(go=go1.13.5, user=root@4b1e33c71b9d, date=20191225-01:04:15)"
level=info ts=2019-12-30T22:34:59.983Z caller=main.go:332 host_details="(Linux 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 devops (none))"
level=info ts=2019-12-30T22:34:59.983Z caller=main.go:333 fd_limits="(soft=65536, hard=65536)"
level=info ts=2019-12-30T22:34:59.983Z caller=main.go:334 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2019-12-30T22:34:59.985Z caller=main.go:648 msg="Starting TSDB ..."
level=info ts=2019-12-30T22:34:59.985Z caller=web.go:506 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2019-12-30T22:34:59.985Z caller=repair.go:59 component=tsdb msg="found healthy block" mint=1577723118290 maxt=1577728800000 ulid=01DXC4A59PX6F6BJ5FBV2RP68H
level=info ts=2019-12-30T22:34:59.985Z caller=repair.go:59 component=tsdb msg="found healthy block" mint=1577728800000 maxt=1577736000000 ulid=01DXC9QKAYQXQGEM46XSCZ1XN6
level=info ts=2019-12-30T22:34:59.989Z caller=head.go:584 component=tsdb msg="replaying WAL, this may take awhile"
level=info ts=2019-12-30T22:34:59.996Z caller=head.go:608 component=tsdb msg="WAL checkpoint loaded"
level=info ts=2019-12-30T22:35:00.011Z caller=head.go:632 component=tsdb msg="WAL segment loaded" segment=4 maxSegment=8
level=info ts=2019-12-30T22:35:00.037Z caller=head.go:632 component=tsdb msg="WAL segment loaded" segment=5 maxSegment=8
level=info ts=2019-12-30T22:35:00.050Z caller=head.go:632 component=tsdb msg="WAL segment loaded" segment=6 maxSegment=8
level=info ts=2019-12-30T22:35:00.066Z caller=head.go:632 component=tsdb msg="WAL segment loaded" segment=7 maxSegment=8
level=info ts=2019-12-30T22:35:00.066Z caller=head.go:632 component=tsdb msg="WAL segment loaded" segment=8 maxSegment=8
level=info ts=2019-12-30T22:35:00.069Z caller=main.go:663 fs_type=XFS_SUPER_MAGIC
level=info ts=2019-12-30T22:35:00.069Z caller=main.go:664 msg="TSDB started"
level=info ts=2019-12-30T22:35:00.069Z caller=main.go:734 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2019-12-30T22:35:00.109Z caller=main.go:762 msg="Completed loading of configuration file" filename=prometheus.yml
level=info ts=2019-12-30T22:35:00.109Z caller=main.go:617 msg="Server is ready to receive web requests."

查看结果

请输入图片描述

配置Grafana

grafana官方市场已经为我们提供了mysql的模板,我们只需要import进来就可以使用了。
模板地址:https://grafana.com/grafana/dashboards/7362

请输入图片描述

进程守护

进程守护我们可以使用systemsupervisor或者nohup都可以,选择适合自己、自己熟悉的就可以了,我这里贴一个system的示例。

[root@mysql01 mysqld_exporter-0.12.1.linux-amd64]# vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target

[Service]
Type=simple
ExecStart=/opt/mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter --config.my-cnf=/opt/mysqld_exporter-0.12.1.linux-amd64/.my.cnf
Restart=on-failure

[Install]
WantedBy=multi-user.target

[root@mysql01 opt]# systemctl daemon-reload # 重新加载一下配置
[root@mysql01 opt]# systemctl start mysqld_exporter
[root@mysql01 opt]# systemctl status mysqld_exporter
[root@mysql01 opt]# systemctl enable mysqld_exporter
[root@mysql01 opt]#  ss -anptu | grep 9104
tcp    LISTEN     0      128      :::9104                 :::*                   users:(("mysqld_exporter",pid=9280,fd=3))
tcp    ESTAB      0      0        ::ffff:192.168.119.136:9104                 ::ffff:192.168.119.119:33944               users:(("mysqld_exporter",pid=9280,fd=5))

添加新评论