在linux节点上安装mysqld exporter随记

来自三线的随记

MySQL Server Prometheus Exporter

binary download and official documentation:

https://github.com/prometheus/mysqld_exporter


Prometheus exporter for MySQL server metrics.

下載mysql exporter 二進制文件壓縮包

在節點上執行命令下載

wget "https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz" 


解壓壓縮包

[root@master mysql]# ls
mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@master mysql]# tar -xzf mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@master mysql]# ls
mysqld_exporter-0.12.1.linux-amd64  mysqld_exporter-0.12.1.linux-amd64.tar.gz


將 mysqld_exporter-0.12.1.linux-amd64 目錄下的 mysqld_exporter 二進制文件複製到 /usr/local/bin 路徑下

注意需要給予該二進制文件nobody用戶可執行權限

[root@master mysql]# cd mysqld_exporter-0.12.1.linux-amd64/
[root@master mysqld_exporter-0.12.1.linux-amd64]# ls
LICENSE  mysqld_exporter  NOTICE
[root@master mysqld_exporter-0.12.1.linux-amd64]# cp mysqld_exporter /usr/local/bin/
[root@master mysqld_exporter-0.12.1.linux-amd64]# ls -l /usr/local/bin/mysqld_exporter
-rwxr-xr-x. 1 root root 14813452 Oct 22 15:05 /usr/local/bin/mysqld_exporter


配置mysqld-exporter 的 mysql 認證用戶帳號密碼

執行mysql command 新建用戶並賦權(command中的相關認證參數請根據實際情況修改)

注意:建議為用戶設置最大連接限制,以避免在服務器重負載下監視碎片導致服務器過載,並非所有MySQL / MariaDB版本都支持此功能。 例如,MariaDB 10.1(由Ubuntu 18.04提供)不支持此功能。

CREATE USER 'exporter'@'127.0.0.1' IDENTIFIED BY 'dangerous.pwd' WITH MAX_USER_CONNECTIONS 5;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'127.0.0.1';


創建systemd service文件

根據實際情況修改其中的Environment配置值

如果上一步驟沒有修改創建用戶命令中的認證參數

則下文可不做變更

直接複製下文全部文字命令並在節點上執行即可

cat <<EOF > /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target 

[Service]
Environment=DATA_SOURCE_NAME=exporter:dangerous.pwd@(127.0.0.1:3306)/
User=nobody
Group=nobody
Type=simple
ExecStart=/usr/local/bin/mysqld_exporter\\
          --web.listen-address=:9104

[Install]
WantedBy=multi-user.target
EOF


重載系統 systemd 配置

執行命令 systemctl daemon-reload

啟動服務並且設置服務自啟

執行命令 systemctl enable --now mysqld_exporter

[root@master mysqld_exporter]# systemctl enable --now mysqld_exporter
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld_exporter.service to /etc/systemd/system/mysqld_exporter.service.


查看服務運行狀態

執行命令systemctl status mysqld_exporter

能夠得到類似下文的回顯結果

主要看Active屬性值為active(running)

以及日誌中提示Listening on :9104就好了

● mysqld_exporter.service - mysqld_exporter
   Loaded: loaded (/etc/systemd/system/mysqld_exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-10-22 16:47:41 CST; 1min 27s ago
 Main PID: 31810 (mysqld_exporter)
   CGroup: /system.slice/mysqld_exporter.service
           └─31810 /usr/local/bin/mysqld_exporter --web.listen-address=:9104

Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg="Starting mysqld_exporter (version=0.12.1, branch=HEAD, revision=48667bf7c3b438b5e93b259f3d17b70...porter.go:257"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg="Build context (go=go1.12.7, user=root@0b3e56a7bc0a, date=20190729-12:35:58)" source="mysqld_exporter.go:258"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg="Enabled scrapers:" source="mysqld_exporter.go:269"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg=" --collect.global_status" source="mysqld_exporter.go:273"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg=" --collect.global_variables" source="mysqld_exporter.go:273"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg=" --collect.slave_status" source="mysqld_exporter.go:273"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg=" --collect.info_schema.innodb_cmp" source="mysqld_exporter.go:273"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg=" --collect.info_schema.innodb_cmpmem" source="mysqld_exporter.go:273"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg=" --collect.info_schema.query_response_time" source="mysqld_exporter.go:273"
Oct 22 16:47:41 master mysqld_exporter[31810]: time="2020-10-22T16:47:41+08:00" level=info msg="Listening on :9104" source="mysqld_exporter.go:283"


測試接口

在節點上使用命令curl -s {{節點IP}}:9104/metrics | grep mysql_up訪問接口

能夠得到類似下文結果(mysql_up 值非0)

[root@master ~]# curl  -s 192.168.150.166:9104/metrics | grep mysql_up
# HELP mysql_up Whether the MySQL server is up.
# TYPE mysql_up gauge
mysql_up 1

或直接使用瀏覽器訪問{{節點IP}}:9104/metrics能夠看到相關 mysqld 實例指標信息則為正常