还原bak到localdb的问题:The logical database file cannot be found ldf
2014-04-05 16:21
465 查看
主要环境相关因素: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放同一目录下问题搞定。
附上脚本:
--避免出现:
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
步骤:
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 SLdbSET 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
相关文章推荐
- myeclipe eclipse 常遇问题:Some projects cannot be imported 、java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver、The file connot be validate
- 在线安装maven插件问题:Cannot complete the install because one or more required items could not be found.
- Caused by: Axis2:The services.xml file cannot be found for the service
- 新版本 Linux Deploy 部署 Debian 提示 E:the selected extractor cannot be found:ar 问题
- The Struts dispatcher cannot be found在struts2中出现的问题
- MyEclipse Error: The Table cannot be found on the database
- 解决Entity Framework Code First 的问题——Model compatibility cannot be checked because the database does not contain model metadata
- ”The MathType DLL cannot be found.“问题解决方法
- MyEclipse中frame引起的“the file XXX can not be found.Please check the location and try agai问题
- Database 'xxx' cannot be upgraded because it is read-only or has read-only file Make the database or files writeable, and rerun recovery.
- Struts2学习——0800 Included file cannot be found问题
- 初学struts,The Struts dispatcher cannot be found问题
- 如何解决打开xcode项目是遇到的“cannot be opened because the project file cannot be parsed.”问题?
- struts2的“The file cannot be validated as the host "struts.apache.org" is currently unreachable"问题解决
- "The MathType DLL cannot be found.Please reinstall MathType"问题解决
- The Struts dispatcher cannot be found在struts2中出现的问题
- 在线安装maven插件问题:Cannot complete the install because one or more required items could not be found.
- 解决MySQL导入文件问题(The selected file was generated by mysqldump and cannot be restored by this applicati)
- yii提示The table "xxx" for active record class "DistAR" cannot be found in the database
- MyEclipse消除frame引起的the file xxx cannot be found