gpt4 book ai didi

azure - 将 Azure 数据库配置为只读

转载 作者:行者123 更新时间:2023-12-02 07:07:54 25 4
gpt4 key购买 nike

有没有办法将 Azure sql 数据库临时配置为只读。在我尝试创建只读用户时,我运行了以下命令

CREATE LOGIN reader WITH password='****' -- successful
CREATE USER readerUser FROM LOGIN reader; -- successful
EXEC sp_addrolemember 'db_datareader', 'readerUser'; -- failed

最后一条语句因错误而失败

Cannot alter the role 'db_datareader', because it does not exist or you do not have permission.

最佳答案

如果您确实想使用上面提到的命令添加只读用户(以便您可以以只读模式或读写模式进行连接,具体取决于指定的连接用户),这里有一个解释当您执行上述命令时可能出现的问题。

CREATE LOGIN reader WITH password='****' -- successful
CREATE USER readerUser FROM LOGIN reader; -- successful
EXEC sp_addrolemember 'db_datareader', 'readerUser'; -- failed

Cannot alter the role 'db_datareader', because it does not exist or you do not have permission.

当我直接在主数据库上执行您提到的所有 3 个命令时,我能够重现相同的错误

理想情况下,要创建只读用户,您必须使用 master 数据库创建登录名,以便它是服务器级别登录名,然后用户创建和角色成员添加将在您打算使用的数据数据库上进行。

-- Run this command on Master Database
CREATE LOGIN reader WITH password='****'

-- Run these commands on data Database
CREATE USER readerUser FROM LOGIN reader;
EXEC sp_addrolemember 'db_datareader', 'readerUser';

-- Optionally, run an extra command to create the same user on Master database. This command is not required if you connect only from code/api's, but if you want to connect to your database using SQL Management Studio, then you need this one.
CREATE USER readerUser FROM LOGIN reader; -- run this on Master Database again

关于azure - 将 Azure 数据库配置为只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com