2. 权限管理
Greenplum 数据库 权限管理 通过 角色(role) 来进行。 Role 概念包括 User 和 Group。 一个 Role 可以是一个 DB 的用户或者一个 Group,或者两者兼备。
Role 可以拥有 DB 对象(tabel),并可以分配改对象的权限给其他 Rolde。
注意:使用psql登陆后显示的提示符是 数据库=#
的是超级管理员,其他用户为 数据库=>
。
2.1 权限规划
在权限规划上应该区分 DBA,app,guest,超级管理员。
这里将举例说明:
例子中将角色划分为4类(使用下划线分隔):
- gpdba, gpdba_xxx, ...
普通数据库管理员,执行数据库的创建,删除,表维护等等相关操作。
- gpapp, gpapp_xxx, ...
普通的应用程序访问,即:通过 ODBC 或 JDBC 来访问的用户都归为此类。
- gpguest, gpguest_xxx, ...
临时的访问客户,开放给外部(其他公司,外部临时用户)的账户,一般来说只有查看数据表权限,并且应该对访问时段进行控制。
- gpadmin
超级用户。
2.2 规划实施
-- 更新超级管理员密码
alter role gpadmin with password 'gpadmin123456';
-- 创建角色 dba 角色,有增加数据库权限和管理其他角色权限
-- create role gpdba with login;
-- alter role gpdba with password 'gp123456';
-- alter role gpdba with createdb;
-- alter role gpdba with createrole;
create role gpdba with login password 'gp123456' createdb createrole;
-- 创建角色 gpapp 角色
-- create role gpapp with login;
-- alter role gpapp with password 'gp123456';
create role gpapp with login password 'gp123456';
-- 创建角色 gpguest 角色
#create role gpguest with login;
#alter role gpguest with password 'gp123456';
#alter role gpguest with noinherit;
create role gpguest with login password 'gp123456' noinherit;
-- 将 gpapp 给具体的用户
create role gpapp_scott with login password 'gp123456';
create role gpapp_lucy with login password 'gp123456';
grant gpapp to gpapp_scott, gpapp_lucy;
revoke gpapp from gpapp_lucy;
drop role gpapp_lucy;
grant all on database gpdb1 to gpapp;
--grant all on table xxx to gpapp;
-- 更细的权限控制通过 grant 和 revoke
create role gpguest_scott with login password 'gp123456';
grant gpguest to gpguest_scott;
grant connect on database gpdb1 to gpguest;
--grant select on table xxx to gpguest;
-- 这里还可以添加时间访问限制,在周六日,不可访问,平时在22:00-06:00不可访问
--alter role gpguest deny
--between day 0 time '00:00' and day 0 time '24:00'
--between day 6 time '00:00' and day 6 time '24:00'
--between day 1 time '00:00' and day 1 time '06:00'
--between day 1 time '22:00' and day 1 time '24:00'
--between day 1 time '00:00' and day 2 time '06:00'
--between day 1 time '22:00' and day 2 time '24:00'
--between day 1 time '00:00' and day 3 time '06:00'
--between day 1 time '22:00' and day 3 time '24:00'
--between day 1 time '00:00' and day 4 time '06:00'
--between day 1 time '22:00' and day 4 time '24:00'
--between day 1 time '00:00' and day 5 time '06:00'
--between day 1 time '22:00' and day 5 time '24:00';
-- 删除访问时间约束
--alter role gpguest drop deny for day 0;
--alter role gpguest drop deny for day 1;
--alter role gpguest drop deny for day 2;
--alter role gpguest drop deny for day 3;
--alter role gpguest drop deny for day 4;
--alter role gpguest drop deny for day 5;
--alter role gpguest drop deny for day 6;
查看 role
\dg
3. 配置客户端认证
3.1 允许连接到 gpdb
客户端访问认证是通过一个叫做 pg_hba.conf
(也是标准的 PostgreSQL 的认证文件)的文件来控制的。
一般只需要修改 主节点(mdw)上 的 pg_hda.conf
。修改访问认证基本基本只需如下设置即可:
nano $MASTER_DATA_DIRECTORY/pg_hba.conf
增加
host all all 10.20.17.0/24 md5
需要注意的是,这个 IP 段是 您的 服务器所在的 IP 段。
重新加载 pg_hba.conf
文件:
$ gpstop -u
3.2 并发连接数设置
要使此配置生效,不仅要修改主节点上的 postgresql.conf
, 也需要修改每个segment instance (段实例)上的postgresql.conf
。
在$MASTER_DATA_DIRECTORY/postgresql.conf(包括 Standby):
max_connections=100
max_prepared_transactions=100
在所有的 Instance 上 SEGMENT_DATA_DIRECTORY/postgresql.conf:
max_connections=500
max_prepared_transactions=100
segment instance (段实例)上的 max_connections
为主节点上的 5-10 倍,最低也应该在 2-3 倍。
修改步骤:
- 1 停止 gpdb
$ gpstop
-
2 在 Master 上编辑$MASTER_DATA_DIRECTORY/postgresql.conf 文件并修改下面两个参数: max_connections (允许连接的数量+superuser_reserved_connections) max_prepared_transactions (大于或等于max_connections)
-
3 在每个 Instance 上编辑 SEGMENT_DATA_DIRECTORY/postgresql.conf 文件并修改下面两个参数: max_connections (建议5-10倍Master上的值) max_prepared_transactions (大于或等于Master上的值)
-
4 重启 GPDB 系统
$ gpstart