主要环境相关因素:win7,ms sql 2012,ms localdb,msms 2012.

步骤:

1,让DBA给一个bak文件到本地来做测试,DBA按自己的工作流程得到bak文件。

2,在msms 2012中创建同样名称的数据库,按我的想法设置mdf和ldf的路径。

3,,开始用上次已经成功过的脚本开始在msms 2012执行脚本。报错:

The logical database file cannot be found ldf,xxxx.ldf can't find。

首先说明为什么用脚本而不直接在管理器中通过UI还原,因为我这边安装完LocalDB后,它的master文件在c盘用户文件夹下面,导致总是会有不能访问master数据库的问题。在上次还原文件的时候,这个问题也折腾了一番,不过由于遇到的人多,马上就找到使用脚本的解决方案了。

没理由mdf文件可以找到,但是日志文件找不到啊。网上搜很久,最后有个 哥们说:

I'm having this problem too.  I managed to deduce that it has something to do with the locations of the original data and log files.  If you look at the file list you'll notice that the data and log files from the original database were stored in different locations.  If the files were stored in the same location the restore would work successfully.

If you want to try this out restore the database in a full SQL Server 2012 instance (Standard or better) making sure the data and log files are stored in the same folder.  Make a back-up of that database and restore it in your LocalDb instance.  Now restore the same database on your full SQL Server instance placing the data and log files in separate folders, create a backup, and try to restore this in your LocalDb instance.  You should get the same error message.

I've submitted feedback via Connect: https://connect.microsoft.com/SQLServer/feedback/details/789845.

以上说了两点:

1,本地新加数据库物理文件路径要保持和bak文件中的一致

即是bak中的文件名称和新数据库名称一致,同时bak文件中的mdf和ldf文件的路径和新数据库中的一致。这一点经过本人验证,只需要逻辑名一致就行了。物理路径和名称可以不一样。

如何查看bak文件物理和逻辑结构:

restore filelistonly
 FROM DISK = 'D:\works\backup\web\xxx.bak'

2,mdf文件和log文件要在同一目录下。

而我这里为什么会出错的原因是,DBA把mdf放在data/*.mdf下,日志放在log/*.ldf下。更牛逼的是他在sql 2012 msms 操作总是成功,导致我没法怀疑他的操作。而在localdb中,它是从mdf来推断ldf路径的,所以总是找不到ldf文件。

最后让DBA把mdf,ldf放同一目录下问题搞定。

附上脚本:

--避免出现:

--Exclusive access could not be obtained because the database is in use

Alter Database SLdb
  SET SINGLE_USER With ROLLBACK IMMEDIATE

RESTORE DATABASE dbname
 FROM DISK = 'D:\works\backup\web\***.bak'
 WITH REPLACE,
 MOVE 'YYYY' TO 'D:\demos\Db\XXXX.mdf',
 MOVE 'YYYY_log' TO 'D:\demos\Db\YYYY_log.ldf',
 STATS = 1
 GO

--再设置回来

alter database *****
set multi_user

还原bak到localdb的问题:The logical database file cannot be found ldf的更多相关文章

  1. sql server启动服务和还原bak文件

    sql server启动服务和还原bak文件, sql server启动要: mysql数据库备份是psc后缀文件, sql server还原数据库备份bak文件: 三张图简介明了: ok:

  2. sql还原(.bak文件还原)

    第一步: 右键“数据库”,选择“还原数据库” 第二步: 选择“设备”,然后选择“…” 第三步: 添加备份文件(这里使用MyDB.bak) 第四步: 勾选“还原”复选框,进度显示“已完成” 第五步: 最 ...

  3. 用SQL2000还原bak文件

    1.右击SQL Server 2000实例下的“数据库”文件夹.就是master等数据库上一级的那个图标.选择“所有任务”,“还原数据库” 2.在“还原为数据库”中填上你希望恢 复的数据库名字.这个名 ...

  4. Sqlserver数据库还原.bak文件失败的两个问题

    一.SQL Server数据库备份还原后,在数据库名称后会出现“受限制访问”字样      解决方案:将数据库限制访问改为:SINGLE_USER 数据库-->属性-->选项-->状 ...

  5. VS2010升级VS2012必备(MVC4 WebPage2.0 Razor2.0资料汇集)

    刚把项目升级到2012,发现发生了很多变化,以下是最近看过的网站和资料汇集,供需要者参考. 本文在最近一个月可能会不断更新. Razor2.0 新特性介绍: 介绍1:http://vibrantcod ...

  6. 13 oracle数据库坏块-逻辑坏块(模拟/修复)

    13 oracle数据库坏块-逻辑坏块 逻辑数据坏块的场景1)oracle bug也可能导致逻辑坏块的产生. 特别是parallel dml. 例如:Bug 5621677 Logical corru ...

  7. RMAN迁移数据库(不改变文件目录)

    1.目标库创建相应目录mkdir -p /u01/app/oracle/oradata/orclmkdir -p /u01/app/oracle/fast_recovery_area/ORCLmkdi ...

  8. vs2012中使用localdb实例还原一个sql server 2008r2版本的数据库

    use localdb sometime is easy than sql server ,and always use visual studio make you stupid. vs2012中还 ...

  9. RESTORE DATABASE命令还原SQLServer 数据库 bak

    今天在sqlServer20005 的management studio里使用bak文件还原数据库的时候,总是失败!Restore failed for Server 'ADANDELI'.  (Mi ...

随机推荐

  1. Erlang Web 监控工具

    转自http://www.cnblogs.com/me-sa/archive/2012/04/17/erlang-web-monitor.html  Erlang已经提供了一系列工具查看运行时状态查看 ...

  2. suse 安装gcc

    1)挂载ISO镜像 新建一个目录: mkdir /mnt/iso 将ISO文件挂载到该目录上: mount -o loop /opt/SLES-11-SP3-DVD-x86_64-GM-DVD1.is ...

  3. 一.jQuery源码解析之总体架构

    (function (window, undefined) { //构建jQuery对象 var document = window.document, navigator = window.navi ...

  4. div+css 命名规则

    编写网页的时候,一般都没有把命名规则认真对待.在结合语义以及搜索优化两方面给出了一套命名方案,对增强阅读性.规范性和提高开发效率是有帮助的,当然,这方面的规范,仁者见仁智者见智,网上有许多前辈们以经验 ...

  5. 开启php的xdebug扩展及phpstorm配置xdebug,chrome调试插件组合

    一. 开启php xdebug扩展      注意:     1. 原生php各版本需对应各自的xdebug版本,可到xdebug上对应下载     2. 若用wampserver等环境,wampse ...

  6. 迷你MVVM框架 avalonjs 0.97发布

    在本版本中,王之三柱臣全部就位! mmRouter: https://github.com/RubyLouvre/mmRouter mmAnimate: https://github.com/Ruby ...

  7. 转化RGB颜色为CMYK颜色

    procedure RGBTOCMYK(R : byte; G : byte; B : byte; var C : byte; var M : byte; var Y : byte; var K : ...

  8. 22.Generate Parentheses (String; Back-Track)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 使用SQL查询所有数据库名和表名

    使用SQL查询所有数据库名和表名 MySQL中查询所有数据库名和表名 查询所有数据库 show databases; 1 1 查询指定数据库中所有表名 select table_name from i ...

  10. input checkbox 复选框大小修改

    设置zoom属性(放大) 利用style: <input type="checkbox" name="returnfee"  style="zo ...