还原 master 数据库 (Transact-SQL)Restore the master Database (Transact-SQL)

适用对象:是SQL Server 否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库APPLIES TO: yesSQL Server noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

本主题介绍如何从完整数据库备份还原 master 数据库。This topic explains how to restore the master database from a full database backup.

还原 master 数据库To restore the master database

  1. 在单用户模式下启动服务器实例。Start the server instance in single-user mode.

    有关如何指定单一用户启动参数 (-m) 的信息,请参阅配置服务器启动选项(SQL Server 配置管理器)For information about how to specify the single-user startup parameter (-m), see Configure Server Startup Options (SQL Server Configuration Manager).

  2. 若要还原 master的完整数据库备份,请使用以下 RESTORE DATABASETransact-SQLTransact-SQL 语句:To restore a full database backup of master, use the following RESTORE DATABASETransact-SQLTransact-SQL statement:

    RESTORE DATABASE master FROM <backup_device> WITH REPLACERESTORE DATABASE master FROM <backup_device> WITH REPLACE

    REPLACE 选项指示 SQL ServerSQL Server 即使已经存在同名数据库也要还原指定的数据库。The REPLACE option instructs SQL ServerSQL Server to restore the specified database even when a database of the same name already exists. 现有的数据库(如果存在)被删除。The existing database, if any, is deleted. 在单用户模式下,建议在 sqlcmd 实用工具中输入 RESTORE DATABASE 语句。In single-user mode, we recommend that you enter the RESTORE DATABASE statement in the sqlcmd utility. 有关详细信息,请参阅 使用 sqlcmd 实用工具For more information, see Use the sqlcmd Utility.

    重要

    在还原 master 后, SQL ServerSQL Server 实例会关闭,并终止 sqlcmd 进程。After master is restored, the instance of SQL ServerSQL Server shuts down and terminates the sqlcmd process. 在重新启动服务器实例之前,请删除单用户引导参数。Before you restart the server instance, remove the single-user startup parameter. 有关详细信息,请参阅配置服务器启动选项(SQL Server 配置管理器)For more information, see Configure Server Startup Options (SQL Server Configuration Manager).

  3. 重新启动服务器实例并继续执行其他恢复步骤,例如还原其他数据库、附加数据库以及更正用户不匹配问题。Restart the server instance and continue other recovery steps such as restoring other databases, attaching databases, and correcting user mismatches.

示例Example

下面的示例将在默认服务器实例上还原 master 数据库。The following example restores the master database on the default server instance. 该示例假定服务器实例是在单用户模式下运行。The example assumes that the server instance is already running in single-user mode. 该示例启动 sqlcmd 并执行 RESTORE DATABASE 语句,以便从磁盘设备 master 还原 Z:\SQLServerBackups\master.bak的完整数据库备份。The example starts sqlcmd and executes a RESTORE DATABASE statement that restores a full database backup of master from a disk device: Z:\SQLServerBackups\master.bak.

备注

对于命名实例,sqlcmd 命令必须指定 -S<ComputerName>\<InstanceName> 选项。For a named instance, the sqlcmd command must specify the -S<ComputerName>\<InstanceName> option.

  
      C:\> sqlcmd  
1> RESTORE DATABASE master FROM DISK = 'Z:\SQLServerBackups\master.bak' WITH REPLACE;  
2> GO  

另请参阅See Also

完整数据库还原(简单恢复模式) Complete Database Restores (Simple Recovery Model)
完整数据库还原(完整恢复模式) Complete Database Restores (Full Recovery Model)
孤立用户故障排除 (SQL Server) Troubleshoot Orphaned Users (SQL Server)
数据库分离和附加 (SQL Server) Database Detach and Attach (SQL Server)
重新生成系统数据库 Rebuild System Databases
数据库引擎服务启动选项 Database Engine Service Startup Options
SQL Server 配置管理器 SQL Server Configuration Manager
备份和还原系统数据库 (SQL Server) Back Up and Restore of System Databases (SQL Server)
RESTORE (Transact-SQL) RESTORE (Transact-SQL)
在单用户模式下启动 SQL ServerStart SQL Server in Single-User Mode