SQL Server 2008 R2:限制服务器登录查看服务器上除 2 个数据库以外的所有数据库

SQL Server 2008 R2 : restrict server login from seeing all but 2 databases on server

在托管 SQL Server 2008 R2 的服务器上,我打开 SSMS,在安全 -> 登录名下有一个名为 "SomeLoginName" 的登录名。当我使用此登录名登录到服务器时,我能够看到服务器上的所有数据库。

我想限制此用户只能查看服务器上的 2 个数据库。我看到一些解决方案说要撤销登录的 VIEW ANY DATABASE 权限,然后将登录添加为我希望 "SomeLoginName" 能够看到的数据库的 db_owner。我不想将 "SomeLoginName" 作为它应该看到的数据库的 db_owner

有没有一种方法可以将 "SomeLoginName" 配置为仅在服务器上看到 2 个数据库,而 "SomeLoginName" 不是这 2 个数据库的 db_owner

提前致谢。

Is there a way that I can configure "SomeLoginName" to only see 2 databases on the server without "SomeLoginName" being the db_owner for these 2 databases?

不,因为你在 SQL Server 2008 R2 没有这样的方法。

SQL Server 2012 开始引入了新的 Contained Databases

这是另一篇有用的文章SQL Server 2012 Contained Database Feature

While looking through the new features and improvements in SQL Server 2012, we found a potentially interesting feature called Contained Databases. A contained database basically includes all database settings and the metadata within itself thereby resulting in no configuration dependencies on the instance of the SQL Server Database Engine where the database is actually installed. Users will be able to connect to a contained database without authenticating a login at the Database Engine level. This feature really helps to isolate the database from the Database Engine thereby making it possible to easily move the database from one instance of SQL Server to another. In this tip we will take a look at how to configure and use this feature of SQL Server 2012.

当使用 contained databases 时,您不需要 loginserver 级别的安全主体),只需要 user 数据库级别。对于 authenticate 您的用户,它将是 database,而不是 server。因此,这个 user 除了创建它的数据库之外不会 "see databases"。

这个用户不一定是db_owner,是有任何权限甚至没有任何权限的普通用户

感谢@sepupic,his/her答案正确。事实证明,我实际上是 运行 MS SQL Server 2012,所以我能够实现包含数据库的概念。不过,@sepupic 的回答中链接页面上列出的步骤对我不起作用。我找到 this 个并将此脚本放在一起。这是它的作用:

  1. 将 MS SQL 服务器实例的 'contained database authentication' 更改为 1
  2. 运行 RECONFIGURE
  3. 创建一个包含的数据库
  4. 为数据库创建一个用户

这是脚本:

USE master;

GO;

EXEC sp_configure 'contained database authentication', 1;

GO;

RECONFIGURE;

GO;

CREATE DATABASE ContainedDB2

CONTAINMENT = PARTIAL;

GO;

USE ContainedDB2;

GO;

CREATE USER cduser2

WITH PASSWORD = N'Pa$$word',

DEFAULT_SCHEMA = dbo;

GO;

然后您只需在以

开头的部分中配置与包含的数据库的连接

Login and Verify the User Permissions on a Contained Database

使用我放在一起的脚本并在我提到的部分下配置连接,这样您就可以使用创建的用户连接到服务器,并且该用户只能看到您想要的包含的数据库到。您必须将用户配置为在包含的数据库中拥有类似 db role db_datareader 的权限,但是如果您搜索它们,就很容易找到有关如何执行这些类型操作的说明。再次感谢@sepupic 让我开始想出答案。