InfluxDB 1.x 兼容性 API
InfluxDB v2 API 包括 InfluxDB 1.x 兼容性端点,这些端点可与 InfluxDB 1.x 客户端库和第三方集成(如 Grafana 等)一起使用。
身份验证
InfluxDB 1.x 兼容性端点要求所有查询和写入请求都使用 API 令牌或 1.x 兼容的凭据进行身份验证。
使用令牌方案进行身份验证
令牌身份验证需要以下凭据
- token:InfluxDB API 令牌
使用带有 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 "https://127.0.0.1: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 约定 username
和 password
的客户端(不支持 Authorization: Token
方案的客户端)一起使用
管理凭据
用户名和密码方案需要以下凭据
- username:1.x 用户名(这与 UI 登录用户名不同)
- password:1.x 密码或 InfluxDB API 令牌。
有关更多信息,请参阅如何在从 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 "https://127.0.0.1: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 "https://127.0.0.1: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();
}
替换以下内容
:InfluxDB 1.x 用户名INFLUX_USERNAME
:InfluxDB 1.x 密码或 InfluxDB API 令牌INFLUX_PASSWORD_OR_TOKEN
替换以下内容
:您注册时使用的电子邮件地址exampleuser@influxdata.com
:您的 InfluxDB API 令牌INFLUX_API_TOKEN
InfluxQL 支持
兼容性 API 支持 InfluxQL,但有以下注意事项
- 不支持
INTO
子句(例如,SELECT ... INTO ...
)。 - 除了
DELETE
和DROP MEASUREMENT
查询(仍允许使用)之外,不支持 InfluxQL 数据库管理命令。
兼容性端点
/query
/query
1.x 兼容性端点使用 InfluxQL 查询 InfluxDB Cloud 和 InfluxDB OSS 2.x。
GET https://127.0.0.1:8086/query
/write
/write
1.x 兼容性端点使用来自 InfluxDB 1.x /write
API 端点的模式将数据写入 InfluxDB Cloud 和 InfluxDB OSS 2.x。
POST https://127.0.0.1:8086/write
数据库和保留策略映射
数据库和保留策略 (DBRP) 映射服务将 InfluxDB 1.x 数据库和保留策略组合映射到 InfluxDB Cloud 和 InfluxDB OSS 2.x Bucket。
此页内容是否对您有帮助?
感谢您的反馈!