---
url: /v6.2/install-first.md
---
# 单机安装及启动

本文讲述如何在单机环境下进行 HENGSHI SENSE 安装的操作过程。

在安装前请确认网络环境，如果是隔离环境，无法连接互联网时，请先按照 [离线环境安装依赖](install-dependencies-offline.md)的指导进行依赖包的安装，然后按照本文的指导继续操作。如果网络环境可以连接互联网，请直接按照本文的指导进行安装。

## 准备工作

1. 请参考[安装环境](enviroment.md)文档准备机器配置

2. 配置用户与安装目录\
   安装程序需要以非 root 用户执行，安装和初始化期间临时需要**无密码sudo**权限。在此之后启动运行可以收回sudo权限。
   请不要使用 root 安装，推荐建立一个拥有 sudo 权限的用户安装、运行 HENGSHI SENSE。\
   本文示例以 hengshi 用户举例，安装路径为/opt/hengshi，下面举例介绍如何创建 hengshi 用户及设置安装目录和权限。
   ```shell
   sudo -n -l
   #如果输出 sudo: a password is required 则说明缺少无密码sudo权限，需要授权后再次验证
   grep hengshi /etc/passwd > /dev/null || sudo useradd -m hengshi
   #建立 henghsi 用户
   sudo mkdir -p /opt/hengshi && sudo chown hengshi:hengshi /opt/hengshi
   #设置安装目录及权限
   ```

3. 服务端 sshd 监听非22端口 (若 sshd 为 22 可忽略这步)\
   以下2种情况需要在部署用户 ~/.ssh/config 中配置各个 host 的实际端口

   1. 如果部署 hengshi 的机器 SSH 端口不是默认的22端口
   2. 如果部署 engine 的 segments 的机器与 hengshi 服务不在一个机器上且 SSH 端口不是默认的22端口

   例如: 本机配置 hostname 为 localhost，并且 HS_ENGINE_SEGMENTS=(Node-A)，对应的监听端口都为122。
   在每台机器的 .ssh/config 配置文件中都需要包含以下配置:

   ```shell
   Host localhost
     Port 122
   Host Node-A
     Port 122
   ```

## 安装 HENGSHI SENSE

执行以下步骤完成安装。

1. 切换到执行安装的用户，示例中用户名为 hengshi。

```shell
sudo su - hengshi
```

2. 进入安装包解压后的目标目录。

```shell
cd ~/pkgs/hengshi-sense-[version]
```

3. 执行安装命令。

```shell
./hs_install -p /opt/hengshi
```

> \[!TIP] 提示
> 如果在使用服务时替换了衡石 metadb，需要确保 `export HS_PG_IF_EXTERNAL=false` 变量的值为 true，可在配置文件 conf/hengshi-sense-env.sh 更改，这样在升级过程中将不会要求停止此服务进行安装升级。

4. 安装成功时，提示以下信息。

```shell
SUCCESSED! installed to xxxxx
```

## 配置系统

启动服务前，请阅读[配置文件](install-configure.md)对相关的配置进行设置。
如内置引擎类型要求为 Doris，请阅读[Doris 引擎配置](engine-doris-config.md)。

如果需要导入自签发证书，默认放在 `/opt/hengshi/conf/trust-certs`。
如需修改路径，请在 `conf/hengshi-sense-env.sh` 中设置 `EVEREST_TRUST_CERTS_DIR`。

把证书放到该目录后，先切到安装目录，再执行以下命令导入和查看结果：

```shell
cd /opt/hengshi
bin/hengshi-sense-bin certs import
bin/hengshi-sense-bin certs check
bin/hengshi-sense-bin certs list
```

## 启动服务

请按照如下步骤启动服务。

1. 切换到启动服务的用户，示例中的用户名为 hengshi。

```shell
sudo su - hengshi
```

2. 进入安装目录。

```shell
cd /opt/hengshi
```

3. 初始化 OS，执行前请确认用户有 sudo 权限。

```shell
bin/hengshi-sense-bin init-os all
```

> \[!TIP] 提示
> 离线环境请使用bin/hengshi-sense-bin init-os all-offline 跳过联网更新依赖

4. 初始化 HENGSHI SENSE。

```shell
bin/hengshi-sense-bin init all
```

5. 启动 HENGSHI SENSE 服务。

```shell
bin/hengshi-sense-bin start all
```

6. 当服务启动成功后，提示以下信息，模块的状态为**IS ACTIVE**。

```shell
 IS ACTIVE! : [metadb] @127.0.0.1:5301/hengshi
 IS ACTIVE! : [engine] @localhost:5302/hengshi
 IS ACTIVE! : [minio] minio server /opt/hengshi/minio-data
 IS ACTIVE! : [redis] 127.0.0.1@5307
 IS ACTIVE! : [syslog] syslog-ng --no-caps --module-path=/opt/hengshi/lib/syslog-ng/lib/syslog-ng -f /opt/hengshi/conf/syslog-ng.conf -p /opt/hengshi/logs/syslog-ng.pid -R /opt/hengshi/logs/syslog-ng.persist -c /opt/hengshi/logs/syslog-ng.ctl
 IS ACTIVE! : [apmserver] /opt/hengshi/lib/apm-server/apm-server -c /opt/hengshi/conf/apm-server.yaml
 IS ACTIVE! : [hengshi] jar /opt/hengshi/lib/bootstrap-6.0.0.jar
 IS ACTIVE! : [monit] /opt/hengshi/lib/monit/bin/monit -c /opt/hengshi/conf/monitrc 
```

7. 通过浏览器访问服务地址，默认地址: `http://localhost:8080` 使用 HENGSHI SENSE 服务。如果不能访问，请检查配置文件 conf/hengshi-sense-env.sh 中的服务端口 HS_HENGSHI_PORT 是否对外开放。

8. 如需配置 nginx 请参考: [安装与启动常见 FAQ](installation-faq.md)  中的1.1.3 如何通过80和443端口进行服务

## 启动服务后相关操作

当 HENGSHI SENSE 服务运行时，需要定时备份数据防止数据丢失，及时清理无用日志释放存储空间。

1. 定时备份数据

   建议每日对数据库 metadb 进行备份，可以备份到本地设备也可以备份到远程设备。定时备份建议在非业务高峰期进行，比如凌晨，避免影响用户使用服务。 下面示例是每日零时将数据备份到远程设备的执行命令，参数详细说明请参考[数据备份](backup-restore.md)。

   ```shell
   0 0 * * * /opt/hengshi/bin/dbbackup.sh -m metadb -l /BACKUP/PATH -h $REMOTE_IP -r /BACKUP/PATH
   ```

2. 定时清理日志

   运行过程中，HENGSHI SENSE 会产生运行日志，这些日志需要定时清理释放存储空间。下面示例是每日定时清理内部数据库的滚动日志的命令。

   ```shell
   0 0 * * * /opt/hengshi/bin/clean_engine.sh -t -r >& /opt/hengshi/logs/clean_engine.log
   0 0,12 * * * /opt/hengshi/bin/clean_engine.sh -c -g -p
   */5 * * * * /opt/hengshi/bin/clean_engine.sh -l
   ```

> \[!TIP] 提示
> 不要直接暴露衡石服务的端口到公网，建议通过 nginx+域名对外提供服务。避免因组件原因造成被攻击的情况。

## 停止服务

通过执行以下命令停止服务。

```shell
sudo su - hengshi
cd /opt/hengshi
bin/hengshi-sense-bin stop all
```

服务停止成功后，提示以下信息，模块状态为"NOT ACTIVE"。

```shell
 NOT ACTIVE! : [metadb] @127.0.0.1:5501/hengshi
 NOT ACTIVE! : [engine] @localhost:5502/hengshi
 NOT ACTIVE! : [minio] minio server /opt/hengshi/minio-data
 NOT ACTIVE! : [redis] 127.0.0.1@5507
 NOT ACTIVE! : [syslog] syslog-ng --no-caps --module-path=/opt/hengshi/lib/syslog-ng/lib/syslog-ng -f /opt/hengshi/conf/syslog-ng.conf -p /opt/hengshi/logs/syslog-ng.pid -R /opt/hengshi/logs/syslog-ng.persist -c /opt/hengshi/logs/syslog-ng.ctl
 NOT ACTIVE! : [apmserver] /opt/hengshi/lib/apm-server/apm-server -c /opt/hengshi/conf/apm-server.yaml
 NOT ACTIVE! : [hengshi] jar /opt/hengshi/lib/bootstrap-6.0-SNAPSHOT.jar
 NOT ACTIVE! : [monit] /opt/hengshi/lib/monit/bin/monit -c /opt/hengshi/conf/monitrc
```

## 检查服务状态

在服务运行过程中，可通过执行以下命令查看程序运行状态。

```shell
sudo su - hengshi
cd /opt/hengshi
bin/hengshi-sense-bin health all
```

其中第一列为模块状态信息:

* **Healthy**高亮表示对应模块运行中
* **Stopped**高亮表示对应模块停止服务
* **Starting**高亮表示对应模块启动中

```shell
[Stopped Starting **Healthy**]: [metadb] hengshi@127.0.0.1:5301/hengshi
[Stopped Starting **Healthy**]: [engine] hengshi@localhost:5302/hengshi
[Stopped Starting **Healthy**]: [minio] minio server /opt/hengshi/minio-data
[Stopped Starting **Healthy**]: [redis] redis-server .*:5307
[Stopped Starting **Healthy**]: [syslog] syslog-ng --no-caps --module-path=/opt/hengshi/lib/syslog-ng/lib/syslog-ng -f /opt/hengshi/conf/syslog-ng.conf -p /opt/hengshi/logs/syslog-ng.pid -R /opt/hengshi/logs/syslog-ng.persist -c /opt/hengshi/logs/syslog-ng.ctl
[Stopped Starting **Healthy**]: [apmserver] /opt/hengshi/lib/apm-server/apm-server -c /opt/hengshi/conf/apm-server.yaml
[Stopped Starting **Healthy**]: [hengshi] jar /opt/hengshi/lib/bootstrap-5.4.5.jar
[Stopped Starting **Healthy**]: [monit] /opt/hengshi/lib/monit/bin/monit -c /opt/hengshi/conf/monitrc
```
