在Azure SQL数据库上设置RESTRICTED_USER

时间:2017-04-11 12:43:24

标签: sql-server database azure permissions

我试图将Azure SQL数据库设置为RESTRICTED_USER模式,面对奇怪的错误,我无法弄清楚自己。

Msg 5011, Level 14, State 2, Line 1
User does not have permission to alter database 'YourDB', the database does not exist, or the database is not in a state that allows access checks.

根据数据库db_owner中的documentation成员身份,角色应该足够了。用户还获得ALTER DATABASE权限。

  

RESTRICTED_USER仅允许db_owner固定数据库的成员   角色和dbcreator以及sysadmin固定服务器角色连接到   数据库,但不限制其数量。所有连接到   数据库在指定的时间范围内断开连接   ALTER DATABASE语句的终止子句。数据库之后   已转换到RESTRICTED_USER状态,连接尝试次数   不合格的用户被拒绝。

https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-set-options

制作数据库RESTRICTED_USER还需要什么?

重现的代码:

CREATE LOGIN YourLogin WITH PASSWORD = 'password' 
GO

-- USE YourDB;
-- switch to YourDB here, Azure SQL does not support USE statement

CREATE USER YourUser
    FOR LOGIN YourLogin
    WITH DEFAULT_SCHEMA = dbo

GRANT ALTER TO YourDB

EXEC sp_addrolemember N'db_owner', N'YourUser'

然后,以下声明失败。

ALTER DATABASE YourDB SET RESTRICTED_USER;

1 个答案:

答案 0 :(得分:1)

我尝试重新解决此问题,只有sysadmin可以将数据库状态更改为restricted_user。

您还提到文档说明membership in database db_owner role should be enough

但文档说明,当数据库设置为restricted_user模式时,可以连接到数据库的角色

  

RESTRICTED_USER仅允许db_owner 固定数据库角色的成员以及dbcreator和sysadmin固定服务器角色连接到数据库,但不限制其数量

总之,具有上述角色的用户将能够连接到数据库,当它被设置为受限用户时,它没有说明,dbmanager可以将数据库的状态更改为受限用户