作者公号:霸王龙的日常 专注数据库,分享实用的项目实战经验。

1 软件简介

AI与数据库结合是近些年的行业研究热点,openGauss较早地参与了该领域的探索,并取得了阶段性的成果。AI特性子模块名为DBMind。

DBMind作为openGauss数据库的一部分,为openGauss数据库提供了自动驾驶能力,是一款领先的开源数据库自治运维平台。通过DBMind, 您可以很容易地发现数据库的问题,同时可以实现秒级的数据库问题根因分析。

1.1 DBMind的特点

• DBMind采用了先进的插件化的架构形式,支持海量插件扩展;• 支持多种运行模式,具备命令行交互式运行、服务式运行;• 面向云原生进行设计,支持Prometheus,并提供多种丰富的exporter插件;• 提供丰富的对接模式,可以很容易地与现有管理系统进行对接,支持RESTful API、Python SDK、命令行、Prometheus协议等模式;• 支持端到端全流程的数据库自治运维能力,包括慢SQL根因分析、workload索引推荐、多指标关联挖掘、故障自修复、异常检测与根因分析等功能;

1.2 DBMind支持的主要能力

• 索引推荐• 异常检测与分析• 多指标关联分析• 慢SQL根因分析• 时序预测• 参数调优与推荐• SQL改写与优化• 故障自动修复

DBMind的AI4DB服务架构如下图所示

图中各关键组件说明:

• DBMind Service: DBMind后台服务,可用于定期离线计算,包括慢SQL根因分析、时序预测等;• Prometheus-server: Prometheus 监控指标存储的服务器;• metadatabase: DBMind在离线计算结束后,将计算结果存储在此处,支持openGauss、SQLite等数据库;• client: 用户读取DBMind离线计算结果的客户端,目前仅实现命令行客户端;若采用openGauss等数据库存储计算DBMind计算结果,则用户可以自行配置Grafana等可视化工具对该结果进行可视化;• openGauss-exporter: 用户从openGauss数据库节点上采集监控指标,供DBMind服务进行计算;• node-exporter: Prometheus官方提供的exporter, 可用于监控该节点的系统指标,如CPU和内存使用情况;• reprocessing-exporter: 用于对Prometheus采集到的指标进行二次加工处理,例如计算CPU使用率等。

2. 安装准备

2.1 环境信息

假设在主控节点 192.168.7.111上部署 DBMind,有一套端口号为26000的数据库环境,环境信息如下:

NAMEVALUE操作系统openEuler20.03 LTS架构x86_64IP192.168.7.111数据库openGauss5.0.1

2.2 创建管理用户

[root@trex ~]# useradd -m trex -d /home/trex

[root@trex ~]# passwd trex

此处设置密码为Trex@123

2.3 查看数据库信息

[root@trex ~]# su - omm

[omm@trex ~]$ gs_om -t status --detail

可以看到数据库端口为26000,数据目录为:/gaussdata/dn

2.4 创建DBMind使用的用户和数据库

切换到omm用户,创建DBMind使用的用户和数据库

[root@trex ~]# su - omm

[omm@trex ~]$ gs_guc reload -D /gaussdata/dn -c 'password_encryption_type=1'

[omm@trex ~]$ gsql -p 26000 -d postgres -c 'create user dbmind_monitor password "Trex@456";'

[omm@trex ~]$ gsql -p 26000 -d postgres -c 'alter user dbmind_monitor monadmin;'

[omm@trex ~]$ gs_guc reload -D /gaussdata/dn -c 'password_encryption_type=2'

[omm@trex ~]$ gsql -p 26000 -d postgres -c 'grant all privileges to dbmind_monitor;'

[omm@trex ~]$ gsql -U dbmind_monitor -p 26000 -d postgres -W "Trex@456" -c 'create database metadatabase;'

2.5 查看数据库

创建完成后,登录到数据库,并输入\l命令查看元数据库metadatabase是否存在。

[omm@trex ~]$ gsql -d postgres -p 26000 -r -U dbmind_monitor -W Trex@456

2.6 数据库信息

NAMEVALUE操作系统用户/密码trex/Trex@123数据库用户/密码dbmind_monitor/Trex@456数据库名metadatabase数据库端口26000

2.7 下载安装包

2.7.1 安装包信息

File NameSoftware NameOS/ArchDBMinddbmind-installer-x86_64-python3.10.sh.tar.gzlinux/x86_64Prometheusprometheus-2.43.0.linux-amd64.tar.gzlinux/x86_64node_exporternode_exporter-1.5.0.linux-amd64.tar.gzlinux/x86_64

2.7.2 下载地址

• DBMind

https://opengauss.obs.cn-south-1.myhuaweicloud.com/5.0.0/dbmind/x86/dbmind-installer-x86_64-python3.10.sh.tar.gz

• Prometheus

https://github.com/prometheus/prometheus/releases/download/v2.43.0/prometheus-2.43.0.linux-amd64.tar.gz

• node_exporter

https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz

**如果通过github下载Prometheus和node_exporter速度慢的话,**你也可以在公众号后台回复 dbmind 获取Prometheus和node_exporter安装包。

3 安装DBMind

3.1 上传dbmind安装包并授权

将安装包dbmind、prometheus、node_exporter上传到/home/trex目录并授权

[root@trex ~]# chown trex:trex -R /home/trex/*.tar.gz

[root@trex ~]# ll -h /home/trex/

3.2 解压

切换到trex用户下并解压dbmind安装包

[root@trex ~]# su - trex

[trex@trex ~]$ tar -zxvf dbmind-installer-x86_64-python3.10.sh.tar.gz

3.3 安装

[trex@trex ~]$ sh dbmind-installer-x86_64-python3.10.sh

安装过程中会提示确认安装位置和添加环境变量,按照提示输入回车或yes就行了。

3.4 重新加载环境变量

[trex@trex ~]$ source ~/.bashrc

3.5 测试是否安装成功

[trex@trex ~]$ gs_dbmind --help

3.6 关于Python运行环境

需要至少为Python3.7的版本。虽然在DBMind的实现中对Python3.7以下的环境尽可能地进行了兼容,但是这些低版本的Python环境疏于测试,可能会引发意料之外的异常。同时,在DBMind启动时,也会尝试校验Python版本,如果Python版本不符合要求,则默认不会继续执行后续的动作。

DBMind会优先选择使用在其根目录下 python 目录中的环境。即 gs_dbmind 命令会首先在/home/trex/openGauss-DBMind/python/bin 目录下寻找 python3 命令执行后续的Python功能。

[trex@trex ~]$ cd openGauss-DBMind/python/bin/

[trex@trex bin]$ ./python3 -V

3.7 关于第三方依赖

DBMind所使用的第三方依赖通过DBMind根目录下的 requirements-xxx.txt 文件指定。对于x86架构(amd64)以及ARM架构(aarch64),使用了不同的文件名进行标识。这是因为ARM平台对于某些第三方依赖并不友好,必须指定特定的版本才可以安装。

可以使用pip工具对第三方依赖进行安装。

3.7.1 方法1:在线安装第三方依赖

以x86环境为例,可以使用下述pip命令在线安装DBMind的第三方依赖库:

[trex@trex ~]$ cd openGauss-DBMind/

[trex@trex ~]$ /home/trex/openGauss-DBMind/python/bin/python3 -m pip install -r requirements-x86.txt

3.7.2 方法2:离线安装第三方依赖

如果你的服务器不能联网,只能离线安装第三方依赖。

• 上传依赖包

这里我已经将41个离线依赖包打包好了,压缩包为:python_3rd_x86_64.zip

公众号后台回复 dbmind 即可获取。

上传python_3rd_x86_64.zip到目录/home/trex/

• 授权并解压

[root@trex ~]# chown trex:trex /home/trex/python_3rd_x86_64.zip

[root@trex ~]# su - trex

[trex@trex ~]$ unzip python_3rd_x86_64.zip

• 安装

[trex@trex ~]$ /home/trex/openGauss-DBMind/python/bin/python3 -m pip install /home/trex/python_3rd_x86_64/*.whl

安装成功如下图

3.8 手动部署

3.8.1 规划配置

使用手动部署的方式,需要用户手动安装prometheus和相关的exporter组件,先做如下的规划配置:

组件地址node_exporter192.168.7.111: 9100cmd_exporter192.168.7.111: 9180opengauss_exporter192.168.7.111: 9187 -> 192.168.7.111: 26000prometheus192.168.7.111: 9090reprocessing_exporter192.168.7.111: 8181

3.8.2 node_exporter

部署node_exporter,将node_exporter安装包放入数据库节点并切换到trex用户空间后,执行如下命令:

解压安装包:

[trex@trex ~]$ tar zxvf node_exporter-1.5.0.linux-amd64.tar.gz

[trex@trex ~]$ cd node_exporter-1.5.0.linux-amd64

启动 默认端口号为9100:

[trex@trex node_exporter-1.5.0.linux-amd64]$ ./node_exporter > node_exporter.log 2>&1 &

如果端口号被占用,可以换成如下命令指定端口号启动

[trex@trex node_exporter-1.5.0.linux-amd64]$ ./node_exporter --web.listen-address=:9101 > node_exporter.log 2>&1 &

输入上述命令后,如果显示[1]+ Exit 字符表示node_exporter安装失败,可以通过node_exporter.log日志查看原因。

3.8.3 cmd_exporter

cmd_exporter用来执行cmd命令并获取返回结果以及采集日志信息。

[trex@trex node_exporter-1.5.0.linux-amd64]$ gs_dbmind component cmd_exporter --web.listen-address 0.0.0.0 --web.listen-port 9180 --disable-https

执行完上述命令,如果控制台显示The process is starting,说明安装无异常。

3.8.4 opengauss_exporter

opengauss_exporter是DBMind采集数据库的组件,从openGauss数据库中读取系统表(或系统视图)的数据,并通过Prometheus保存起来。在数据库节点上安装。

opengauss_exporter的默认端口号范围是9187-9197,如果被占用可以使用其他端口。

方式一:URL格式

[trex@trex ~]$ gs_dbmind component opengauss_exporter --url postgresql://dbmind_monitor:Trex%40456@192.168.7.111:26000/postgres --web.listen-address 0.0.0.0 --web.listen-port 9187 --log.level info --disable-https

说明: 对于采用URL格式的DSN,由于@等特殊字符用来分割URL串中各个部分的内容,故需要URL编码。例如某个用户dbmind的密码为Trex@456,则URL格式的DSN为psotgresql:dbmind:Trex%40456@127.0.0.1:26000,即将@字符编码为%40。

方式二:K-V格式 [trex@trex ~]$ gs_dbmind component opengauss_exporter --url "dbname=postgres user=dbmind_monitor password=Trex@456 host=192.168.7.111 port=26000" --web.listen-address 0.0.0.0 --web.listen-port 9187 --log.level info --disable-https

执行完上述命令,如果控制台显示The process is starting,说明安装无异常。也可以通过下述命令检查opengauss_exporter是否已启动。

# 端口号9187为用户指定的端口号, 如果失败会显示Connection refused

curl -vv http://192.168.7.111:9187/metrics

3.8.5 prometheus

prometheus是业内非常流行的开源监控系统,同时本身也是一款时序数据库。需要将其安装在主控节点,默认占用端口为9090,将prometheus安装包放入主控节点环境并切换到目标用户空间后,执行如下命令:

解压并切换到prometheus目录下: [trex@trex node_exporter-1.5.0.linux-amd64]$ cd ~

[trex@trex ~]$ tar zxvf prometheus-2.43.0.linux-amd64.tar.gz

[trex@trex ~]$ cd prometheus-2.43.0.linux-amd64/

修改prometheus配置文件: [trex@trex prometheus-2.43.0.linux-amd64]$ vi prometheus.yml

在该文件中找到 scrape_configs 部分,该部分包含了所有需要被抓取的targets的配置信息,用户需要根据自己的节点数量和端口占用情况进行修改。根据规划可做如下配置: scrape_configs:

# The job name is added as a label `job=` to any timeseries scraped from this config.

- job_name: "prometheus"

# metrics_path defaults to '/metrics'

# scheme defaults to 'http'.

static_configs:

- targets: ["192.168.7.111:9090"]

- job_name: "opengauss_exporter"

static_configs:

- targets: ["192.168.7.111:9187"]

- job_name: "node_exporter"

static_configs:

- targets: ["192.168.7.111:9100"]

- job_name: "reprocessing_exporter"

static_configs:

- targets: ["192.168.7.111:8181"]

- job_name: "cmd_exporter"

static_configs:

- targets: ["192.168.7.111:9180"]

启动prometheus并将日志输入到同目录下的prometheus.log里:

[trex@trex prometheus-2.43.0.linux-amd64]$ ./prometheus --web.enable-admin-api --web.enable-lifecycle --storage.tsdb.retention.time=1w >./prometheus.log 2>&1 &

3.8.6 reprocessing_exporter

reprocessing_exporter是一个用于二次加工处理数据的exporter。由于node_exporter、opengauss_exporter保存到prometheus中的数据是即时的监控信息,而只通过这些信息是无法反应某些指标的瞬时增量信息的。因此,reprocessing_exporter可以用来计算增量信息或者聚合结果等。

由于reprocessing_exporter是从prometheus中获取指标数据,进行二次加工处理后再返回给prometheus。因此,它与prometheus是一一对应的,即如果只有一个prometheus服务,则只需要一个reprocessing_exporter即可。

prometheus和reprocessing_exproter一起部署在主控节点上,reprocessing_exproter依赖prometheus,所以需要先启动prometheus再启动reprocessing_exporter。可以通过下述命令启动reprocessing_exporter:

# host_ip为主控节点的ip地址,9090为prometheus占用的端口号,8181为reprocessing_exporter占用的端口号,用户可以按需修改

gs_dbmind component reprocessing_exporter host_ip 9090 --web.listen-address 0.0.0.0 --web.listen-port 8181 --disable-https

根据实际环境修改后

[trex@trex ~]$ gs_dbmind component reprocessing_exporter 192.168.7.111 9090 --web.listen-address 0.0.0.0 --web.listen-port 8181 --disable-https

启动完成之后,访问prometheus网页:http://{主控节点ip}:9090/,打开Status/Targets,查看所有exporter的启动状态。

http://192.168.7.111:9090/

4. 启动DBMind

4.1 创建配置文件

切换到openGauss-DBMind目录下,输入如下命令启动DBMind

[trex@trex ~]$ cd ~/openGauss-DBMind

[trex@trex openGauss-DBMind]$ gs_dbmind service setup -c dbmindconf

启动配置,会在openGauss-DBMind目录下创建dbmindconf/dbmind.conf配置文件

4.2 修改配置文件

[trex@trex openGauss-DBMind]$ vi dbmindconf/dbmind.conf

其中必填的配置项如下:

4.2.1 TSDB

TDSB为时序数据库配置,即prometheus相关信息。

[TSDB]

name = prometheus # The type of time-series database. Options: prometheus.

host = 192.168.7.111 # Address of time-series database.

port = 9090 # Port to connect to time-series database.

4.2.2 METADATABASE

DBMind服务器所创建的metadatabase数据库相关信息,注意用户需要有数据库创建的权限。在启动DBMind之前,需要把metadatabase创建好。

[METADATABASE]

dbtype = opengauss # Database type. Options: sqlite, opengauss, postgresql.

host = 192.168.7.111 # {数据库主节点ip:192.168.7.111} Address of meta-data database.

port = 26000 # {数据库占用的端口号:26000} Port to connect to meta-data database.

username = dbmind_monitor # User name to connect to meta-data database.

password = Trex@456 # Password to connect to meta-data database.

database = metadatabase # Database name to connect to meta-data database.

4.2.3 WORKER

该模块用于指定DBMind可以使用的worker子进程数量,如果写0则会进行自适应,尽可能多地使用CPU资源,根据需要进行修改。

用户可以使用cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l命令查看逻辑CPU的个数,根据服务器情况配置worker子进程数量。

[WORKER]

process_num = 2 # Number of worker processes on a local node. Less than or equal to zero means adaptive.

4.2.4 AGENT

该模块用于指定DBMind连接到openGauss Agent的信息。使用该Agent,可以让DBMind获取到被监控实例的即时状态,从而提高分析准确性。同时,也可以向数据库实例下发一些变更动作。

master_url为http://{DBMind服务器IP}:{opengauss_exporter监听端口},置空会自动识别。

usename为数据库实例的dbmind_minotor用户。

[AGENT]

master_url = http://192.168.7.111:9187

username = dbmind_monitor # Username to login the monitoring database. Credential for agent.

password = Trex@456 # Password to login the monitoring database. Credential for agent.

4.2.5 TIMED_TASK

设置定时任务:

• discard_expired_results: 删除过期结果

• self_monitoring:指标异常监控• daily_inspection:日检• weekly_inspection:周检• monthly_inspection: 月检• slow_sql_diagnosis:慢SQL诊断• knob_recommend:参数调优• slow_query_killer:慢SQL查杀

[TIMED_TASK]

task=discard_expired_results,self_monitoring,daily_inspection, weekly_inspection,slow_sql_diagnosis,knob_recommend,slow_query_killer# select the scheduled task to start,separated by ','.

4.2.6 WEB-SERVICE

[WEB-SERVICE]

host = 192.168.7.111 # Which host to bind for web service. '0.0.0.0' means to bind all.

port = 8080 # Port to listen for web serivce.

4.3 配置项初始化

[trex@trex openGauss-DBMind]$ gs_dbmind service setup --initialize -c dbmindconf

4.4 启动DBMind服务

[trex@trex openGauss-DBMind]$ gs_dbmind service start -c dbmindconf

显示如下报错:

注:

官网提供的依赖文件requirements-x86.txt的列表里没有python-multipart,所以这里需要单独安装。

如果你前面使用python_3rd_x86_64.zip离线安装的依赖包,则不会存在该问题。因为压缩包中已包含python-multipart。<详见3.7.2章节>

解决方法:

通过如下命令安装python-multipart 依赖包。

注意,这里需要联网安装。

[trex@trex openGauss-DBMind]$ /home/trex/openGauss-DBMind/python/bin/python3 -m pip install python-multipart

再次启动DBMind服务

[trex@trex openGauss-DBMind]$ gs_dbmind service start -c dbmindconf

启动完成后,就可以通过http://{DBMind服务器IP}:8080访问DBMind前台页面。

5 访问DBMind

http://192.168.7.111:8080

注:

用户名:dbmind_monitor

密码:Trex@456

进入主页

至此,openGauss DBMind已搭建完成。

推荐文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: