HOW TO: RESTORE THE MASTER DATABASE IN SQL SERVER 2012

by Thomas LaRock | Jan 14, 2014 | MSSQLSQL MCMSQL MVPSQLServerPedia Wiki | 16 comments

In my seven years as a production DBA I can count on one hand the number of times I have needed to restore the master database as a result of a disaster. With over 180 instances of SQL Server in our care I believe that is a large enough sample size making it safe to assume that restoring master is not something you will find yourself needing to do often. But when the time comes (in the middle of the night, of course) you will want to be prepared.

The master database stores metadata about the instance of SQL Server: things like server logins, databases and database file locations, server configuration details, and linked server definitions are all stored inside of the master database. If the master database is not available (say, due to corruption or a disaster that wipes away the disk where the master database is kept) then your instance of SQL Server cannot function.

So, yeah, it’s kinda a big deal as databases go. When the time comes you will want to be able to restore the master database quickly.

Restoring the master database is surprisingly easy. Of course I am assuming you have a backup. You are running backups, right? Good. If you aren’t running backups then you need to get started. Here’s a link to the scripts that SQL experts use the world over, made by SQL Server MVP Ola Hallengren.

Assuming you have a good backup of master you can test the restore process by following these simple steps:

  1. Using SQL Configuration manager, stop the SQL Server instance
  2. Open a command window
  3. In the command window, start the instance executable in maintenance mode
  4. In a second command window, open SQLCMD
  5. Restore master from within the SQLCMD window
  6. Using SQL configuration manager, restart instance

That’s all there is to it. I would have my team practice this on a test server a few times a year just to make sure their skills were sharp should they ever need to restore master when half-asleep in the middle of the night. As part of the test I would have them create a SQL login at the beginning. That way when the restore was complete they could verify that the login disappeared.

Let’s walk through it together.

RESTORE THE MASTER DATABASE IN SQL SERVER 2012

First up, let’s take a backup of the master database. I’m doing this as part of an exercise here, you will want to make certain that if you take a backup of master on a server in your environment that you aren’t interfering with any existing backup plan:

BACKUP DATABASE [master] TO  DISK = N'C:\SQL\Backups\master.bak'

Next we will create a test login as part of our practice run:

USE [master]
GO
CREATE LOGIN [master_restore_test]
WITH PASSWORD=N'test',
DEFAULT_DATABASE=[master],
CHECK_EXPIRATION=OFF,
CHECK_POLICY=OFF

Just for fun, we will create a new database as well:

CREATE DATABASE [restore_test]

Since the login and database were created after the backup they won’t be there after the restore is complete.

Let’s get the restore process started. First we must stop the instance. I will use SQL Configuration Manager to select the instance and then hit the red stop button:

Next we will restart the instance using the –m startup parameter from a command window. I will navigate to the directory where the sqlservr.exe resides (for me that is C:\Program Files\Microsoft SQL Server\MSSQL11.JAMBON\MSSQL\Binn, probably not the exact same for you, but close). One there I will run the following command:

.\sqlservr.exe –c –m –s JAMBON

Since I am using a named instance (JAMBON), I need to include the –s switch followed by the name. The –c switch allows for faster startup time when SQL Server is not being run as a service. You can read more about the available switches here. [PRO TIP: That’s not a typo, the sqlservr.exe executable is missing the second ‘e’. No, I don’t know why. I’m guessing someone will tell me this is how you lived back in the MS-DOS days before Y2Kscared the hell out of everyone. But trust me, you’ll only misspell it a few dozen times in the middle of the night before you figure it out.]

This is what you will see after it has started:

Next, I will open up a command window and connect to my named instance using:

SQLCMD –S .\JAMBON

You can read more about SQLCMD here. After I connect to the instance, I can restore the master database:

RESTORE DATABASE master FROM DISK = 'C:\SQL\Backups\master.bak' WITH REPLACE

Note that once the restore is complete the instance will be shut down. It looks something like this:

The first command window will also reflect that the instance has been shut down:

So, let’s get the instance restarted!

I will use SQL Server Configuration Manager to start the instance and once it is started I will open up SQL Server Management Studio and run the following query:

SELECT *
FROM sys.server_principals
WHERE name = 'master_restore_test'

And verify that the login is no longer there.

The database is gone as well. However, the underlying database files still exist. Remember, master only stores the metadata about your instance. The restore didn’t erase the database files, it just reset the instance to a point in time before the database was created. This is important because if you find yourself in a disaster recovery situation you will want to understand why you may not be able to access your databases after restoring master.

To get our test database back you can either do a restore, or an attach. Here’s the syntax for the attach:

USE [master]
GO
CREATE DATABASE [restore_test] ON
( FILENAME = N'C:\SQL\Data\restore_test.mdf' ),
( FILENAME = N'C:\SQL\Logs\restore_test_log.ldf' )
FOR ATTACH
GO

Now, the more likely disaster scenario would be that the restore of master has the metadata for the databases but the database files don’t exist. In that case, SQL Server 2012 will show the database in the state of “recovery pending”, like so:

You can’t attach the database in this case, because master thinks it is already there. And, in a way, it is. But it’s more like a ghost than anything else. The easiest thing here is to do a restore. In fact, that’s the method I chose, and I even had scripts ready to run in the event of a disaster that would restore all of the databases dynamically. But what if you don’t have backups (yeah, that does happen). Well then, you can drop the database, then attach.

But if you don’t have backups, and you don’t have the data and log files, then you WILL have plenty of time to update your resume before leaving town.

Congrats, you have just restored the master database in SQL Server 2012. Take your time and practice this. Try different disaster scenarios and see what error messages get generated. You can learn a lot about recovery methods in a short amount of time just by practicing a master database restore.

SQL SERVER2008 R2修改Server Collation--文檔沒細看先存下來.的更多相关文章

  1. SQL Server2008 R2 数据库镜像实施手册(双机)SQL Server2014同样适用

    这篇文章主要介绍了SQL Server2008 R2 数据库镜像实施手册(双机)SQL Server2014同样适用,需要的朋友可以参考下 一.配置主备机 1. 服务器基本信息 主机名称为:HOST_ ...

  2. SQL Server2008 R2开启远程连接总结

    ============================== SQL Server2008 R2开启远程连接(最全总结) ============================== 安装过程:适用W ...

  3. SQL Server2008 R2 安装失败后的解决办法

    当你第一次安装SQL Server2005,SQL Server2008,SQL Server2012失败后,第二次重新安装一般还是容易安装失败,原因就是你没有完全卸载,还存留残留文件和注册表. 我安 ...

  4. 安装的SQL Server2008 R2版的连接不到本地数据,提示未找到或无法访问服务器。----复制自百度知道

    安装的SQL Server2008 R2版的连接不到本地数据,提示未找到或无法访问服务器.使用Windows身份验证 2012-09-17 00:23hj168926 | 分类:数据库DB | 浏览3 ...

  5. sql server2008 R2 生成带数据的脚本

    目前 sql server2008 R2 版本有这个功能 http://www.cnblogs.com/weisenz/archive/2013/03/20/2971334.html

  6. sql server2008 R2打开报错:无法识别的配置节 system.serviceModel解决办法分享

    本人是先安装的sql server2008 R2成功可以运行后,再安装VS2010成功后,再打开sql server2008,就出现以下错误,无法连接服务器.   无法识别的配置节 system.se ...

  7. sql server2008 R2 各个版本的区别与选择

    目前已知的SQL Server 2008 R2的版本有: 企业版.标准版.工作组版.Web版.开发者版.Express版.Compact 3.5版. 这个次序也是各个版本功能的强大程度从高到低的一个排 ...

  8. Win10 系统安装Sql Server2008 R2 数据库遇到的问题及解决办法总结!

    1.开始安装时,提示要先安装 “.NET Framework 3.5(包括.NET 2.0和3.0)”,之前已经下载好.NET Framework 3.5 sp1,安装时还是提示要先安装 “.NET  ...

  9. sql server2008 r2 密钥

    Microsoft SQL Server 2008 R2序列号密钥 数据中心版32位:PTTFM-X467G-P7RH2-3Q6CG-4DMYB数据中心版64位:DDT3B-8W62X-P9JD6-8 ...

随机推荐

  1. Diffie-Hellman 密钥交换

    假定有两个全局公开的参数,分别为一个素数p和一个整数g,g是p的一个原根,为了协商共享的会话密钥: 首先,用户A随机选取a,计算出A = g^a mod p,并将A发送给B:然后,用户B随机选取b,计 ...

  2. java代码多线程实现如下

    总结:我的比赛得了最差的奖,老师提都没提,所以,我应该有自知之明,你并不是他最喜欢的学生 import java.util.Scanner; //利用多线程实现输入等待…… public class ...

  3. Docker - 避免启动container后运行shell脚本执行完成后docker退出container

    问题 最近在使用 Dockerfile 启动容器,发现使用Dockerfile调用容器里面的shell,当shell执行完成以后,docker会退出容器. 分析 Docker 在执行shell的时候, ...

  4. 第14 章 Spring MVC的工作机制与设计模式

    14.1 Spring MVC的总体设计 要使用SPring MVC,只要在web.xml中配置一个DispatcherServlet. 再定义一个dispatcherServlet-servlet. ...

  5. python学习(四) 字典:当索引不好用时

    第四章 字典:当索引不好用时 4.1 字典的使用 字典的适用场景: 表示一个游戏棋盘的状态,每个键都是由坐标值组成的元组: 存储文件修改时间,用文件名作为键: 数字电话/地址簿 4.2 创建和使用字典 ...

  6. C# 通用类型转换方法

    在程序开发过程中经常需要进行数据的类型转换,而且如果一个字段的类型改成另一个类型时,所有相关的类型转换的地方都要跟着修改,不但造成了很多重复转换的代码而且修改字段类型时额外修改相关转换代码的工作量也很 ...

  7. win7下cygwin 中 root用户的设置

    问题描述: cygwin 在 win10下安装完成后使用当前用户登录后看所在磁盘的文件权限是没有问题的,但在cygwin编译出来的文件的权限为空,这个问题可以使用以下方法来解决: 解决办法: 将cyg ...

  8. WARNING: cell0 mapping not found - not syncing cell0

    WARNING: cell0 mapping not found - not syncing cell0

  9. SEO网站title应该怎么写

    第一:具有独特性 在你的网站中,也许有成千上万的页面,首页-分类-无数的文章页面,这些都有固定的标题,他们的标题最好不要相同.有的时候也许不是`站长们故意的,但是在使用编辑软件的时候,经常 会出现很多 ...

  10. docker 制作本地镜像

    docker commit 55ddf8d62688 py_wb # 容器ID, 容器名称tag py_wb IP地址:5000/my-web:20180511 # 远程registory地址 我的镜 ...