操作步骤;
1、检查tempdb的逻辑名字和它的存在位置。可以使用下面语句:
SELECT name, physical_name FROM sys.master_files WHERE database_id = DB_ID('tempdb');
2、停止数据库服务.
3、拷贝原来tempdb的文件到新的位置(原来文件位置可以通过上述查询得到)。拷贝完毕后,启动数据库服务。
4、执行如下命令:
USE master; GO ALTER DATABASE tempdb MODIFY FILE (NAME = tempdev, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb.mdf'); ALTER DATABASE tempdb MODIFY FILE (NAME = temp2, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb_mssql_2.ndf'); ALTER DATABASE tempdb MODIFY FILE (NAME = temp3, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb_mssql_3.ndf'); ALTER DATABASE tempdb MODIFY FILE (NAME = temp4, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb_mssql_4.ndf'); ALTER DATABASE tempdb MODIFY FILE (NAME = temp5, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb_mssql_5.ndf'); ALTER DATABASE tempdb MODIFY FILE (NAME = temp6, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb_mssql_6.ndf'); ALTER DATABASE tempdb MODIFY FILE (NAME = temp7, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb_mssql_7.ndf'); ALTER DATABASE tempdb MODIFY FILE (NAME = temp8, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb_mssql_8.ndf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\templog.ldf'); GO
在这里,name=tempdev 和templog 等是tempdb的逻辑名字,FILENAME = 'F:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\TempDB\tempdb.mdf' 是tempdb的新位置.
执行结果如下:
5、然后检查tempdb移动是否成功。
SELECT name, physical_name FROM sys.master_files WHERE database_id = DB_ID('tempdb');
6、接着重启数据库服务
7、注意在步骤3中,我们重新启动数据库服务的时候,还是会在老的tempdb路径下生成一套tempdb的数据库文件,现在它们都是没用的垃圾文件了,我们要去删掉它们。