查看SQL Server Resource Database以及修改系统表

摘要:
参考:如何登录DAC(SQLServer2005)ResourceDBLocationChangesResourceDBLocatationChanges1。什么是Resourcedb?(来自msdn)资源数据库是包含所有系统的只读数据库

参考文献:

SQL 2005修改系统表

SQL Server 2005中修改系统表的方法

如何登入DAC(SQL Server 2005) 

ResourceDB Location Changes

ResourceDB Location Changes

1.What is resourcedb?(from msdn)

The Resource database is a read-only database that contains all the system objects that are included with SQL Server. SQL Server system objects, such as sys.objects, are physically persisted in the Resource database, but they logically appear in the sys schema of every database. The Resource database does not contain user data or user metadata.

The Resource database makes upgrading to a new version of SQL Server an easier and faster procedure. In earlier versions of SQL Server, upgrading required dropping and creating system objects. Because the Resource database file contains all system objects, an upgrade is now accomplished simply by copying the single Resource database file to the local server.

2.ResourceDB’s Location

The physical file names of the Resource database are mssqlsystemresource.mdf and mssqlsystemresource.ldf. Their locations location are different depend on the SQL Server version.

2.1.In SQL Server 2008 and above

<drive>:\Program Files\Microsoft SQL Server\MSSQL10.<instance_name>\Binn\. Such as

D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn

2.2.In SQL Server 2005

Same place where master database is located. Such as D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA

3.Experiment on 2008 and above

Step1:

Copy the mssqlsystemresource.MDF and mssqlsystemresource.LDF to the master database’s location, rename the copies to MyResourceDB.mdf and MyResourceDB.ldf.

Step2:

Attach the new files with this script generated by the SSMS:

--正常登录模式
USE [master]
GO
CREATE DATABASE [MyResourceDB] ON
( FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyResourceDB.mdf' ),
( FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyResourceDB.ldf' )
 FOR ATTACH
GO
if not exists (select name from master.sys.databases sd where name = N'MyResourceDB' AND
    SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() )
        EXEC [MyResourceDB].dbo.sp_changedbowner @loginame=N'sa', @map=false
GO
use MyResourceDB
--查询失败:Invalid object name 'sys.role_permissions'.
select * from sys.role_permissions
--修改失败:Invalid object name 'sys.role_permissions'.
update sys.role_permissions  set number=111  where name='System Administrators'

Then you can find the new database MyResourceDB in the Object Explorer. But if you want to open these table in the MyResourceDB, it will report error: Invalid object name 'MyResourceDB.sys.role_permissions'.

Step3: Starting SQL Server in Single-User Mode

  1. Close all the SQL Server Service in the SQL Server Configuration manager, such as SQL Server Agent, SQL Server Reporting Service and so on.
  2. Use sqlservr.exe -m -c cmd to start SQL Server in single-user mode in the path of D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn>.

Step4: Use the Dedicated Administrator Connection to modify system table.

We Use the Dedicated Administrator Connection with SQL Server Management Studio. First open the SSMS, then click New Query, then we choose the Server type and type the server name as the follow picture:

查看SQL Server Resource Database以及修改系统表第1张

“ADMIN:.” means Dedicated Administrator Connection to the default instance.

Step5: Browse the resourcedb

--DAC模式
use MyResourceDB
select * from MyResourceDB.sys.role_permissions   --system table,succsess successfully
select * from MyResourceDB.sys.databases  --system view,excute successfully

--update this system table,error:Failed to update database "mssqlsystemresource" because the database is read-only.
update MyResourceDB.sys.role_permissions  set number=111  where name='System Administrators' 

--update this system view,success.
update MyResourceDB.sys.databases  set create_date='2009-07-11 11:13:40.723'  where name='TESTDB1' 
--Warning: System table ID 28 has been updated directly in database ID 8 and cache coherence may not have been maintained. SQL Server should be restarted.

------------------------------------------------------------------------------------------

--直接使用mssqlsystemresource
use mssqlsystemresource
select * from mssqlsystemresource.sys.role_permissions
select * from mssqlsystemresource.sys.databases

--修改这张表,报错:Failed to update database "mssqlsystemresource" because the database is read-only.
update mssqlsystemresource.sys.role_permissions  set number=111  where name='System Administrators' 

--与前面使用MyResourceDB不同。修改视图,报错:Failed to update database "mssqlsystemresource" because the database is read-only.
update sys.databases  set create_date='2012-07-11 11:13:40.723'  where name='TESTDB1' 
select * from sys.databases

------------------------------------------------------------------------------------------

use master
select * from sys.databases  --success
update sys.databases  set create_date='2012-07-11 11:13:40.723'  where name='TESTDB1' 

Conclusion

In SQL Server 2008 and above, resource database is a read-only database and every instance has only one. It was created when the database instance installed. Because the resource database is read-only and never changed after it is created, when the resource database is damaged or lost, we can copy other database instance’s resource database to our instance in the same SQL Server version.

This experiment is the similar with the experiment that how to update the sys.databases view in master database.

4.     Experiment on 2005

Now, we have to do the same experiment on SQL Server 2005. As before, I have said that mssqlsystemresource.mdf located the the same path of master.mdf. We also copy and rename these two files and attach them to the database.

At this time, we will see the different result. We can change the sys.role_permissions table without any warning and error. So if you want to change the resource database, you only need to create a resource database copy and attach it as a new database. We can change it whatever we want. At last, we replace the resource database with the new database.

--正常登陆模式,附加数据库
USE [master]
GO
CREATE DATABASE [MyResourceDB] ON 
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\MyResourceDB.mdf' ),
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\MyResourceDB.ldf' )
 FOR ATTACH
GO
if not exists (select name from master.sys.databases sd where name = N'MyResourceDB' AND
    SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() ) 
        EXEC [MyResourceDB].dbo.sp_changedbowner @loginame=N'sa', @map=false
GO

--使用MyResourceDB
use MyResourceDB
select * from sys.role_permissions--可以查询

--可以修改
begin tran
update sys.role_permissions  set number=111  where name='System Administrators' 
select * from sys.role_permissions   --可以查询
rollback  --回滚
select * from sys.role_permissions   --可以查询

--DAC模式下
use mssqlsystemresource
select * from sys.role_permissions --可以查询
--更新列,报错:无法更新数据库"mssqlsystemresource",因为数据库是只读的。
update sys.role_permissions  set number=111  where name='System Administrators' 

--使用MyResourceDB
use MyResourceDB
select * from sys.role_permissions--可以查询

--修改,报错:无法更新数据库"mssqlsystemresource",因为数据库是只读的。
update sys.role_permissions  set number=111  where name='System Administrators' 

结论:

MyResourceDB可在普通登录模式下被修改,在DAC模式下修改MyResourceDB就是在修改mssqlsystemresource这个数据库。

5.比较SQL Server不同版本在查看和修改MyResourceDB上的区别

对于MyResourceDB这个数据库,对于不同版本的SQL Server以及不同的登录模式下,我们有不同的权限,我们通过下面两张表说明其中的不同。

使用SSMS正常登陆

使用SSMS正常登陆

2005

2008

2012

System Tables中是否有表

Y

Y

N

能否查看System Tables表的内容

Y

N

N

能否修改System Tables表的内容

Y

N

N

单用户模式启动实例,使用DAC登录

单用户模式启动实例

使用DAC登录

2005

2008

2012

能否查看System Tables表的内容

Y

Y

Y

能否修改System Tables表的内容

N

N

N

DAC

If you use a named instance, you must open SQL Server Browser Service to connect to dedicated administrator connection (DAC) endpoints. If you use a default instance, it does not need.

在启动sharepoint server的时候无法使用单用户模式启动实例。

免责声明:文章转载自《查看SQL Server Resource Database以及修改系统表》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇kafka入门webstorm设置新建vue文件的模板下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

plsql和tsql常用函数比对

http://www.jb51.net/list/list_154_1.htm 数学函数 1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) S:select ceiling(-1.001) value O:select ceil(-1.001) value from...

JAVA框架-Mybatis中(代理、动态SQL和高级映射)

Mapper代理 在上一个博客中,我们直接利用session和<select>标签来执行sql的方式存在一些问题。 session执行sql时都需要提供要执行sql的id,而这个id是字符串类型,意味着id是否正确在编译期间是无法获知的,必须等到运行时才能发现错误, sql需要的参数和返回值类都不明确,这也增加了出错的概率 我们最理想的方式...

oracle创建用户、授予权限及删除用户

创建用户 oracle对表空间 USERS 无权限 alter user 用户名 quota unlimited on users; //创建临时表空间 create temporary tablespace test_temp tempfile 'E:/oracle/product/10.2.0/oradata/testserver/test_tem...

Oracle系统表大全(转)

下面全面是对Oracle系统表的一些介绍: 数据字典dict总是属于Oracle用户sys的。   1、用户:    select username from dba_users;   改口令    alter user spgroup identified by spgtest;   2、表空间:    select * from dba_data_fi...

insert时出现主键冲突的处理方法【转载】

原文出处:http://hi.baidu.com/ytjwt/blog/item/1ccc2c26022b0608908f9d8c.html 使用"insert into"语句进行数据库操作时可能遇到主键冲突,用户需要根据应用场景进行忽略或者覆盖等操作。总结下,有三种解决方案来避免出错。1. insert ignore into遇主键冲突,保持原纪录,忽略...

informatica 学习日记整理

1. INFORMATICA CLIENT的使用 1.1Repository Manager 的使用 1.1.1创建Repository。 前提: a. 在ODBC数据源管理器中新建一个数据源连接至你要创建Repository的数据库(例:jzjxdev) b. 要在你要连接的数据库中新建一个用户(例:name: ETL password: E...