主要环境相关因素: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. (接上一条)解决ssh隧道断开自动重连的问题

    Sounds like you need autossh. This will monitor an ssh tunnel and restart it as needed. http://www.d ...

  2. linux svn soeasy

    http://blog.163.com/longsu2010@yeah/blog/static/173612348201202114212933/

  3. 嵌入式linux开发:杂七杂八的话

    1. 编译器:常用的有Sourcery,linaro等.当然芯片厂家一般也会提供可用的编译器. 2. 将配置和编译时的输出O到其他位置:make O=some_dir 这样做的好处是保持源代码树的干净 ...

  4. bzoj3802: Vocabulary

    Description 给你三个字符串,这些字符串有些单词模糊不可认了,用"?"来代表. 现在你可以用任意英文小写字母来代表它们.要求是使得给定的三个字符串中 所有的"? ...

  5. 走迷宫(用队列bfs并输出走的路径)

    #include <iostream> #include <stack> #include <string.h> #include <stdio.h> ...

  6. 20165233 学习基础和C语言基础调查

    学习基础与<做中学>阅读心得 读<做中学>有感 娄老师通过在学习工作中总结出的"做中学"的方式,将其运用到减肥.五笔训练.乒乓球训练以及英文单词背诵的过程中 ...

  7. Spring cloud config-client 爬坑

    配置文件 找不到属性 Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'from' in st ...

  8. sqlplus 方式连接 远程数据库

    方式一:简易连接,不用进行网络配置,其实就是tnsname.ora文件,但只支持oracle10G以上.命令:sqlplus 用户名/密码@ip地址[:端口]/service_name [as sys ...

  9. intellij idea15,SVN commit file提示No changes detected

    备注:我用的intellij 15,已经配置了SVN.且我的工程是从SVN导出的   遇到的问题:Subversion提交代码时提示No changes detected,当然新文件想要add也是选项 ...

  10. Bourne Shell:控制语句、条件判断、文本处理、常用命令

    条件判断是一个程序获得智能的基础,而Bourne Shell脚本则通过命令 [ 来模拟大多数编程语言中的条件表达式. shell中支持的控制结构有: (1) if then else fi (2) f ...