设置 InfluxDB
在您开始本教程时,请做以下几点,以确保您所需的一切都已就位。
(可选) 下载、安装和配置
influxCLI。influxCLI 提供了一种简单的方法,通过命令行与 InfluxDB 进行交互。有关详细的安装和设置说明,请参阅 Use the influx CLI。创建一个全访问API令牌。
使用 InfluxDB UI、
influxCLI 或 InfluxDB API 创建一个 全访问令牌。- Visit cloud2.influxdata.com in a browser to log in and access the InfluxDB UI.
- Navigate to Load Data > API Tokens using the left navigation bar.
- Click + Generate API token and select All Access API Token.
- Enter a description for the API token and click Save.
- Copy the generated token and store it for safe keeping.
如果您还没有,请下载、安装并配置
influxCLI。使用
influx auth create命令创建一个全访问令牌。提供以下内容:
--all-access标志--host标志与您的 InfluxDB 区域 URL-o, --org或--org-id标志与您的 InfluxDB 组织名称或 ID-t, --token标志与您的操作员令牌
influx auth create \ --all-access \ --host http://cloud2.influxdata.com \ --org <YOUR_INFLUXDB_ORG_NAME> \ --token <YOUR_INFLUXDB_OPERATOR_TOKEN>复制生成的令牌并将其安全存储。
使用
POST请求方法向 InfluxDB API/api/v2/authorizations端点发送请求。POST http://cloud2.influxdata.com/api/v2/authorizations
请在您的请求中包含以下内容:
- 请求头:
- 授权: 令牌 <INFLUX_OPERATOR_TOKEN>
- 内容类型: application/json
- 请求体: JSON主体具有以下属性:
- 状态:
"active" - 描述: API令牌描述
- orgID: InfluxDB组织ID
- 权限: 对象数组,每个对象表示对InfluxDB资源类型或特定资源的权限。每个权限包含以下属性:
- 操作: “read”或“write”
- 资源: JSON对象,表示要授予权限的InfluxDB资源。每个资源至少包含以下属性:
- orgID: InfluxDB组织ID
- 类型: 资源类型。
有关InfluxDB资源类型的相关信息,请使用
/api/v2/resources端点.
- 状态:
以下示例使用cURL和InfluxDB API生成一个全访问令牌:
export INFLUX_HOST=http://cloud2.influxdata.com export INFLUX_ORG_ID=<YOUR_INFLUXDB_ORG_ID> export INFLUX_TOKEN=<YOUR_INFLUXDB_OPERATOR_TOKEN> curl --request POST \ "$INFLUX_HOST/api/v2/authorizations" \ --header "Authorization: Token $INFLUX_TOKEN" \ --header "Content-Type: text/plain; charset=utf-8" \ --data '{ "status": "active", "description": "All access token for get started tutorial", "orgID": "'"$INFLUX_ORG_ID"'", "permissions": [ {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "authorizations"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "authorizations"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "buckets"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "buckets"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "dashboards"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "dashboards"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "orgs"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "orgs"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "sources"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "sources"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "tasks"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "tasks"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "telegrafs"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "telegrafs"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "users"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "users"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "variables"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "variables"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "scrapers"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "scrapers"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "secrets"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "secrets"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "labels"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "labels"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "views"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "views"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "documents"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "documents"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "notificationRules"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "notificationRules"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "notificationEndpoints"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "notificationEndpoints"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "checks"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "checks"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "dbrp"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "dbrp"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "notebooks"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "notebooks"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "annotations"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "annotations"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "remotes"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "remotes"}}, {"action": "read", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "replications"}}, {"action": "write", "resource": {"orgID": "'"$INFLUX_ORG_ID"'", "type": "replications"}} ] } '响应体包含一个具有以下属性的JSON对象:
- id: API令牌ID
- 令牌: API 令牌 (重要)
- status: 令牌状态
- 描述: 令牌描述
- orgID: 与该令牌关联的InfluxDB组织ID
- org: 与令牌相关联的InfluxDB组织名称
- userID: 与该令牌关联的用户 ID
- user: 与该令牌关联的用户名
- permissions: 组织资源的权限列表
复制生成的
token并安全存储。我们建议使用密码管理器或秘密存储库来安全地存储敏感令牌。
配置认证凭据.
在您进行此教程时,与 InfluxDB Cloud 的交互需要您的 InfluxDB 主机、组织名称或 ID以及您的 API 令牌。根据您使用的客户端与 InfluxDB 进行交互,提供这些凭据的方法是不同的。
在配置你的令牌时,使用你创建的 全访问令牌。
在通过InfluxDB UI 管理 InfluxDB 时,身份验证凭据会自动提供,这些凭据与您登录的用户相关联。
有三种方式可以向
influxCLI 提供身份验证凭据:本快速入门教程中的所有
influxCLI 示例假设您的 InfluxDB 主机、组织 和 令牌 是通过 活动influxCLI 配置 或环境变量提供的。使用InfluxDB API时,请以以下方式提供所需的连接凭据:
- InfluxDB 主机: 您的 InfluxDB Cloud 区域 URL。
- InfluxDB API 令牌: 包含一个
Authorization标头,使用Bearer或Token方案和您的 InfluxDB API 令牌。例如:Authorization: Bearer 0xxx0o0XxXxx00Xxxx000xXXxoo0==. - InfluxDB 组织名称或 ID: 根据使用的 API 端点,将其作为 URL 路径、查询字符串或请求体的一部分传递。
本教程中的所有API示例均使用cURL从命令行执行。要为示例cURL命令提供所有必要的凭据,请在命令行会话中设置以下环境变量。
export INFLUX_HOST=http://cloud2.influxdata.com export INFLUX_ORG=<YOUR_INFLUXDB_ORG_NAME> export INFLUX_ORG_ID=<YOUR_INFLUXDB_ORG_ID> export INFLUX_TOKEN=<YOUR_INFLUXDB_API_TOKEN>创建一个桶.
使用InfluxDB UI、
influxCLI或InfluxDB API创建一个新桶。- Visit cloud2.influxdata.com in a browser to log in and access the InfluxDB UI.
- Navigate to Load Data > Buckets using the left navigation bar.
- Click + Create bucket.
- Provide a bucket name (get-started) and select Never to create a bucket with an infinite retention period.
- Click Create.
如果您还没有,请下载、安装并配置
influxCLI。使用
influx bucket create命令创建一个新存储桶。提供以下内容:
-n, --name标志与桶的名称。- 连接和认证凭据
influx bucket create --name get-started
要使用InfluxDB HTTP API创建一个桶,请使用
POST请求方法向InfluxDB API/api/v2/buckets端点发送请求。POST http://cloud2.influxdata.com/api/v2/buckets
请在您的请求中包含以下内容:
- Headers:
- Authorization: Token
INFLUX_TOKEN - Content-Type:
application/json
- Authorization: Token
- 请求正文: JSON 对象,具有以下属性:
- org: InfluxDB 组织名称
- name: 桶名称
- retentionRules: 定义桶的保留期限的保留规则对象列表。
每个保留规则对象具有以下属性:
- type:
"expire" - everySeconds: 保留期持续时间,以秒为单位。
0表示保留期是无限的。
- type:
export INFLUX_HOST=http://cloud2.influxdata.com export INFLUX_ORG_ID=<YOUR_INFLUXDB_ORG_ID> export INFLUX_TOKEN=<YOUR_INFLUXDB_API_TOKEN> curl --request POST \ "$INFLUX_HOST/api/v2/buckets" \ --header "Authorization: Token $INFLUX_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "orgID": "'"$INFLUX_ORG_ID"'", "name": "get-started", "retentionRules": [ { "type": "expire", "everySeconds": 0 } ] }'