如何防止 SQL Server LocalDB 自动关闭?

25

我正在使用 SQL Server 2012 Express LocalDB。如果实例没有活动,它们似乎会在10分钟后自动停止。是否有一种干净的方式让实例永远运行下去?

2个回答

33

可以通过 T-SQL 中的 'user instance timeout' 选项对超时时间进行配置:

sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
sp_configure 'user instance timeout', 5;
GO

超时时间以分钟为单位表示,最大值为65535。设置后,我相信您需要重启实例。请不要尝试将其设置为0,这只会使实例在启动后立即关闭,这将使将值重新设置为有用的值变得困难 :-)。
来源:此 BOL 文章包含其他适用于 LocalDB 实例的有用信息。 最终建议 如果您需要始终运行并在计算机启动时启动的内容,您可能只需考虑使用常规的基于服务的 SQL Server Express 实例。

1
请注意,timeout选项的最小值为5,因此无法将超时值设置为低于5分钟。 - Uttam

16

以下是如何在命令行中执行Krzysztof Kozielczyk的答案。

启动localdb实例。

C:\> sqllocaldb start v11.0
LocalDB instance "v11.0" started.
获取服务器路径,即实例管道名称。
C:\> sqllocaldb info v11.0
Name:               v11.0
Version:            11.0.3000.0
Shared name:        IIS_DB
Owner:              DESKTOP-AAAT5QS\bigfo
Auto-create:        Yes
State:              Running
Last start time:    2/17/2016 12:06:43 PM
Instance pipe name: np:\\.\pipe\LOCALDB#SH9D87FB\tsql\query

在该服务器上运行一个 SQL 命令。

C:\> sqlcmd -S np:\\.\pipe\LOCALDB#SH9D87FB\tsql\query

1> sp_configure 'show advanced options', 1;
2> GO

Configuration option 'show advanced options' changed from 1 to 1. 
Run the RECONFIGURE statement to install.

1> RECONFIGURE;
2> GO

1> sp_configure 'user instance timeout', 5;
2> GO

Configuration option 'user instance timeout' changed from 5 to 5. 
Run the RECONFIGURE statement to install.

1> RECONFIGURE;
2> GO

> exit

我没有名字管道,所以我使用了这个 **sqlcmd -S "(localdb)\MSSQLLocalDB"**。 - bh_earth0
2
要查看当前设置,请使用 sp_configure 'user instance timeout' - Nelson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接