天天看點

Greenplum:超級管理者權限被删除如何恢複

問題

由于一次誤操作,将gpadmin的superuser權限被撤銷了,現在建立資料庫或者其他使用者等指令都是無法執行。

實驗

檢視pg_authid相關檔案

postgres=# \d+ pg_authid;
                           Table "pg_catalog.pg_authid"
      Column       |           Type           | Modifiers | Storage  | Description 
-------------------+--------------------------+-----------+----------+-------------
 rolname           | name                     | not null  | plain    | 
 rolsuper          | boolean                  | not null  | plain    | 
 rolinherit        | boolean                  | not null  | plain    | 
 rolcreaterole     | boolean                  | not null  | plain    | 
 rolcreatedb       | boolean                  | not null  | plain    | 
 rolcatupdate      | boolean                  | not null  | plain    | 
 rolcanlogin       | boolean                  | not null  | plain    | 
 rolconnlimit      | integer                  | not null  | plain    | 
 rolpassword       | text                     |           | extended | 
 rolvaliduntil     | timestamp with time zone |           | plain    | 
 rolconfig         | text[]                   |           | extended | 
 rolresqueue       | oid                      |           | plain    | 
 rolcreaterextgpfd | boolean                  |           | plain    | 
 rolcreaterexthttp | boolean                  |           | plain    | 
 rolcreatewextgpfd | boolean                  |           | plain    | 
 rolcreaterexthdfs | boolean                  |           | plain    | 
 rolcreatewexthdfs | boolean                  |           | plain    | 
Indexes:
    "pg_authid_oid_index" UNIQUE, btree (oid), tablespace "pg_global"
    "pg_authid_rolname_index" UNIQUE, btree (rolname), tablespace "pg_global"
    "pg_authid_rolresqueue_index" btree (rolresqueue), tablespace "pg_global"
Triggers:
    pg_sync_pg_authid AFTER INSERT OR DELETE OR UPDATE ON pg_authid FOR EACH STATEMENT EXECUTE PROCEDURE fl
atfile_update_trigger()
Has OIDs: yes

Tablespace: "pg_global"

postgres=# select oid,relfilenode from pg_class where relname='pg_authid_oid_index';
 oid  | relfilenode 
------+-------------
 2677 |        2677
(1 row)

postgres=# select oid,relfilenode from pg_class where relname='pg_authid_rolname_index';
 oid  | relfilenode 
------+-------------
 2676 |        2676
(1 row)

postgres=# select oid,relfilenode from pg_class where relname='pg_authid_rolresqueue_index';
 oid  | relfilenode 
------+-------------
 6029 |        6029
(1 row)

postgres=# select oid,relfilenode from pg_class where relname='pg_authid';
 oid  | relfilenode 
------+-------------
 1260 |        1260
(1 row)
           

在備份環境下(最好按照生産環境重新建立一份)建立出來足夠的使用者

​ 模拟建立使用者,要求使用者名和密碼一緻才可以(尤其是gpadmin預設密碼)

postgres=# create role only_select login password '123456';
NOTICE:  resource queue required -- using default resource queue "pg_default"
CREATE ROLE           

檢查pg_authid的表和索引檔案路徑并将測試環境下相關路徑檔案拷貝到生産伺服器上

​ $DATA/global/1260

​ $DATA/global/6029

​ $DATA/global/2676

​ $DATA/global/2677

注意替換時這些檔案盡量備份

模拟誤操作

postgres=# alter role gpadmin  nosuperuser;
ALTER ROLE

postgres=# select * from pg_authid where rolname='gpadmin';                                                
-[ RECORD 1 ]-----+------------------------------------
rolname           | gpadmin
rolsuper          | f
rolinherit        | t
rolcreaterole     | t
rolcreatedb       | t
rolcatupdate      | f
rolcanlogin       | t
rolconnlimit      | -1
rolpassword       | md5b44a9b06d576a0b083cd60e5f875cf48
rolvaliduntil     | 
rolconfig         | 
rolresqueue       | 6055
rolcreaterextgpfd | t
rolcreaterexthttp | t
rolcreatewextgpfd | t
rolcreaterexthdfs | t
rolcreatewexthdfs | t
           

關閉資料庫

​ 在關閉資料庫前去各個正常的segment的節點上執行指令

$  PGOPTIONS='-c gp_session_role=utility' psql -d postgres -p 6001
select oid,relfilenode from pg_class where relname='pg_authid_oid_index';(都要看看)           
$ gpstop -af           

替換所有的資料目錄的global下對應的oid名

​ 首先替換的檔案要備份出來,防止誤操作

​ 其次最好檢查一下每個資料節點是否存在這幾個檔案其次這幾個檔案是否屬于這個表oid檔案

最後結果

gpstart -a

postgres=# \du
                        List of roles
  Role name  |            Attributes             | Member of 
-------------+-----------------------------------+-----------
 gpadmin     | Superuser, Create role, Create DB | 
 only_select |                                   | 
           

實驗2

将superuser權限取消

postgres=# alter role gpadmin  nosuperuser;
ALTER ROLE           

正常關閉資料庫

$ gpstop -af           

指令解析

/data2/install-gpdb6/bin/postgres --single -P -O -D /data2/gpdb6/gpAux/gpdemo/datadirs/qddir/demoDataDir-1 -p 6000   -c gp_session_role=utility postgres           

/data2/install-gpdb6/bin/postgres

資料庫安裝環境位址

/data2/gpdb6/gpAux/gpdemo/datadirs/qddir/demoDataDir-1

資料庫資料檔案位址

-p 6000

這是資料庫啟動的端口号(如果不清楚,可以檢視/data2/gpdb6/gpAux/gpdemo/datadirs/qddir/demoDataDir-1的postmaster.opts)

postgres

這裡指的是postgres資料庫(預設pg,gp都有該資料庫)

進入類似postgresql單使用者模式并修改權限指令

​ 假設1個資料庫有1個主節點(master),2個資料節點(slave1,slave2),每個資料節點有2個primary(primary1,primary2),2個mirror(mirror1,mirro2)

​ 就要找到各個segment的postmaster.opts(裡面有執行的資料檔案位址和端口号),然後進入類似postgresql單使用者模式

$ /data/greenplum/greenplum-db-4.3.7.2/bin/postgres --single -P -O -D /data/primary2/gpseg1/ -p 40001 -c gp_session_role=utility postgres
PostgreSQL stand-alone backend 8.2.15
backend> alter user gpadmin superuser 
           

​ 使用快捷鍵

CTRL+D

就可以退出環境了

​ 後期這裡可以驗證一下xmin,xmax是否發生變化

​ 這樣的話就要前後執行9遍等執行結束後

啟動資料庫

$ gpstart -a
....           

小結

兩者的思路不同,前者是通過檔案替換,後者通過單使用者指令模式,相較而言,後者更适合實戰