为什么我需要db_owner才能打开一个连接
Why do I need db_owner to open a connection
我创建了一个 Sql 服务器数据库(通过 Visual Studio),并在其中创建了数据库中的用户。我为该用户赋予了 db_datareader 和 db_datawriter 角色。
当我尝试打开连接时出现以下异常。如果我将用户添加到 db_owner 角色,那么它就可以工作。为什么? db_datareader 不应该足以打开连接吗?
System.Data.SqlClient.SqlException
HResult=0x80131904
Message=A connection was successfully established with the server, but then an error occurred during the login process. (provider: Named Pipes Provider, error: 0 - No process is on the other end of the pipe.)
Source=.Net SqlClient Data Provider
StackTrace:
at LicenseLibrary.Database.AzureDataAccess.<>c__DisplayClass15_0.<ExecuteAction>b__0() in C:\git\Store\LicenseLibrary\Database\AzureDataAccess.cs:line 150
Inner Exception 1:
Win32Exception: No process is on the other end of the pipe
更新:
我正在使用 VisualStudio 运行以创建数据库的许多 .sql 脚本创建数据库。为此的是(删除了 "GO;" 的行:
CREATE USER [readwrite] WITH PASSWORD = N'**************';
CREATE USER [readonly] WITH PASSWORD = N'************';
ALTER ROLE [db_datareader] ADD MEMBER [readonly];
ALTER ROLE [db_datareader] ADD MEMBER [readwrite];
ALTER ROLE [db_datawriter] ADD MEMBER [readwrite];
此外,在它想要的角色上失败是一个奇怪的例外 - 说没有流程。
我在 TestDatabaseRoles.zip 中有一个示例 - 3 行代码即可实现(也需要 zip 中的数据库)。
首先,作为背景,该错误消息是一条通用的连接失败消息,出于安全原因,其详细信息不向客户端公开。您需要查看 SQL 日志以查看真正的错误。
readwrite
是包含的数据库用户,因此很多事情都可能失败。
SQL 日志中的登录失败消息类似于:
Login failed for user 'readwrite'. Reason: Could not find a login
matching the name provided. [CLIENT: <named pipe>
]
如果未配置包含的数据库身份验证,或者
Login failed for user 'readwrite'. Reason: Failed to open the specified database. [CLIENT: <local machine>
]
如果数据库名称错误或用户缺少 CONNECT 权限,或
Login failed for user 'readwrite'. Reason: Password did not match that for the user provided. [Database: 'Database1'] [CLIENT: <local machine>
]
如果密码不正确。
总而言之,连接成功必须满足以下所有条件:
1) 实例必须包含已启用的数据库身份验证,
exec sp_configure 'contained database authentication', 1;
GO
RECONFIGURE ;
2) 数据库必须设置为partial
包含。
alter database Database1 set containment = partial
3) 用户必须拥有 CONNECT 权限(db_owner 已经拥有)。
grant connect to readwrite
我创建了一个 Sql 服务器数据库(通过 Visual Studio),并在其中创建了数据库中的用户。我为该用户赋予了 db_datareader 和 db_datawriter 角色。
当我尝试打开连接时出现以下异常。如果我将用户添加到 db_owner 角色,那么它就可以工作。为什么? db_datareader 不应该足以打开连接吗?
System.Data.SqlClient.SqlException
HResult=0x80131904
Message=A connection was successfully established with the server, but then an error occurred during the login process. (provider: Named Pipes Provider, error: 0 - No process is on the other end of the pipe.)
Source=.Net SqlClient Data Provider
StackTrace:
at LicenseLibrary.Database.AzureDataAccess.<>c__DisplayClass15_0.<ExecuteAction>b__0() in C:\git\Store\LicenseLibrary\Database\AzureDataAccess.cs:line 150
Inner Exception 1:
Win32Exception: No process is on the other end of the pipe
更新: 我正在使用 VisualStudio 运行以创建数据库的许多 .sql 脚本创建数据库。为此的是(删除了 "GO;" 的行:
CREATE USER [readwrite] WITH PASSWORD = N'**************';
CREATE USER [readonly] WITH PASSWORD = N'************';
ALTER ROLE [db_datareader] ADD MEMBER [readonly];
ALTER ROLE [db_datareader] ADD MEMBER [readwrite];
ALTER ROLE [db_datawriter] ADD MEMBER [readwrite];
此外,在它想要的角色上失败是一个奇怪的例外 - 说没有流程。
我在 TestDatabaseRoles.zip 中有一个示例 - 3 行代码即可实现(也需要 zip 中的数据库)。
首先,作为背景,该错误消息是一条通用的连接失败消息,出于安全原因,其详细信息不向客户端公开。您需要查看 SQL 日志以查看真正的错误。
readwrite
是包含的数据库用户,因此很多事情都可能失败。
SQL 日志中的登录失败消息类似于:
Login failed for user 'readwrite'. Reason: Could not find a login matching the name provided. [CLIENT:
<named pipe>
]
如果未配置包含的数据库身份验证,或者
Login failed for user 'readwrite'. Reason: Failed to open the specified database. [CLIENT:
<local machine>
]
如果数据库名称错误或用户缺少 CONNECT 权限,或
Login failed for user 'readwrite'. Reason: Password did not match that for the user provided. [Database: 'Database1'] [CLIENT:
<local machine>
]
如果密码不正确。
总而言之,连接成功必须满足以下所有条件:
1) 实例必须包含已启用的数据库身份验证,
exec sp_configure 'contained database authentication', 1;
GO
RECONFIGURE ;
2) 数据库必须设置为partial
包含。
alter database Database1 set containment = partial
3) 用户必须拥有 CONNECT 权限(db_owner 已经拥有)。
grant connect to readwrite