Grafana基于MySQL作为数据存储实战案例

                                              作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.安装grafana

1.安装grafana依赖包

apt-get install -y adduser libfontconfig1 musl

2.下载grafana软件包

wget https://dl.grafana.com/enterprise/release/grafana-enterprise_9.5.21_amd64.deb

3.本地安装grafana

dpkg -i grafana-enterprise_9.5.21_amd64.deb

二.Grafana主要参数说明

[root@prometheus-server31 ~]# vim /etc/grafana/grafana.ini 
...
[paths]
data = /var/lib/grafana  
	# 指定Grafana的数据存储路径(包含会话,用户,配置信息),如果使用了sqlite3开发数据库,则数据也是会以本地文件的方式存储。

temp_data_lifetime = 24h 
	# 临时文件存储的时间周期。

logs = /var/log/grafana
	# 指定日志的存储路径

plugins = /var/lib/grafana/plugins
	# 指定插件的存储目录。
	
provisioning = conf/provisioning
	# 包含grafana将在启动和运行时应用的配置文件的文件夹。


[server]
protocol = http
	# Grafana使用的协议,支持的协议有: http, https, h2, socket
	
http_addr =
	# 绑定服务器的IP地址,如果为空,默认绑定所有的网络接口。
	
http_port = 3000
	# 指定Grafana监听的端口。
	
[database]
type = sqlite3
	# 定义数据的存储类型。支持: "mysql", "postgres" or "sqlite3",
	
host = 127.0.0.1:3306
	# 指定数据库的主机IP地址及端口。

name = grafana
	# 指定数据库的用户名。
	
password =
	# 指定数据库的密码。

ssl_mode = disable
	# 使用启用了SSL,比如MySQL使用的自建证书,则建议跳过证书校验。有效值为: "true", "false", or "skip-verify"
	
	
[security]
disable_initial_admin_creation = false
	# 首次启动grafana时禁用创建管理员用户

admin_user = admin
	# 启动时会创建的用户名。
	
admin_password = admin
	# 指定管理员用户的密码。
	
admin_email = admin@localhost
	# 默认管理员的邮箱地址。
	
[dashboards]

versions_to_keep = 20
	# 默认保留最近Dashboard的20次修改记录。
	
min_refresh_interval = 5s
	# 刷新Dashboard最小间隔时间为5s。
	
[users]
allow_sign_up = true
	# 是否运行用户注册。
	
allow_org_create = true
	# 允许非管理员用户创建组织。
	
verify_email_enabled = false
	# 注册用户时是否需要邮箱验证。
	
default_theme = dark 
	# 默认的主题。
	
default_language = en-US
	# 默认使用的语言。


[auth]
disable_login = true    
	# 禁止用户登录。
	
[auth.anonymous]
enabled = false
	# 是否启用匿名用户登录。

三.Grafana基于MySQL作为数据存储实战案例

1 安装docker环境

略,见视频。

2 启动mysql服务

[root@node-exporter43 ~]# docker container rm -f `docker container ps -qa`
[root@node-exporter43 ~]# 
[root@node-exporter43 ~]# docker run -d --name mysql-server --restart always --network host -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -e MYSQL_DATABASE=prometheus -e MYSQL_USER=linux97  -e MYSQL_PASSWORD=oldboyedu mysql:8.0.36-oracle --character-set-server=utf8 --collation-server=utf8_bin --default-authentication-plugin=mysql_native_password
0f7d832176e5bf5445b5fcf0db42210fc20c416dbc62cf7021affdd724f2835e
[root@node-exporter43 ~]# 
[root@node-exporter43 ~]# docker ps -l
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS     NAMES
0f7d832176e5   mysql:8.0.36-oracle   "docker-entrypoint.s…"   3 seconds ago   Up 2 seconds             mysql-server
[root@node-exporter43 ~]# 
[root@node-exporter43 ~]# ss -ntl | grep 3306
LISTEN 0      151                *:3306             *:*          
LISTEN 0      70                 *:33060            *:*          
[root@node-exporter43 ~]# 

3.修改Grafana的配置文件

[root@prometheus-server31 ~]# vim /etc/grafana/grafana.ini 
...
[database]
...
type = mysql
host = 10.0.0.43:3306
name = prometheus
user = linux97
password = oldboyedu          

4.启动grafna

[root@prometheus-server31 ~]# systemctl restart grafana-server.service 
[root@prometheus-server31 ~]# 
[root@prometheus-server31 ~]# ss -ntl | grep 3000
LISTEN 0      4096               *:3000            *:*          
[root@prometheus-server31 ~]# 

5.登录Grafana的webUI【注意,之前的数据全部都丢失了,因为之前使用的是sqllit3,现在用的是MySQL。】

http://10.0.0.31:3000/

初始的用户名和密码为: admin

6.验证数据是否写入成功

[root@node-exporter43 ~]# docker exec -it mysql-server mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.36 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| prometheus         |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> 
mysql> USE prometheus
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 
mysql> SHOW TABLES;
+-------------------------------+
| Tables_in_prometheus          |
+-------------------------------+
| alert                         |
| alert_configuration           |
| alert_configuration_history   |
| alert_image                   |
| alert_instance                |
| alert_notification            |
| alert_notification_state      |
| alert_rule                    |
| alert_rule_tag                |
| alert_rule_version            |
| annotation                    |
| annotation_tag                |
| api_key                       |
| builtin_role                  |
| cache_data                    |
| correlation                   |
| dashboard                     |
| dashboard_acl                 |
| dashboard_provisioning        |
| dashboard_public              |
| dashboard_public_email_share  |
| dashboard_public_magic_link   |
| dashboard_public_session      |
| dashboard_public_usage_by_day |
| dashboard_snapshot            |
| dashboard_tag                 |
| dashboard_usage_by_day        |
| dashboard_usage_sums          |
| dashboard_version             |
| data_keys                     |
| data_source                   |
| data_source_acl               |
| data_source_cache             |
| data_source_usage_by_day      |
| entity_event                  |
| file                          |
| file_meta                     |
| folder                        |
| kv_store                      |
| library_element               |
| library_element_connection    |
| license_token                 |
| login_attempt                 |
| migration_log                 |
| ngalert_configuration         |
| org                           |
| org_user                      |
| permission                    |
| playlist                      |
| playlist_item                 |
| plugin_setting                |
| preferences                   |
| provenance_type               |
| query_history                 |
| query_history_star            |
| quota                         |
| recording_rules               |
| remote_write_targets          |
| report                        |
| report_dashboards             |
| report_settings               |
| role                          |
| secrets                       |
| seed_assignment               |
| server_lock                   |
| session                       |
| setting                       |
| short_url                     |
| star                          |
| tag                           |
| team                          |
| team_group                    |
| team_member                   |
| team_role                     |
| temp_user                     |
| test_data                     |
| user                          |
| user_auth                     |
| user_auth_token               |
| user_dashboard_views          |
| user_role                     |
| user_stats                    |
+-------------------------------+
82 rows in set (0.01 sec)

mysql> 
posted @ 2025-04-07 00:41  尹正杰  阅读(185)  评论(0)    收藏  举报