4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GrafanaをMySQLと連携してデータの永続化

Posted at

概要

  • データ監視のためのgrafanaについて
  • 業務で使用するときはkubernetesを使用してgrafanaを使用しているが、デフォルトの設定だとデータが永続化されない(podが再起動するとデータが消える)
  • grafanaがデータベースとしてMySQLを使えるというのを見つけたから試してみる!

環境

  • 今回は個人のMacで
  • どんな設定をしたらMySQLと連携できるのかだけ確認する

準備

grafanaとMySQLのインストール

brew install grafana
brew install mysql

確認

brew services list

起動と動作確認

  • MySQL
brew services start mysql
mysql -u root
  • grafana
brew services start grafana
# http://localhost:3000/にアクセス(デフォルトの管理者ユーザはadmin/admin)

mysqlの設定

  • databaseを作成する
  • grafanaユーザを作成する
    • mysql8ではmysql_native_passwordを指定する必要がある
    • その他のpluginではgrafanaが対応していない
mysql -u root

create database grafana;

create user 'grafana'@'localhost' identified with mysql_native_password by 'grafana';

grant all on grafana.* to 'grafana'@'localhost';

いざ連携!

ここからは公式ページを参考に。。。

設定ファイルは/usr/local/etc/grafana/grafana.iniこれ

databaseの設定をこんな感じに修正

grafana.ini
#################################### Database ####################################
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url properties.

# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql
host = localhost:3306
name = grafana
user = grafana
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = grafana

grafanaをリスタート!

brew services restart grafana

動いた!!!

DBを確認

mysql -u grafana -p grafana

mysql> show tables;
+--------------------------+
| Tables_in_grafana        |
+--------------------------+
| alert                    |
| alert_notification       |
| alert_notification_state |
| annotation               |
| annotation_tag           |
| api_key                  |
| cache_data               |
| dashboard                |
| dashboard_acl            |
| dashboard_provisioning   |
| dashboard_snapshot       |
| dashboard_tag            |
| dashboard_version        |
| data_source              |
| login_attempt            |
| migration_log            |
| org                      |
| org_user                 |
| playlist                 |
| playlist_item            |
| plugin_setting           |
| preferences              |
| quota                    |
| sample                   |
| server_lock              |
| session                  |
| star                     |
| tag                      |
| team                     |
| team_member              |
| temp_user                |
| test_data                |
| user                     |
| user_auth                |
| user_auth_token          |
+--------------------------+
35 rows in set (0.00 sec)

たくさんテーブル作られてる!

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?