需求
不管是yum安裝,亦或者是docker安裝的方式,influxdb默認(rèn)安裝完畢之后,并不會(huì)自帶用戶(hù)認(rèn)證的功能,直接就可以進(jìn)行訪(fǎng)問(wèn)的了。
情況如下:
root@d248ddfcd76c:/# influx
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
>
或者curl直接查詢(xún)出來(lái)數(shù)據(jù),不需要用戶(hù)認(rèn)證,如下:
[root@server ~]# curl -G 'http://localhost:8086/query' --data-urlencode "q=show databases;"
{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["_internal"]]}]}]}
[root@server ~]#
對(duì)于這種不設(shè)置防備的措施,總體來(lái)說(shuō),不是很好。
所以,下面來(lái)看看怎么設(shè)置用戶(hù)認(rèn)證。
處理步驟
創(chuàng)建admin用戶(hù)
基本命令:
# 顯示用戶(hù)
SHOW USERS
# 創(chuàng)建用戶(hù)
CREATE USER "username" WITH PASSWORD 'password'
# 賦予用戶(hù)管理員權(quán)限
GRANT ALL PRIVILEGES TO username
# 創(chuàng)建管理員權(quán)限的用戶(hù)
CREATE USER <username> WITH PASSWORD '<password>' WITH ALL PRIVILEGES
# 修改用戶(hù)密碼
SET PASSWORD FOR username = 'password'
# 撤消權(quán)限
REVOKE ALL ON mydb FROM username
# 查看權(quán)限
SHOW GRANTS FOR username
# 刪除用戶(hù)
DROP USER "username"
實(shí)際操作如下:
# 查看所有用戶(hù)
> show users
user admin
---- -----
>
# 創(chuàng)建一個(gè)root用戶(hù),設(shè)置密碼為newpwd,主要不要使用雙引號(hào)" 括起來(lái),不然會(huì)報(bào)錯(cuò)
> create user "root" with password 'newpwd'
>
# 再次查看用戶(hù)信息,發(fā)現(xiàn)admin為false,說(shuō)明還要設(shè)置一下權(quán)限。
> show users
user admin
---- -----
root false
>
# 刪除root用戶(hù)
> drop user root
>
> show users
user admin
---- -----
>
# 重新設(shè)置root用戶(hù),并設(shè)置帶上所有權(quán)限
> create user "root" with password 'newpwd' with all privileges
>
# 發(fā)現(xiàn)admin權(quán)限為true了,那么admin的用戶(hù)就創(chuàng)建好了。
> show users
user admin
---- -----
root true
>
在配置文件啟用認(rèn)證
默認(rèn)情況下,influxdb的配置文件是禁用認(rèn)證策略的,所以需要修改設(shè)置一下。
編輯配置文件vim /etc/influxdb/influxdb.conf,把 [http] 下的 auth-enabled 選項(xiàng)設(shè)置為 true 。

配置完畢之后,重啟influxdb服務(wù)即可。
使用admin用戶(hù)登陸influxdb
在配置啟動(dòng)認(rèn)證以及重啟influxdb之后,如果不使用admin用戶(hù)登陸,則會(huì)報(bào)錯(cuò)如下:
root@e81b9a3b0eba:/# influx
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
>
> show users;
ERR: unable to parse authentication credentials
Warning: It is possible this error is due to not setting a database.
Please set a database with the command "use <database>".
>
這里已經(jīng)報(bào)錯(cuò)提示需要權(quán)限驗(yàn)證:ERR: unable to parse authentication credentials
這里使用admin認(rèn)證用戶(hù)登陸訪(fǎng)問(wèn)一下,格式如下:
influx -username '用戶(hù)名' -password '密碼'
執(zhí)行如下:
root@e81b9a3b0eba:/# influx -username 'root' -password 'newpwd'
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
>
> show users;
user admin
---- -----
root true
>
http帶上admin用戶(hù)查詢(xún)、寫(xiě)入數(shù)據(jù)
查詢(xún)數(shù)據(jù)
當(dāng)配置了admin認(rèn)證用戶(hù)之后,進(jìn)行http的api請(qǐng)求的時(shí)候就要帶上用戶(hù)名和密碼的參數(shù),不然無(wú)法執(zhí)行,示例如下:
curl -G "http://localhost:8086/query" -u username:password --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query" --data-urlencode "u=username" --data-urlencode "p=password" --data-urlencode "q=SHOW DATABASES"
curl -G "http://localhost:8086/query?u=username&p=password&q=SHOW+DATABASES"
執(zhí)行如下:
其中增加?pretty=true可以將json展開(kāi)顯示。
[root@locust03 ~]# curl -G "http://localhost:8086/query?pretty=true" -u root:newpwd --data-urlencode "q=show databases"
[root@locust03 ~]# curl -G "http://localhost:8086/query" -u root:newpwd --data-urlencode "q=show databases"
{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["_internal"]]}]}]}
[root@locust03 ~]#
[root@locust03 ~]# curl -G "http://localhost:8086/query" --data-urlencode "u=root" --data-urlencode "p=newpwd" --data-urlencode "q=show databases"
{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["_internal"]]}]}]}
[root@locust03 ~]#
[root@locust03 ~]# curl -G "http://localhost:8086/query?u=root&p=newpwd&q=show+databases"
{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["_internal"]]}]}]}
[root@locust03 ~]#
寫(xiě)入數(shù)據(jù)
當(dāng)然設(shè)置了用戶(hù)認(rèn)證之后,寫(xiě)入數(shù)據(jù)的時(shí)候也要帶上用戶(hù)名以及密碼&u=username&p=password,如下:
用戶(hù)名密碼寫(xiě)在URL中
curl -i -X POST "http://localhost:8086/write?db=mydb&u=username&p=password" --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
用戶(hù)名密碼寫(xiě)在HTTP頭Authorization選項(xiàng)
curl -i -X POST "http://localhost:8086/write?db=mydb" -u username:password --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
下面來(lái)執(zhí)行一下:
[root@server ~]# curl -i -X POST "http://localhost:8086/write?db=mydb&u=root&p=newpwd" --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 4b314fea-0d16-11ea-8017-0242ac110002
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.9
X-Request-Id: 4b314fea-0d16-11ea-8017-0242ac110002
Date: Fri, 22 Nov 2019 10:53:19 GMT
[root@server ~]#
[root@locust03 ~]# curl -i -X POST "http://localhost:8086/write?db=mydb" -u root:newpwd --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86"
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 6b2c10bb-0d16-11ea-8018-0242ac110002
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.9
X-Request-Id: 6b2c10bb-0d16-11ea-8018-0242ac110002
Date: Fri, 22 Nov 2019 10:54:12 GMT
[root@locust03 ~]#
查看插入后的influxdb數(shù)據(jù),如下:
> use mydb
Using database mydb
>
> show measurements;
name: measurements
name
----
cpu_load_short
>
> select * from cpu_load_short;
name: cpu_load_short
time host region value value2
---- ---- ------ ----- ------
1434055562000000000 server01 us-west 0.64 0.86
1574420052671401939 server01 us-west 0.64 0.86
>