数据库分离和附加 (SQL Server)Database Detach and Attach (SQL Server)

06/30/2020

本文内容

适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQL ServerSQL Server (all supported versions)适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQL ServerSQL Server (all supported versions)

可以分离数据库的数据和事务日志文件,然后将它们重新附加到同一或其他 SQL ServerSQL Server实例。The data and transaction log files of a database can be detached and then reattached to the same or another instance of SQL ServerSQL Server. 如果要将数据库更改到同一计算机的不同 SQL ServerSQL Server 实例或要移动数据库,分离和附加数据库会很有用。Detaching and attaching a database is useful if you want to change the database to a different instance of SQL ServerSQL Server on the same computer or to move the database.

SecuritySecurity

文件访问权限可在很多数据库操作过程中设置,其中包括分离或附加数据库。File access permissions are set during a number of database operations, including detaching or attaching a database.

重要

建议您不要附加或还原来自未知或不可信源的数据库。We recommend that you do not attach or restore databases from unknown or untrusted sources. 此类数据库可能包含恶意代码,这些代码可能会执行非预期的 Transact-SQLTransact-SQL 代码,或者通过修改架构或物理数据库结构导致错误。Such databases could contain malicious code that might execute unintended Transact-SQLTransact-SQL code or cause errors by modifying the schema or the physical database structure.

使用来自未知源或不可信源的数据库前,请在非生产服务器上针对数据库运行 DBCC CHECKDB ,然后检查数据库中的代码,例如存储过程或其他用户定义代码。Before you use a database from an unknown or untrusted source, run DBCC CHECKDB on the database on a nonproduction server and also examine the code, such as stored procedures or other user-defined code, in the database.

分离数据库Detaching a Database

分离数据库是指将数据库从 SQL ServerSQL Server 实例中删除,但使数据库在其数据文件和事务日志文件中保持不变。Detaching a database removes it from the instance of SQL ServerSQL Server but leaves the database intact within its data files and transaction log files. 之后,就可以使用这些文件将数据库附加到任何 SQL ServerSQL Server实例,包括分离该数据库的服务器。These files can then be used to attach the database to any instance of SQL ServerSQL Server, including the server from which the database was detached.

如果存在下列任何情况,则不能分离数据库:You cannot detach a database if any of the following are true:

已复制并发布数据库。The database is replicated and published. 如果进行复制,则数据库必须是未发布的。If replicated, the database must be unpublished. Before you can detach it, you must disable publishing by running sp_replicationdboption.

备注

如果无法使用 sp_replicationdboption,可以通过运行 sp_removedbreplication删除复制。If you cannot use sp_replicationdboption, you can remove replication by running sp_removedbreplication.

数据库中存在数据库快照。A database snapshot exists on the database.

必须首先删除所有数据库快照,然后才能分离数据库。Before you can detach the database, you must drop all of its snapshots.

备注

不能分离或附加数据库快照。A database snapshot cannot be detached or attached.

数据库是 Always On 可用性组的一部分。The database is part of an Always On availability group.

在将数据库从可用性组中删除之前,无法分离该数据库。The database cannot be detached until it is removed from the availability group.

该数据库正在某个数据库镜像会话中进行镜像。The database is being mirrored in a database mirroring session.

除非终止该会话,否则无法分离该数据库。The database cannot be detached unless the session is terminated.

数据库处于可疑状态。The database is suspect. 无法分离可疑数据库;必须将数据库设为紧急模式,才能对其进行分离。A suspect database cannot be detached; before you can detach it, you must put it into emergency mode. For more information about how to put a database into emergency mode, see ALTER DATABASE (Transact-SQL).

数据库为系统数据库。The database is a system database.

备份、还原及分离Backup and Restore and Detach

分离只读数据库将会丢失有关差异备份的差异基准的信息。Detaching a read-only database loses information about the differential bases of differential backups.

响应分离错误Responding to Detach Errors

分离数据库时生成的错误会阻止完全关闭数据库和重新生成事务日志。Errors produced while detaching a database can prevent the database from closing cleanly and the transaction log from being rebuilt. 收到错误消息后,请执行下列更正操作:If you receive an error message, perform the following corrective actions:

重新附加与数据库关联的所有文件,而不仅仅是主文件。Reattach all files associated with the database, not just the primary file.

解决导致生成错误消息的问题。Resolve the problem that caused the error message.

再次分离数据库。Detach the database again.

附加数据库Attaching a Database

您可以附加复制的或分离的 SQL ServerSQL Server 数据库。You can attach a copied or detached SQL ServerSQL Server database. 当将包含全文目录文件的 SQL Server 2005 (9.x)SQL Server 2005 (9.x) 数据库附加到 SQL Server 2019 (15.x)SQL Server 2019 (15.x) 服务器实例上时,会将目录文件从其以前的位置与其他数据库文件一起附加,这与 SQL Server 2005 (9.x)SQL Server 2005 (9.x)中的情况相同。When you attach a SQL Server 2005 (9.x)SQL Server 2005 (9.x) database that contains full-text catalog files onto a SQL Server 2019 (15.x)SQL Server 2019 (15.x) server instance, the catalog files are attached from their previous location along with the other database files, the same as in SQL Server 2005 (9.x)SQL Server 2005 (9.x). 有关详细信息,请参阅 全文搜索升级。For more information, see Upgrade Full-Text Search.

附加数据库时,所有数据文件(MDF 文件和 NDF 文件)都必须可用。When you attach a database, all data files (MDF and NDF files) must be available. 如果任何数据文件的路径不同于首次创建数据库或上次附加数据库时的路径,则必须指定文件的当前路径。If any data file has a different path from when the database was first created or last attached, you must specify the current path of the file.

备注

如果附加的主数据文件是只读的,则 数据库引擎Database Engine 假定数据库也是只读的。If the primary data file being attached is read-only, the 数据库引擎Database Engine assumes that the database is read-only.

当加密的数据库首次附加到 SQL ServerSQL Server 实例时,数据库所有者必须通过执行下面的语句打开数据库的主密钥:OPEN MASTER KEY DECRYPTION BY PASSWORD = 'password'。When an encrypted database is first attached to an instance of SQL ServerSQL Server, the database owner must open the master key of the database by executing the following statement: OPEN MASTER KEY DECRYPTION BY PASSWORD = 'password'. 建议通过执行下面的语句对主密钥启用自动解密:ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY。We recommend that you enable automatic decryption of the master key by executing the following statement: ALTER MASTER KEY ADD ENCRYPTION BY SERVICE MASTER KEY.

附加日志文件的要求在某些方面取决于数据库是读写的还是只读的,如下所示:The requirement for attaching log files depends partly on whether the database is read-write or read-only, as follows:

对于读写数据库,通常可以附加新位置中的日志文件。For a read-write database, you can usually attach a log file in a new location. 不过,在某些情况下,重新附加数据库需要使用其现有的日志文件。However, in some cases, reattaching a database requires its existing log files. 因此,请务必保留所有分离的日志文件,直到在不需要这些日志文件的情况下成功附加了数据库。Therefore, it is important to always keep all the detached log files until the database has been successfully attached without them.

如果读写数据库具有单个日志文件,并且您没有为该日志文件指定新位置,附加操作将在旧位置中查找该文件。If a read-write database has a single log file and you do not specify a new location for the log file, the attach operation looks in the old location for the file. 如果找到了旧日志文件,则无论数据库上次是否完全关闭,都将使用该文件。If it is found, the old log file is used, regardless of whether the database was shut down cleanly. 但是,如果未找到旧文件日志,数据库上次是完全关闭且现在没有活动日志链,则附加操作将尝试为数据库创建新的日志文件。However, if the old log file is not found and if the database was shut down cleanly and has no active log chain, the attach operation attempts to build a new log file for the database.

如果附加的主数据文件是只读的,则 数据库引擎Database Engine 假定数据库也是只读的。If the primary data file being attached is read-only, the 数据库引擎Database Engine assumes that the database is read-only. 对于只读数据库,日志文件在数据库主文件中指定的位置上必须可用。For a read-only database, the log file or files must be available at the location specified in the primary file of the database. 因为 SQL ServerSQL Server 无法更新主文件中存储的日志位置,所以无法生成新的日志文件。A new log file cannot be built because SQL ServerSQL Server cannot update the log location stored in the primary file.

附加数据库时的元数据更改Metadata Changes on Attaching a Database

分离再重新附加只读数据库后,会丢失有关当前差异基准的备份信息。When a read-only database is detached and then reattached, the backup information about the current differential base is lost. “差异基准” 是数据库或其文件或文件组子集中所有数据的最新完整备份。The differential base is the most recent full backup of all the data in the database or in a subset of the files or filegroups of the database. 如果没有基准备份信息, master 数据库会变得与只读数据库不同步,这样之后进行的差异备份可能会产生意外结果。Without the base-backup information, the master database becomes unsynchronized with the read-only database, so differential backups taken thereafter may provide unexpected results. 因此,如果对只读数据库使用差异备份,在重新附加数据库后,应通过进行完整备份来建立新的差异基准。Therefore, if you are using differential backups with a read-only database, you should establish a new differential base by taking a full backup after you reattach the database. For information about differential backups, see Differential Backups (SQL Server).

附加时,数据库会启动。On attach, database startup occurs. 通常,附加数据库时会将数据库重置为它分离或复制时的状态。Generally, attaching a database places it in the same state that it was in when it was detached or copied. 但是,附加和分离操作都会禁用数据库的跨数据库所有权链接。However, attach-and-detach operations both disable cross-database ownership chaining for the database.

重要

为安全起见,,默认情况下,每当附加数据库时,is_broker_enabled、is_honoor_broker_priority_on 和 is_trustworthy_on 的选项均设置为 OFF 。By default and for security, the options for is_broker_enabled, is_honor_broker_priority_on and is_trustworthy_on are set to OFF whenever the database is attached. For information about how to set these options on, see ALTER DATABASE (Transact-SQL).

备份、还原及附加Backup and Restore and Attach

与任何完全或部分脱机的数据库一样,不能附加正在还原文件的数据库。Like any database that is fully or partially offline, a database with restoring files cannot be attached. 如果停止了还原顺序,则可以附加数据库。If you stop the restore sequence, you can attach the database. 然后,可以重新启动还原顺序。Then, you can restart the restore sequence.

将数据库附加到其他服务器实例Attaching a Database to Another Server Instance

重要

无法在早期版本的 SQL Server 中附加由较新版本的 SQL ServerSQL Server 创建的数据库。A database created by a more recent version of SQL ServerSQL Server cannot be attached in earlier versions. 这将阻止数据库被旧版本的 数据库引擎Database Engine 物理使用。This precludes the database from being physically used with an older version of the 数据库引擎Database Engine. 但是,这与元数据状态相关且不会影响数据库兼容性级别。However, this relates to metadata state and does not affect the database compatibility level.

将数据库附加到其他服务器实例时,为了给用户和应用程序提供一致的体验,您最好在其他服务器实例上为数据库重新创建部分或全部元数据(例如登录名和作业)。When you attach a database onto another server instance, to provide a consistent experience to users and applications, you might have to re-create some or all of the metadata for the database, such as logins and jobs, on the other server instance.

相关任务Related Tasks

分离数据库To detach a database

附加数据库To attach a database

使用分离和附加操作升级数据库To upgrade a database using detach and attach operations

使用分离和附加操作移动数据库To move a database using detach and attach operations

删除数据库快照To delete a database snapshot

另请参阅See Also

mysql数据库的分离_数据库分离和附加 (SQL Server)相关推荐

  1. 数据库分离和附加 (SQL Server)

    数据库分离和附加 (SQL Server) 2018/11/2 适用对象:SQL ServerAzure SQL 数据库Azure SQL 数据仓库并行数据仓库 可以分离数据库的数据和事务日志文件,然 ...

  2. mysql数据库工程师 课程_数据库开发工程师需要学习哪些课程?

    展开全部 数据库开发工程师需要学习的课程有: 1.计32313133353236313431303231363533e58685e5aeb931333365646263算机导论 内容提要:为新学生提供 ...

  3. 数据库应用程序开发基础篇—— .NET中SQL Server数据库的操作C#篇之一

    数据库应用程序开发基础篇-- .NET中SQL Server数据库的操作C#篇之一 写在前面:前面介绍了数据库系统的基本概念,SQl语句基本使用方法,接下来通过学习具体语言和具体数据库结合的应用开发来 ...

  4. python读取sqlserver数据库方法_SQLServer数据库之Python读取配置文件,并连接数据库SQL Server...

    本文主要向大家介绍了SQLServer数据库之Python读取配置文件,并连接数据库SQL Server,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助. 用配置文件保存固定 ...

  5. 因为数据库正在使用,所以无法获得对数据库的独占访问权。 (3101)[42000] [Microsoft][SQL Server Native Client 10.0][SQL Server]RES

    问题描述: SQL server 数据库还原时,遇到问题. [RES] Database restore start [RES] (Full)  [2022-06-27 22:12:15.000] [ ...

  6. MySQL删除空值语句_数据库语句sql 删除空记录

    最简单删除SQL Server中所有数据的方法 原文:最简单删除SQL Server中所有数据的方法 最简单删除SQL Server中所有数据的方法   编写人:CC阿爸   2014-3-14 其实 ...

  7. 【MySQL 第10章_数据库的设计规范】

    第10章_数据库的设计规范 1. 为什么需要数据库设计 2.范式 2.1范式简介 2.2范式都包括哪些 2.3 键和相关属性的概念 2.4第一范式(1st NF) 2.5 第二范式(2nd NF) 2 ...

  8. mysql数据库简单介绍_数据库的简单介绍

    关系型数据库与非关系型数据库 关系型数据库:mysql /oracle/sql server/sqlite 优点: 1.易于维护:都是使用表结构,格式一致: 2.使用方便:sql语言通用,可用于复杂查 ...

  9. mysql双机互备linux成功的_配置MySQL双机热备 - Linux服务器MySQL双机热备份试验_数据库技术_Linux公社-Linux系统门户网站...

    二.配置MySQL双机热备 传统模式都是采用MySQL双机互备,至于双机热备我搜索了大量的精华帖子都没有发现安装配置文档.故我的一个想法产生了:当建立新的数据库时,默认会建立到/usr/local/m ...

最新文章

  1. ajax异步传输数据时return返回总是undefined(转载)
  2. 第16届东北赛区线上比赛斯赛点时间安排+直播链接
  3. C++五子棋(七)——main函数以及项目总结
  4. android怎么用别人的工程,Android导入别人的工程
  5. ADS_LPC2103开发板SPI 4位数码管测试试验
  6. 010. 使用.net框架提供的属性
  7. 服务器配置多个域名冲突
  8. 阅读软件怎么添加书源_认识一波苹果安卓手机上,那些以一敌百的小说漫画软件...
  9. Unity VR成像原理
  10. RN路由-React Navigation组件5.x-基本原理(中文文档)
  11. activiti 入门——activiti API (二)
  12. 90 后高管:“下不手开除 70、80 后,公司死了谁负责?”
  13. 理解Linemod匹配算法
  14. WebMatrix网站开发系列教程:第一讲 WebMatrix入门经典
  15. SourceTree 设置内置对比视图 不diff大文件
  16. python做线性回归统计推断提取参数_线性回归及其Python实现(最大似然法)
  17. 【spark on kubernetes】spark operator访问hdfs,hive
  18. 刘涵 美国 西北大学 计算机,西北大学关于表彰2010-2011学年度学生先进集体.doc...
  19. IBM Cloud 2015 - Invoice - 04 电汇Wire Transfer (Wired Pay, Tele Pay)
  20. CentOS 7 Crontab 用法

热门文章

  1. SAP 电商云 Spartacus UI 的 proxy facade 是如何调用实际实现类
  2. 关于 SAP Spartacus UI 框架选型问题
  3. TypeScript Downleveling - 什么是 TypeScript 的降级行为
  4. SAP BTP Kyma Runtime dashboard 打开报缺少缺陷的错误消息该如何解决
  5. SAP Spartacus的home page navigation逻辑
  6. Angular set函数和Component属性的命名冲突问题
  7. SAP CRM, C4C和Hybris Commerce的数据迁移策略
  8. Angular dependency injection - how injection is parsed
  9. 使用application log 分析navigation target解析错误
  10. SAP Fiori Launchpad tile跳转目标的解析逻辑