对于我正在使用 Azure SQL 的项目,为了在 Web 应用程序中使用,我创建了一个用户“justread”,该用户被授予对数据库中某个架构的选择访问权限。
我以经典方式完成此操作,分两步:
在主数据库上
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
在应用程序数据库上
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/