场景:
前几天接到一个朋友的电话,说他们由于将TempDB设置错误导致SQL Server无法重启,让我帮忙看看。
他们本来是想将TempDB的文件挪动到其他路径,比如准确的脚本应该是:
USE master GO ALTER DATABASE tempdb MODIFY FILE (NAME = tempdata_01, FILENAME = 'D:SQL2K14_DATAMSSQL12.MSSQLSERVERMSSQLDATA empdb.mdf') GO ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'D:SQL2K14_DATAMSSQL12.MSSQLSERVERMSSQLLOG emplog.ldf')
但是他们却写成了:
USE master GO ALTER DATABASE tempdb MODIFY FILE (NAME = tempdata_01, FILENAME = 'D:SQL2K14_DATAMSSQL12.MSSQLSERVERMSSQLDATA') GO ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'D:SQL2K14_DATAMSSQL12.MSSQLSERVERMSSQLDATA')
之后,未经检查,就直接重启了SQL Server服务,最后导致服务无法被启动。
查看数据库和Windows的错误日志,错误信息为:
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file '
D:SQL2K14_DATAMSSQL12.MSSQLSERVERMSSQLDATA'.
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file '
D:SQL2K14_DATAMSSQL12.MSSQLSERVERMSSQLDATA'.
解决步骤:
1.先将正确的Alter Database Tempdb的脚本放在某个路径下,比如我这里就放在D:TempModifyTemp.sql
USE master GO ALTER DATABASE tempdb MODIFY FILE (NAME = tempdata_01, FILENAME = 'D:SQL2K14_DATAMSSQL12.MSSQLSERVERMSSQLDATA empdb.mdf') GO ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'D:SQL2K14_DATAMSSQL12.MSSQLSERVERMSSQLDATA emplog.ldf')
2.在命令行中使用单用户模式启动SQL Server服务:
C:Userswh42>net start MSSQLServer /f /T3608 /m
The SQL Server (MSSQLSERVER) service is starting.
The SQL Server (MSSQLSERVER) service was started successfully.
3.在该cmd窗口中,使用sqlcmd来执行第一步中的脚本:
在cmd中先进入脚本所在的路径,之后执行该脚本:
C:Userswh42>d: D:>cd Temp D:Temp>sqlcmd -iModifyTempDB.sql Changed database context to 'master'. The file "tempdata_01" has been modified in the system catalog. The new path wil l be used the next time the database is started. The file "templog" has been modified in the system catalog. The new path will be used the next time the database is started.
4.根据上面的提示,我们已经将TempDB的对应的meta信息修改完毕,此时我们只需要关闭该窗口,再使用正常模式重启SQL Server服务即可。
这样,我们就解决了由于设置TempDB错误,导致无法启动SQL Server服务的问题了。