sql-server - Azure SQL Server - 如何授予用户对主数据库的只读访问权限

标签 sql-server azure azure-sql-database

对于我正在使用 Azure SQL 的项目,为了在 Web 应用程序中使用,我创建了一个用户“justread”,该用户被授予对数据库中某个架构的选择访问权限。

我以经典方式完成此操作,分两步:

  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 Server 完成的,但我也收到了一条错误消息:

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.

我该如何完成这个任务?

最佳答案

首先,您要在master数据库中创建表并授予“justread”用户对这些表的权限吗?如果是这种情况,我们将无法在 Azure SQL 的 master 数据库中创建表。 Azure SQL 中的主数据库是只读的,它保存有关角色、登录名等的信息。

但是,如果您想授予 Azure SQL 中 master 数据库系统表的“justread”用户权限,只需在 master 数据库中执行以下脚本即可。我测试了这个过程,一旦我们执行下面的脚本,justread用户就可以访问master数据库的系统表。

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

其次,根据此official article ,“查看服务器状态”当前不是 Azure SQL 中支持的概念,因此在运行以下脚本时会收到错误消息。

 grant view server state to [justread]
go

关于sql-server - Azure SQL Server - 如何授予用户对主数据库的只读访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42631907/

相关文章:

sql - 如何获取上一行值

ios - 在 Xamarin 上推送通知的 Web API - 流程是什么?

Azure SQL 性能

sql-server - 在所有数据库上授予相同的数据库角色 SQL Server 2008 R2

sql-server - 如何在 Linq to Entities 中进行多个内部联接

sql-server - 如何从 Visual Basic 6 连接到 Sql Server 2008 数据库?

Azure API 应用程序问题 - 身份验证

azure - Synapse 自动转换 ISO 日期字符串参数 yyyy-mm-ddThh :mm:ss into mm/dd/yyyy hh:mm:ss

sql-server - 从用于 Multi-Tenancy 的单一数据库/单一模式数据库架构开始

t-sql - Azure SQL Server 中的模糊搜索