無論是yum安裝,亦或者是docker安裝的方式,influxdb默認安裝完畢以後,並不會自帶用戶認證的功能,直接就能夠進行訪問的了。 狀況以下:docker
root@d248ddfcd76c:/# influx
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
>
複製代碼
或者curl直接查詢出來數據,不須要用戶認證,以下:shell
[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 ~]#
複製代碼
對於這種不設置防備的措施,整體來講,不是很好。json
因此,下面來看看怎麼設置用戶認證。vim
基本命令:api
# 顯示用戶
SHOW USERS
# 建立用戶
CREATE USER "username" WITH PASSWORD 'password'
# 賦予用戶管理員權限
GRANT ALL PRIVILEGES TO username
# 建立管理員權限的用戶
CREATE USER <username> WITH PASSWORD '<password>' WITH ALL PRIVILEGES
# 修改用戶密碼
SET PASSWORD FOR username = 'password'
# 撤消權限
REVOKE ALL ON mydb FROM username
# 查看權限
SHOW GRANTS FOR username
# 刪除用戶
DROP USER "username"
複製代碼
實際操做以下:bash
# 查看全部用戶
> show users
user admin
---- -----
>
# 建立一個root用戶,設置密碼爲newpwd,主要不要使用雙引號" 括起來,否則會報錯
> create user "root" with password 'newpwd'
>
# 再次查看用戶信息,發現admin爲false,說明還要設置一下權限。
> show users
user admin
---- -----
root false
>
# 刪除root用戶
> drop user root
>
> show users
user admin
---- -----
>
# 從新設置root用戶,並設置帶上全部權限
> create user "root" with password 'newpwd' with all privileges
>
# 發現admin權限爲true了,那麼admin的用戶就建立好了。
> show users
user admin
---- -----
root true
>
複製代碼
默認狀況下,influxdb的配置文件是禁用認證策略的,因此須要修改設置一下。app
編輯配置文件vim /etc/influxdb/influxdb.conf
,把 [http]
下的 auth-enabled
選項設置爲 true
。curl
配置完畢以後,重啓influxdb服務便可。ui
在配置啓動認證以及重啓influxdb以後,若是不使用admin用戶登錄,則會報錯以下:this
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>".
>
複製代碼
這裏已經報錯提示須要權限驗證:ERR: unable to parse authentication credentials
這裏使用admin認證用戶登錄訪問一下,格式以下:
influx -username '用戶名' -password '密碼'
複製代碼
執行以下:
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
>
複製代碼
當配置了admin認證用戶以後,進行http的api請求的時候就要帶上用戶名和密碼的參數,否則沒法執行,示例以下:
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"
複製代碼
執行以下: 其中增長?pretty=true
能夠將json展開顯示。
[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 ~]#
複製代碼
固然設置了用戶認證以後,寫入數據的時候也要帶上用戶名以及密碼&u=username&p=password
,以下:
用戶名密碼寫在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"
複製代碼
用戶名密碼寫在HTTP頭Authorization選項
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"
複製代碼
下面來執行一下:
[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數據,以下:
> 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
>
複製代碼