2017-03-06 302 views
1

對於我正在使用Azure SQL的項目,在Web應用程序中的使用我創建了一個用戶'justread',該用戶被授予select - 訪問數據庫中的某個模式。Azure SQL Server - 如何授予用戶只讀訪問主數據庫的權限

我這樣做的傳統方式,在2個步驟:

  1. 在主數據庫

    if exists(select * from sys.database_principals where name = N'justread') 
        begin 
        drop user [justread] 
        drop login [justread] 
        end 
    go 
    if not exists (select * from sys.database_principals where name = N'justread') 
        begin 
        create login [justread] with password= N'123456' 
        end 
    go 
    
  2. 在應用程序數據庫

    create user [justread] for login [justread] with default_schema=[dbo] 
    go 
    grant select on schema::dbo to [justread] 
    go 
    

在應用程序數據庫但是,我收到了一個允許同一用戶只讀訪問主數據庫的請求。

不要問我爲什麼...

我第一個通過賦予訪問相同的是和我一樣的應用程序數據庫上試了一下:

create user [justread] for login [justread] with default_schema=[dbo] 
go 
grant select on schema::dbo to [justread] 
go 

第一個查詢工作,沒有問題。但是第二次失敗,出現以下錯誤:

Msg 15151, Level 16, State 1, Line 5 
Cannot find the schema 'dbo', because it does not exist or you do not have permission. 

我試着用搜索引擎,發現它是如何與一個經典的SQL服務器完成的,但我也到了那裏的錯誤消息:

grant view server state to [justread] 
go 

Msg 40520, Level 16, State 1, Line 4 
Securable class 'server' not supported in this version of SQL Server. 

我如何完成這件事?

回答

0

首先,你是否想在master數據庫中創建表並授予這些表的'剛讀'用戶權限?如果是這種情況,我們無法在Azure SQL的master數據庫中創建表。 Azure SQL中的主數據庫是隻讀的,它用於存放有關角色,登錄等信息。

但是,如果要授予Azure SQL中master數據庫系統表上的'justread'用戶權限,只需在master數據庫中執行以下腳本。我測試過程中,只要我們執行下面的腳本,剛讀取的用戶就可以訪問master數據庫的系統表。

create user [justread] for login [justread] with default_schema=[dbo] 
Go 

其次,按本official article,查看服務器狀態目前不是在SQL Azure中支持的概念,因此在運行下面的腳本時收到錯誤消息。

grant view server state to [justread] 
go 
+0

這似乎是伎倆。謝謝。 :-) – Fysicus

0

用戶[justread]是爲了在我的應用程序的數據庫上執行簡單的數據查詢而創建的。由於我未知的原因,客戶端還需要該用戶對「主」數據庫擁有隻讀訪問權限。

使用以下查詢我設法給[只讀]所需的訪問權限,因此我可以在master數據庫上執行查詢。

create user [justread] for login [justread] with default_schema=[dbo] 
 
Go

一些表仍然似乎是遙不可及的,雖然如以下錯誤消息似乎表明:

enter image description here

就目前而言,這可能不過足夠了。

相關問題