仅适用 CentOS 7

mysqld_export 安装

prometheus 监听 mysql 服务

一键安装

curl -sSL http://s1.sgfoot.com/sh/mysql_exporter.sh | sudo bash

验证

# 验证进程是否启动
netstat -nplt |grep mysqld_exporter

# 验证是否可以获取 metrics
curl http://localhost:9104/metrics

配置nginx安全访问

如果涉及到外网访问则需要配置密码访问

参考:nginx 添加权限验证

htpasswd -bc /etc/nginx/htpasswd.users sgfoot sgfoot.pass
# sgfoot 是帐号名
# sgfoot.pass 是密码

nginx 的vhost配置 mysqld_exporter.conf

server {
    listen 80;
    server_name mysqld_exporter.io;
    location / {
    	auth_basic           "Prometheus";
        auth_basic_user_file /etc/nginx/htpasswd.users;# 验证文件
 	    proxy_pass http://127.0.0.1:9104;
   }
}

添加 prometheus 节点

  1. 配置 host

    vim /etc/hosts
    127.0.0.1 mysqld_exporter.io
    
  2. vim /data/local/prometheus/prometheus.yml 在 node_exporter.targets 添加 host:port

    scrape_configs:
        - job_name: 'mysqld_exporter' # mysqld_exporter 数据源。
            scrape_interval: 10s
            static_configs:
              - targets: ['mysqld_exporter.io:9104']
            basic_auth:
              username: sgfoot
              password: sgfoot.pass
    
  3. 重启 systemctl restart prometheus

参考

promethues监听MySQL

nginx 添加权限验证