sql server 中隐藏掉无关数据库

先贴上我实际测试的效果

Problem

I have a SQL Server instance that has hundreds of databases.  Navigating the database tree in SSMS is a pain and I was wondering if there was a way to limit the list of databases that I see in SSMS?

Solution

SQL Server consolidation is becoming more popular these days to reduce costs and therefore more and more databases are being put on one instance. It is very common to host multiple databases on a consolidated instance from multiple applications and departments and sometimes application owners want to hide their databases to other users of the instance. They do not want to make their database visible to others. This tip will give you an understanding on how databases can be hidden.

Setup

Suppose there are two databases A and B from two different applications and they are hosted on the same SQL Server instance. The users of database A are not allowed to see database B and vice versa. Here we will create two different logins user_A and user_B and give them appropriate rights to their own databases.

CREATE DATABASE A
GO
CREATE DATABASE B
GO
CREATE LOGIN user_A with password=‘[email protected]‘
Go
CREATE LOGIN user_B with password=‘[email protected]‘
Go
USE A
GO
CREATE USER user_A for login user_A;
GO
EXEC sp_addrolemember ‘db_owner‘, ‘user_A‘
GO
USE B
GO
CREATE USER user_B for login user_B
GO
EXEC sp_addrolemember ‘db_owner‘, ‘user_B‘

NOTE:-DO NOT MAKE CHANGES IN PRODUCTION WITHOUT PROPER TESTINGS IN LOWER-LIFE CYCLE ENVIRNOMENTS

Hiding all user databases for all logins

Suppose you want to hide all databases for all logins. Generally we hide our databases for security purposes. We can run the below statements to hide all databases for all logins. The databases will then only be visible to sysadmin logins or owners of the database.

USE MASTER
GO
DENY VIEW ANY DATABASE TO PUBLIC
GO

Once you run the above statement, you will not be able to see any databases in SQL Server Management Studio unless you are a sysadmin or your login is the owner of a database(s).

Here you can see in the below screen shot, I have connected using logins user_A and user_B and none of the user databases are showing after running the Deny View access to public.

Only sysadmins and database owners can see databases

To allow the logins to see their databases, I will make both logins the owners for their respective databases. User_A will be owner of database A and user_B will be the owner of database B. Run the below statements to change the database owners.

USE A
GO
SP_changedbowner [USER_A]
GO
USE B
GO
SP_changedbowner [USER_B]

We can check the database owners by running sp_helpdb. As you can see in the below screenshot that the database owners have been changed for both databases.

Now we can connect to the SQL Server instance again using both logins and see the changes compared to before.  Here we can see that only one database is visible for both logins. Database A is visible to user_A and database B is visible to user_B. This is because both logins are now the database owners of these databases.

Does making a user a db_owner work

Now we will create a new login user_C and assign db_owner access to both databases and check whether these databases are visible to this new login.

CREATE LOGIN user_C with password=‘[email protected]‘
GO
USE A
GO
CREATE USER user_C for login user_C;
GO
EXEC sp_addrolemember ‘db_owner‘, ‘user_C‘
GO
USE B
GO
CREATE USER user_c for login user_C
GO
EXEC sp_addrolemember ‘db_owner‘, ‘user_C‘

As we can see below, neither of these databases are visible for login user_C.  So from this we can see that you have to be the database owner to be able to see the databases in SQL Server Management Studio if the DENY VIEW ANY DATABASE is enabled for public.

Steps to hide databases for a specific login

Suppose we don‘t want to do this across the board, but only do this for a specific login.  We can run the below statement instead of DENY VIEW ANY DATABASE TO PUBLIC. After running the below statement, this login won‘t be able to see databases except for any database that this login is the database owner, but all other logins can see the database as long as you did not also deny view to Public.

USE MASTER
GO
GRANT VIEW ANY DATABASE TO PUBLIC; -- turn this back on if it was off
GO
DENY VIEW ANY DATABASE TO USER_A;
GO

Steps to view all databases

By default, the VIEW ANY DATABASE permission is granted to the public role. Therefore, by default, every user that connects to an instance of SQL Server can see all databases in the instance. To grant the VIEW ANY DATABASE permission to a specific login or to all logins run the following query:

--To grant the VIEW ANY DATABASE permission to a specific login.
USE MASTER
GO
GRANT VIEW ANY DATABASE TO [login_name];
GO
--To grant the VIEW ANY DATABASE permission to public.
USE MASTER
GO
GRANT VIEW ANY DATABASE TO PUBLIC;
Go

Note that if you use the DENY VIEW to PUBLIC this overrides the setting for an individual login, so if you DENY VIEW to PUBLIC and GRANT VIEW to a specific login this login will still not be able to see the databases.

If you are using DENY VIEW to PUBLIC and you want a login to still be able to see all databases without making that login a sysadmin you can do the following.  Make the login a user in the master database and make that user a db_owner of the master database.  This is not a very good option from a security perspective, but this does work.  This way a login can see all databases without having to be a sysadmin.

Conclusion

As you can see from the above, there are limited options to hiding databases.  Once you hide all databases the only logins that can see the databases are the logins that are the owners of the database or if the login is a sysadmin.  Also, each database can only have one owner, so you can‘t assign multiple owners to the same database.

Next Steps
  • Follow this process to hide your databases in SQL Server Management Studio.
  • Read more SSMS tips

引用资料:How to hide SQL Server user databases in SQL Server Management Studio

时间: 2024-10-09 23:16:20

sql server 中隐藏掉无关数据库的相关文章

Sql Server中判断表或者数据库是否存在

Sql Server中判断表或者数据库是否存在 SQL Server中判断数据库是否存在: 法(一): select * From master.dbo.sysdatabases where name='数据库名' 法(二): if db_id('数据库名') is not null drop database ... go create ... SQL Server中判断表对象是否存在: select count(*) from sysobjects where id = object_id(

转:SQL Server中服务器角色和数据库角色权限详解

当几个用户需要在某个特定的数据库中执行类似的动作时(这里没有相应的Windows用户组),就可以向该数据库中添加一个角色(role).数据库角色指定了可以访问相同数据库对象的一组数据库用户. 数据库角色的成员可以分为如下几类: Windows用户组或用户账户 SQL Server登录 其他角色 SQL Server的安全体系结构中包括了几个含有特定隐含权限的角色.除了数据库拥有者创建的角色之外,还有两类预定义的角色.这些可以创建的角色可以分为如下几类: 固定服务器 固定数据库 用户自定义 固定服

SQL Server 中登录账号与数据库用户迁移

1.      先创建一个SqlServer 身份验证的登录名,并映射到数据库中. 如:创建用户 [kk] 映射到数据库 [mytest],此时数据库 [mytest] 会增加一个用户 [kk] 2.      此时再删除登录名 [kk],删除后,数据库[mytest] 将存在一个孤立用户 [kk] 3.      查看当前数据库中是否存在孤立用户 use mytest; exec sp_change_users_login @Action='Report'; 4.      对于孤立用户,有两

将SQL Server中的数据库导入到PowerDesigner

再用PD建表完成后导成SQL脚本然后在SQL Server中运行后生成数据库后,就想到,可不可以将直接将数据库的内容生成PD文档?经过上网查,当然可以的. 要将SQL Server中的数据库导入到PD中,首先需要建立一个数据库的链接,然后进行逆向工程的操作.下面开始操作. 第一步:打开数据库菜单,选择"Configure Connections" 第二步:创建新的ODBC链接 第三步:选择系统数据源 第四步:选择需要的数据库 第五步:"完成" 第六步:命名数据源,并

sql server中的系统数据库

1.master数据库 master是SQL Server中最重要的数据库,是整个数据库服务器的核心.用户不能直接修改该数据库,如果损坏了master数据库,整个SQL Server服务器将不能工作.该数据库中包含以下内容:所有用户的登陆信息.用户所在的组.所有系统的配置选项.服务器中本地数据库的名称和信息.SQL Server的初始化方式等.作为一个数据库管理员,应该顶起备份master数据库. 2.model数据库 model数据库是SQL Server中创建数据库的模板,如果用户希望创建的

MSSQL之二 Sql Server中管理库与表

作为数据库开发人员,你负责创建和管理数据库和表.当创建表的时候,维护数据的完整性对你是很重要的.为确保表中的数据是准确的,一致的和可靠的,SQL Server提供了各种你可以应用到表上以增强数据完整性的检查. SQL Server包含各种系统数据库.本章介绍不同类型的系统数据库并且解释如何管理用户定义的数据库和管理用户自定义表. 重点 ?      管理数据库 ?      管理表 ?      SQL Server 2008中的数据类型 预习功课 ?        创建数据库 ?       

如何进行数据库,比如ORACLE,SQL SERVER的逆向工程,将数据库导入到PowerDesigner中

如何进行数据库,比如ORACLE,SQL SERVER的逆向工程,将数据库导入到PowerDesigner中 Oracle的反向工程就是指将Oracle中的数据库,当然也可以是SQL Server中的数据库导入到PD中,这个需要建立一个数据库的链接,然后进行逆向工程的操作. 第一步:建立数据库的链接: PowerDesigner建立与数据库的连接,以便生成数据库和从数据库生成到PD中.[Oracle 10G版] PowerDesigner建立与数据库的连接,以便生成数据库和从数据库生成到PD中.

????SQL Server中默认数据库和默认表的作用

我们知道Oracle数据库的安装架构可以是1个数据库对应1个或多个实例.而在SQL Server中,其架构和Oracle 完全相反,它是1个实例(默认实例名为Hostname主机名)下面包含多个数据库,在sqlserver数据库中(2000,2005,2008等),主要包含4个默认的数据库,分别是master数据库.model数据库.tempdb数据库和msdb数据库.这些数据库是SQL Server的心脏和灵魂.另外,还默认安装了两个实例数据库,分别是:northwind数据库和pubs数据库

SQL Server中数据库文件的存放方式,文件和文件组 (转载)

简介 在SQL SERVER中,数据库在硬盘上的存储方式和普通文件在Windows中的存储方式没有什么不同,仅仅是几个文件而已.SQL SERVER通过管理逻辑上的文件组的方式来管理文件.理解文件和文件组的概念对于更好的配置数据库来说是最基本的知识. 理解文件和文件组 在SQL SERVER中,通过文件组这个逻辑对象对存放数据的文件进行管理. 先来看一张图: 我们看到的逻辑数据库由一个或者多个文件组构成 而文件组管理着磁盘上的文件.而文件中存放着SQL SERVER的实际数据. 为什么通过文件组