仅适用 CentOS 7

mysqld_export 安装

prometheus 监听 mysql 服务

一键安装

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

验证

1
2
3
4
5
# 验证进程是否启动
netstat -nplt |grep mysqld_exporter

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

配置nginx安全访问

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

参考:nginx 添加权限验证

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

nginx 的vhost配置 mysqld_exporter.conf

1
2
3
4
5
6
7
8
9
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

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

    1
    2
    3
    4
    5
    6
    7
    8
    
    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 添加权限验证