简体   繁体   English

即使没有人连接也有单用户限制

[英]Single user restrictions even if nobody's connected

When using SQL Server Management Studio I try to query a DB created with "Single user" restrictions.使用 SQL Server Management Studio 时,我尝试查询使用“单用户”限制创建的数据库。

I log in by using SQL Credentials and "sa" user and by running sp_who2 command I double check nobody is connected to the DB, nevertheless when executing a query on a DB table, the system replies:我使用 SQL 凭据和“sa”用户登录,并通过运行 sp_who2 命令仔细检查没有人连接到数据库,但是在对数据库表执行查询时,系统回复:

Database 'MyDB' is already open and can only have one user at a time.数据库“MyDB”已经打开,一次只能有一个用户。

I'd like to understand why I am experiencing this issue.我想了解为什么我会遇到这个问题。 Can anybody help on this?有人可以帮忙吗?

When you set a db in "single user" mode you can only open a single connection to the database (more info here ).当你在“单用户”模式下设置一个数据库时,你只能打开一个到数据库的连接(更多信息在这里)。

So when you open SSMS to navigate through database objects you open the only permitted connection.因此,当您打开 SSMS 以浏览数据库对象时,您会打开唯一允许的连接。 After this any other operation that requires a new connection to the db will fail, for example creating a new query window to write a query.在此之后,任何其他需要与数据库建立新连接的操作都将失败,例如创建一个新查询 window 来编写查询。

From MS Docs:来自 MS Docs:

When you start an instance of SQL Server in single-user mode, SQL Server Management Studio can connect to SQL Server.当您在单用户模式下启动 SQL Server 实例时,SQL Server Management Studio 可以连接到 SQL Server。 Object Explorer in Management Studio might fail because it requires more than one connection for some operations . Management Studio 中的 Object 资源管理器可能会失败,因为某些操作需要多个连接 To manage SQL Server in single-user mode, execute Transact-SQL statements by connecting only through the Query Editor in Management Studio, or use the sqlcmd utility.要在单用户模式下管理 SQL 服务器,请仅通过 Management Studio 中的查询编辑器连接来执行 Transact-SQL 语句,或使用 sqlcmd 实用程序。

The same happens on premise if you have the SQL Server agent running.如果您正在运行 SQL 服务器代理,则会在本地发生同样的情况。

Another advice from MS Docs (if you are running SQL Server on premise): MS Docs 的另一个建议(如果您在本地运行 SQL 服务器):

Stop the SQL Server Agent service before connecting to an instance of SQL Server in single-user mode;在以单用户模式连接到 SQL Server 实例之前,停止 SQL Server Agent 服务; otherwise, the SQL Server Agent service uses the connection, thereby blocking it.否则,SQL 服务器代理服务会使用该连接,从而阻止它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM