目次
はじめに
こんにちは。ネットワークエンジニアの「だいまる」です。
今回は、MariaDB(MySQL)の状態監視を行うための「prometheus-mysqld-exporter」をインストールし、grafanaのダッシュボードを作成した方法をまとめていきます。
MySQL Exporterによる監視構築
Step1:インストール
以下コマンドでインストールを実施します。
sudo apt install prometheus-mysqld-exporter
Step2:MySQL exporterの設定
インストールのみでは、MySQL exporterの起動は失敗すると思うので、exporter向けの設定ファイルを読み込ませるための設定が必要です。
その設定は「/etc/default/prometheus-mysqld-exporter」のARGSにどの設定ファイルを読み込ませるかを記載します。

今回は「my_exporter.cnf」を新たに作ります!
### Set the command-line arguments to pass to the server.
#
# See below for a full list of available arguments.
#
# Due to shell scaping, to pass backslashes for regexes, you need to double
# them (\\d for \d). If running under systemd, you need to double them again
# (\\\\d to mean \d), and escape newlines too.
ARGS="--config.my-cnf /etc/mysql/my_exporter.cnf"
Step3:my.cnfの編集
次にStep2で指定した「/etc/mysql/my_exporter.cnf」ファイルにMySQL Exporterがログインするための情報を記載します。
[client]
user=exporter
password=Daimaru_Blog
host=127.0.0.1
Step4:MariaDB(MySQL)のユーザ作成
Step3で指定した「user」と「password」のユーザを作成します。
MariaDB [(none)]> create user 'exporter'@'localhost' identified by 'Daimaru_Blog';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> GRANT PROCESS,REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]>
ユーザ作成後は、一度MySQL Exporterの再起動を実施してください。

再起動後、問題なく起動できると思います!
Step5:prometheusの設定
MySQL Exporter側の準備を終えたら、prometheus側の設定をし再起動してあげます。
- job_name: "MYSQL_EXPORTER"
static_configs:
- targets: ["X.X.X.X:9104"]
Step6:FW(ACL)の設定追加
ファイアウォールの設定を入れている人はここでPermitしてあげましょう。
Step7:Grafana Dashboardの設定
今回は、以下のダッシュボードをインポートして利用してみました。
https://grafana.com/grafana/dashboards/14031-mysql-dashboard
最後に
今回は、MariaDB(MySQL)を監視するための「MySQL Exporter」の構築とGrafanaのダッシュボードについてまとめてみました。
