Loading... ## MySQL 创建用户及赋权 ```sql create user 'exporter'@'%' identified by '<密码>' with max_user_connections 3; select user from mysql.user; grant process,replication client, select on *.* to 'exporter'@'%'; ``` ## MySQLD Exporter 安装 从[官网](https://prometheus.io/download/)下载对应的 MySQLD Exporter 软件包 ```bash wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz tar zxvf mysqld_exporter-0.15.1.linux-amd64.tar.gz mv mysqld_exporter-0.15.1.linux-amd64 /usr/local/mysqld_exporter ``` 复制证书及密钥到 MySQLD Exporter 目录 `/usr/local/mysqld_exporter` 创建 MySQLD Exporter 的配置文件 `vim /usr/local/mysqld_exporter/config.yaml` ```yaml tls_server_config: cert_file: node_exporter.crt key_file: node_exporter.key # 增加 BasicAuth 验证 basic_auth_users: # 当前设置的用户名为 asdasfdassad,可以设置多个 asdasfdassad: $2y$12$n2rueJIEKRfWcfTKyHUHneVxXbwr/qv2/dD.L.N6CHSXWNHbADoDG ``` 创建连接数据库配置文件 `/usr/local/mysqld_exporter/my.cnf` ```ini [client] user=用户名 password=密码 ``` ### 通过服务启动 创建服务 `vim /etc/systemd/system/mysqld_exporter.service`,要根据实际情况修改启动参数 ```bash [Unit] Description=Prometheus mysqld exporter Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/mysql_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/my.cnf --web.config.file=/usr/local/mysqld_exporter/config.yaml [Install] WantedBy=multi-user.target ``` 创建用户并赋权 ```bash useradd prometheus -M -s /sbin/nologin chown prometheus:prometheus /usr/local/mysqld_exporter -R chmod 755 /usr/local/mysqld_exporter -R ``` 设置开机启动并启动服务 ```bash systemctl enable mysqld_exporter service mysqld_exporter start service mysqld_exporter status ``` ### 增加防火墙规则 MySQLD Exporter 默认使用 `9104` 端口,防火墙上需要放行这个端口(不建议运行公网直接访问),建议只允许 Prometheus 服务端IP访问 ``` iptables -A INPUT -s 10.20.30.110 -p tcp --dport 9104 -j ACCEPT -m comment --comment "prometheus-mysqld-exporter" iptables-save >/etc/iptables/rules.v4 ``` ## 在 Prometheus 服务端增加 Job 修改配置文件 `vim /usr/local/prometheus/prometheus.yml`,在 `scrape_configs` 中增加一个 `job`,例如: ```yaml scrape_configs: # mysqld job - job_name: "mysqld" scheme: https tls_config: ca_file: node_exporter.crt server_name: "qwerto.local" insecure_skip_verify: true basic_auth: username: sadsdasfasdas password: sadafdgareteqwrsadeqwfvsdfafdsf static_configs: - targets: ["MySQLD Exporter 服务器1 IP:9104"] ``` 重载 Prometheus 后生效 `service prometheus reload` 最后修改:2024 年 03 月 05 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏