系统数据库和用户数据库日志都丢失的情况下,数据库无法启动,需要先重建系统数据库日志文件以便把sqlserver service拉起来,再重建用户数据库
试过但是行不通的办法
1、-mClient单用户模式下启动数据库并重建系统数据库日志,即使用net start "SQL Server (MSSQLSERVER)" -m"Microsoft SQL Server Management Studio - Query"进入数据库重建系统数据库日志,出现报错,系统数据库日志不允许重建
System databases master, model, and tempdb cannot have their logs rebuilt.
2、设置启动项-T3608,即在SQL Server Configuration Manager里面对应的sqlserver service服务添加启动项参数-T3608,sqlserver service服务器还是起不来
-T3608 禁止 SQL Server 自动启动和恢复除 master 数据库之外的任何数据库。
3、使用安装包repair现有sqlserver实例,行不通,因为repair其实是需要sqlserver实例有startup handle,而系统数据库日志都丢失的情况下,sqlserver实例压根无法启动,也就么有startup handle的概念,所以repair一个没有系统数据库日志的sqlserver实例会报错:Could not find the Database Engine startup handle
4、完全卸载,再重新安装,这个时候是可以把数据库拉起来的,不过master库的东西和msdb库的东西都丢失了,不过可以重建msdb库,重建msdb的前提是master和model库必须正常,也就是一个数据库能用,必须保证master和model库正常,因为一个sqlserver 实例能否使用必须有tempdb,而tempdb依赖model,model依赖master
Sqlserver系统数据库和用户数据库日志文件丢失的恢复方法
1、系统数据库有备份的话,可以把系统数据库以重命名的方式当成用户数据库恢复到其他实例,但是必须保证恢复后的数据文件和日志文件的名称和之前一样,再把恢复后的数据文件和日志文件拷贝回出问题的实例。比如A实例的系统数据库master的日志文件丢失了,可以把A实例的master库恢复到B实例master_test库,但是必须保证B的master_test库的数据文件和日志文件名称和A库的master库的数据文件和日志文件名称一致,再把B的master_test库的数据文件和日志文件名称拷贝回A实例master库对应的目录,这样A实例就可以启动了
2、如果系统数据库没有备份,则在其他实例上建立一个数据库,数据文件和日志文件名称和丢失的系统数据库一样,再把新建的数据库offline,把这个新建数据库的数据文件和日志文件改名或拷贝到其他目录,再把丢失日志的系统数据库数据文件拷贝到这个新建数据库的对应的目录,再执行rebuild log,这样这个数据文件就自动生成了日志文件,再把这个系统数据库数据文件和日志文件拷贝回丢失系统数据库日志文件的目录,这时这个曾经丢失了系统数据库日志文件的sqlserver实例就可以启动了,参见如下示例
假如A实例的master和model和msdb的日志文件都丢了
2.1、在B实例建立和master、model、msdb同名数据库文件FILENAME的用户数据库master_test、model_test、msdb_test
1 2 3 4 5 6 7 8 9 10 11 12 |
CREATE DATABASE [master_test] ON PRIMARY ( NAME = N 'master' , FILENAME = N 'E:\master.mdf' ) LOG ON ( NAME = N 'mastlog' , FILENAME = N 'E:\mastlog.ldf' ) GO CREATE DATABASE [model_test] ON PRIMARY ( NAME = N 'modeldev' , FILENAME = N 'E:\model.mdf' ) LOG ON ( NAME = N 'modellog' , FILENAME = N 'E:\modellog.ldf' ) GO CREATE DATABASE [msdb_test] ON PRIMARY ( NAME = N 'MSDBData' , FILENAME = N 'E:\msdbdata.mdf' ) LOG ON ( NAME = N 'MSDBLog' , FILENAME = N 'E:\MSDBLog.ldf' ) GO |
2.2、在B实例对这三个用户数据库master_test、model_test、msdb_test进行offline脱机操作
1 2 3 |
alter database [master_test] set offline alter database [model_test] set offline alter database [msdb_test] set offline |
2.3、再在B实例把master_test、model_test、msdb_test数据文件和日志文件拷贝到其他目录或改名,再把A实例的master、model、msdb数据文件拷贝到B实例master_test、model_test、msdb_test数据库对应目录,再执行REBUILD LOG,这样A实例的master、model、msdb数据文件其实就有日志文件,把这些数据文件和日志拷贝会A实例对应目录,A实例就可以启动了
1 2 3 |
ALTER DATABASE [master_test] REBUILD LOG ON ( NAME =master_log, FILENAME= 'E:\mastlog.ldf' ); ALTER DATABASE [model_test] REBUILD LOG ON ( NAME =modellog, FILENAME= 'E:\modellog.ldf' ); ALTER DATABASE [msdb_test] REBUILD LOG ON ( NAME =MSDBLog, FILENAME= 'E:\msdblog.ldf' ); |
会有如下警告,说明生成了新的日志文件
Warning: The log for database 'master_test' has been rebuilt. Transactional consistency has been lost. The RESTORE chain was broken, and the server no longer has context on the previous log files, so you will need to know what they were. You should run DBCC CHECKDB to validate physical consistency. The database has been put in dbo-only mode. When you are ready to make the database available for use, you will need to reset database options and delete any extra log files.
Warning: The log for database 'model_test' has been rebuilt. Transactional consistency has been lost. The RESTORE chain was broken, and the server no longer has context on the previous log files, so you will need to know what they were. You should run DBCC CHECKDB to validate physical consistency. The database has been put in dbo-only mode. When you are ready to make the database available for use, you will need to reset database options and delete any extra log files.
Warning: The log for database 'msdb_test' has been rebuilt. Transactional consistency has been lost. The RESTORE chain was broken, and the server no longer has context on the previous log files, so you will need to know what they were. You should run DBCC CHECKDB to validate physical consistency. The database has been put in dbo-only mode. When you are ready to make the database available for use, you will need to reset database options and delete any extra log files.
3、再回到A实例,在A实例执行如下语句,把生成的语句再执行一遍就可以rebuild 用户数据库的日志文件,并设置数据库的访问限制为多用户
1 2 3 4 |
select 'ALTER DATABASE [' +db_name(database_id)+ '] REBUILD LOG ON (NAME=' + name + ', FILENAME=' '' +physical_name+ '' ');' from master.sys.master_files where type_desc= 'LOG' and database_id>4 select 'ALTER DATABASE [' +db_name(database_id)+ '] SET MULTI_USER WITH NO_WAIT' from master.sys.master_files where type_desc= 'LOG' and database_id>4 |
版权申明:本站文章均来自网络,如有侵权,请联系01056159998 邮箱:itboby@foxmail.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有