postgresql数据库监控实验06-influxdb用户配置

postgresql数据库监控实验06-influxdb用户配置

环境

机器:
移动云的
10.176.140.72 plat-ecloud01-mgmt-monitor04 monitor04

操作系统:
CentOS Linux release 7.3.1611 (Core)

InfluxDB版本:
influxdb-1.7.9

权限说明

1.管理员用户(admin user)

管理员用户拥有所有数据库的read和write权限,并拥有以下语句权限:

  • 创建删除数据库:CREATE DATABASE, and DROP DATABASE
  • 删除数据集合和表:DROP SERIES and DROP MEASUREMENT
  • 创建,修改,删除保留策略:CREATE RETENTION POLICY, ALTER RETENTION POLICY, and DROP RETENTION POLICY
  • 创建删除连续查询:CREATE CONTINUOUS QUERY and DROP CONTINUOUS QUERY

2.非管理员用户(non-admin user)

非管理员用户可拥有管理员授予的指定数据库的readwrite,或all(all=read+write)权限。

用户管理命令

1.用户命令

创建管理员用户:

CREATE USER admin WITH PASSWORD '' WITH ALL PRIVILEGES
如:CREATE USER paul WITH PASSWORD 'timeseries4days' WITH ALL PRIVILEGES

特别的是,influxdb中创建用户的命令是等幂(idempotent),例如,重复创建相同用户是不会报错的:

> CREATE USER admin WITH PASSWORD 'oracle' WITH ALL PRIVILEGES
> show users
user     admin
----     -----
telegraf false
admin    true
> CREATE USER admin WITH PASSWORD 'oracle' WITH ALL PRIVILEGES

但是如果任何一个值更改,都会报错:

> CREATE USER admin WITH PASSWORD 'oracle123' WITH ALL PRIVILEGES
ERR: user already exists
Warning: It is possible this error is due to not setting a database.
Please set a database with the command "use ".
> CREATE USER admin WITH PASSWORD 'oracle123'
ERR: user already exists
Warning: It is possible this error is due to not setting a database.
Please set a database with the command "use ".

创建非管理员用户:

CREATE USER <username> WITH PASSWORD ''
如:CREATE USER "rachel_smith" WITH PASSWORD 'asdf1234!'

删除用户:

DROP USER <username>
如:DROP USER "todd"

2.权限命令

查看所有用户权限:

SHOW USERS

查看指定用户权限:

SHOW GRANTS FOR <user_name>
如:SHOW GRANTS FOR "todd"

授予权限给一个已存在的用户(使用管理员用户):

GRANT ALL PRIVILEGES TO <username>
如:GRANT ALL PRIVILEGES TO "todd"

授予权限给一个已存在的用户(使用非管理员用户):

GRANT [READ,WRITE,ALL] ON <database_name> TO <username>
如:GRANT READ ON "NOAA_water_database" TO "todd"

撤销用户权限(使用管理员用户):

REVOKE ALL PRIVILEGES FROM <username>
如:REVOKE ALL PRIVILEGES FROM "todd"

撤销用户权限(使用非管理员用户):

REVOKE [READ,WRITE,ALL] ON <database_name> FROM <username>
如:REVOKE ALL ON "NOAA_water_database" FROM "todd"

3.密码管理命令

重设用户密码:

SET PASSWORD FOR <username> = ''
如:SET PASSWORD FOR "todd" = 'influxdb4ever'

身份验证配置

1.创建admin用户

influxdb初始安装后是没有用户的,所以要开启身份验证需要先创建一个可用的管理员用户,否则。。就没办法连到influxdb了。

创建用户admin并授予管理员权限:

[root@localhost ~]# influx -precision rfc3339
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
> CREATE USER admin WITH PASSWORD 'oracle' WITH ALL PRIVILEGES
> show users
user     admin
----     -----
telegraf false
admin    true

2.修改influxdb配置文件

默认情况下,配置文件中是关闭了身份验证的,需要修改配置文件并重启influxdb:
将http配置中的auth-enabled改为true

[root@localhost ~]# vim /etc/influxdb/influxdb.conf

###
### [http]
###
### Controls how the HTTP endpoints are configured. These are the primary
### mechanism for getting data into and out of InfluxDB.
###

[http]

  # Determines whether user authentication is enabled over HTTP/HTTPS.
  auth-enabled = true

重启influxd服务:

[root@localhost ~]# systemctl restart influxd

3.验证

登录验证:
再次尝试不输入用户名,密码登录:

[root@localhost ~]# influx -precision rfc3339
Failed to connect to http://localhost:8086: Get http://localhost:8086/ping: dial tcp [::1]:8086: connect: connection refused
Please check your connection settings and ensure 'influxd' is running.

无法登陆

再试试使用admin用户登录:

[root@localhost ~]# influx -precision rfc3339 -username admin -password oracle
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
> 

可以正常登录

权限验证:
查看管理员用户能看到的database:

> show databases
name: databases
name
----
_internal
telegraf
t
tt

创建新用户并授予t库的read权限:

> CREATE USER "t" WITH PASSWORD 't'
> GRANT READ ON "t" TO "t"
> > SHOW GRANTS FOR "t"
database privilege
-------- ---------
t        READ

验证t用户权限,使用t登录,他只能看到拥有权限的库:

[root@localhost ~]# influx -precision rfc3339 -username t -password t
Connected to http://localhost:8086 version 1.7.9
InfluxDB shell version: 1.7.9
> show databases
name: databases
name
----
t

验证read权限

> use t
Using database t
> show measurements
name: measurements
name
----
disk_free
test_measurement
> select * from test_measurement limit 3
name: test_measurement
time                           host     user value
----                           ----     ---- -----
2019-12-05T06:03:48.392809347Z server01 root 1
2019-12-05T06:03:49.681200028Z server01 root 1
2019-12-05T06:03:50.13881617Z  server01 root 1

可以正常读数据。

验证write权限:

> insert test_measurement,host=server01,user=root value=3
ERR: {"error":"\"t\" user is not authorized to write to database \"t\""}

无write权限

你可能感兴趣的:(监控)