文档文档

InfluxDB 1.x 兼容性 API

InfluxDB v2 API 包含 InfluxDB 1.x 兼容性端点,这些端点与 InfluxDB 1.x 客户端库和第三方集成(如 Grafana 等)一起使用。

查看完整的 v1 兼容性 API 文档

身份验证

InfluxDB 1.x 兼容性端点要求所有查询和写入请求都使用 API 令牌或 1.x 兼容凭据进行身份验证。

使用令牌方案进行身份验证

令牌身份验证需要以下凭据

使用带有 Token 方案的 Authorization 标头向 InfluxDB 提供您的令牌。Token 方案是单词 Token、一个空格和您的令牌(所有字符均区分大小写)。

语法

Authorization: Token INFLUX_API_TOKEN

示例

#######################################
# Use a token in the Authorization header
# to query the InfluxDB 1.x compatibility API.
#
# Replace INFLUX_API_TOKEN with your InfluxDB API token.
#######################################

curl --get "http://localhost:8086" \
  --header "Authorization: Token INFLUX_API_TOKEN" \
  --header 'Content-type: application/json' \
  --data-urlencode "db=mydb" \
  --data-urlencode "q=SELECT * FROM cpu_usage"
/**
  * Use the Token authentication scheme
  * to query the InfluxDB 1.x compatibility API.
  *
  * Replace INFLUX_API_TOKEN with your InfluxDB API token.
  */

const https = require('https');
const querystring = require('querystring');

function queryWithToken() {
  const queryparams = {
      db: 'mydb',
      q: 'SELECT * FROM cpu_usage',
  };

  const options = {
    host: 'localhost:8086',
    path: "/query?" + querystring.stringify(queryparams),
    headers: {
      'Authorization': 'Token INFLUX_API_TOKEN',
      'Content-type': 'application/json'
    },
  };

  const request = https.get(options, (response) => {
    let rawData = '';
    response.on('data', () => {
      response.on('data', (chunk) => { rawData += chunk; });
    })
    response.on('end', () => {
      console.log(rawData);
    })
  });

  request.end();
}

使用用户名和密码方案进行身份验证

将以下身份验证方案与支持 InfluxDB 1.x usernamepassword 约定的客户端(不支持 Authorization: Token 方案)一起使用

管理凭据

用户名和密码方案需要以下凭据

  • username: 1.x 用户名(这与 UI 登录用户名不同)
  • password: 1.x 密码或 InfluxDB API 令牌。

密码或令牌

如果您已为 1.x 兼容用户名设置密码,请提供 1.x 兼容密码。如果您尚未为 1.x 兼容用户名设置密码,请提供 InfluxDB 身份验证令牌作为密码。

有关更多信息,请参阅手动从 InfluxDB v1 升级到 v2 时如何创建和管理 1.x 兼容授权

基本身份验证

使用带有 Basic 方案的 Authorization 标头向 InfluxDB 提供用户名和密码凭据。

大多数 HTTP 客户端都提供基本身份验证选项,该选项接受 <username>:<password> 语法并在发送请求之前对凭据进行编码。

语法
Authorization: Basic INFLUX_USERNAME:INFLUX_PASSWORD_OR_TOKEN
示例
#######################################
# Use Basic authentication with an
# InfluxDB 1.x compatible username and password
# to query the InfluxDB 1.x compatibility API.
#
# Replace 
INFLUX_USERNAME
with your 1.x-compatible username.
# Replace
INFLUX_PASSWORD_OR_TOKEN
with your InfluxDB API token
# or 1.x-compatible password. ####################################### # Use the default retention policy. ####################################### # Use the --user option with `--user <username>:<password>` syntax # or the `--user <username>` interactive syntax to ensure your credentials are # encoded in the header. ####################################### curl --get "http://localhost:8086/query" \ --user "
INFLUX_USERNAME
"
:"
INFLUX_PASSWORD_OR_TOKEN
"
\
--data-urlencode "db=mydb" \ --data-urlencode "q=SELECT * FROM cpu_usage"
/**
  * Use Basic authentication with an
  * InfluxDB 1.x compatible username and password
  * to query the InfluxDB 1.x compatibility API.

  * Replace 
INFLUX_USERNAME
with your 1.x-compatible username.
* Replace
INFLUX_PASSWORD_OR_TOKEN
with your InfluxDB API token
* or 1.x-compatible password. * Use the default retention policy. */ const https = require('https'); const querystring = require('querystring'); function queryWithUsername() { const queryparams = { db: 'mydb', q: 'SELECT * FROM cpu_usage', }; const options = { host: 'localhost:8086', path: '/query?' + querystring.stringify(queryparams), auth: '
INFLUX_USERNAME
:
INFLUX_PASSWORD_OR_TOKEN
'
,
headers: { 'Content-type': 'application/json' }, }; const request = https.get(options, (response) => { let rawData = ''; response.on('data', () => { response.on('data', (chunk) => { rawData += chunk; }); }) response.on('end', () => { console.log(rawData); }) }); request.end(); }

查询字符串身份验证

使用 InfluxDB 1.x API 参数通过查询字符串提供凭据。

使用查询字符串参数时请考虑
  • URL 编码可能包含空格或其他特殊字符的查询参数。
  • 注意通过 URL 暴露敏感数据的风险
语法
 /query/?u=
INFLUX_USERNAME
&p=
INFLUX_PASSWORD_OR_TOKEN
/write/?u=
INFLUX_USERNAME
&p=
INFLUX_PASSWORD_OR_TOKEN
示例
#######################################
# Use querystring authentication with an
# InfluxDB 1.x compatible username and password
# to query the InfluxDB 1.x compatibility API.
#
# Replace 
INFLUX_USERNAME
with your 1.x-compatible username.
# Replace
INFLUX_PASSWORD_OR_TOKEN
with your InfluxDB API token
# or 1.x-compatible password. # # Use the default retention policy. ####################################### curl --get "http://localhost:8086/query" \ --data-urlencode "u=
INFLUX_USERNAME
"
\
--data-urlencode "p=
INFLUX_PASSWORD_OR_TOKEN
"
\
--data-urlencode "db=mydb" \ --data-urlencode "q=SELECT * FROM cpu_usage"
/**
  * Use querystring authentication with an
  * InfluxDB 1.x compatible username and password
  * to query the InfluxDB 1.x compatibility API.
  *
  * Replace 
INFLUX_USERNAME
with your 1.x-compatible username.
* Replace
INFLUX_PASSWORD_OR_TOKEN
with your InfluxDB API token
* or 1.x-compatible password. * * Use the default retention policy. */ const https = require('https'); const querystring = require('querystring'); function queryWithToken() { const queryparams = { db: 'mydb', q: 'SELECT * FROM cpu_usage', u: '
INFLUX_USERNAME
'
,
p: '
INFLUX_PASSWORD_OR_TOKEN
'
}; const options = { host: 'localhost:8086', path: "/query?" + querystring.stringify(queryparams) }; const request = https.get(options, (response) => { let rawData = ''; response.on('data', () => { response.on('data', (chunk) => { rawData += chunk; }); }) response.on('end', () => { console.log(rawData); }) }); request.end(); }

替换以下内容

替换以下内容

  • exampleuser@influxdata.com
    : 您注册时使用的电子邮件地址
  • INFLUX_API_TOKEN
    : 您的 InfluxDB API 令牌
InfluxQL 支持

兼容性 API 支持 InfluxQL,但存在以下注意事项

  • 不支持 INTO 子句(例如,SELECT ... INTO ...)。
  • 除了 DELETEDROP MEASUREMENT 查询(仍然允许)之外,不支持 InfluxQL 数据库管理命令。

兼容性端点


此页是否对您有帮助?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像当前一样使用它,而无需对您的代码进行任何更改。

阅读更多

现已全面上市

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速、近实时数据引擎,可在本地磁盘或对象存储中收集和处理数据并持久化。InfluxDB 3 Enterprise 构建在 Core 的基础上,增加了高可用性、读取副本、增强的安全性以及数据压缩,从而实现更快的查询和优化的存储。InfluxDB 3 Enterprise 的免费层可供非商业家庭或业余爱好者使用。

更多信息,请查看