Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change to database to 'online' and set db to 'multi-user'

I have a 2008 sql database that is offline that I would like to take online and set to multi-user. Using sql server management studio - new query window - when i execute the following:

   ALTER DATABASE mydb SET ONLINE;

    ALTER DATABASE mydb SET MULTI_USER;

I receive this error message:

Msg 5064, Level 16, State 1, Line 1 Changes to the state or options of database 'mydb' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it.
Msg 5069, Level 16, State 1, Line 1 ALTER DATABASE statement failed. Msg 5064, Level 16, State 1, Line 3 Changes to the state or options of database 'mydb' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it. Msg 5069, Level 16, State 1, Line 3 ALTER DATABASE statement failed.

How would I get the database online and in multi-user mode?

like image 648
FiveTools Avatar asked Jul 05 '11 18:07

FiveTools


People also ask

How do I change my database to multi user mode?

Scroll down to get 'STATE' field. Select Restrict access option from STATE field. Select MULTI_USER from dropdown menu. Click 'OK'.

How do I take my database from single user mode online?

Use SQL Server Management StudioRight-click the database to change, and then select Properties. In the Database Properties dialog box, select the Options page. From the Restrict Access option, select Single. If other users are connected to the database, an Open Connections message will appear.

Can a database have multiple users?

A multiuser environment is one in which other users can connect and make changes to the same database that you are working with. As a result, several users might be working with the same database objects at the same time.


1 Answers

a user is currently connected to it

^ This is the problem you need to solve.

Make sure you are not IN that database. Close any query windows that are connected to it, shut down Object Explorer Details, close SSMS and re-open it without Object Explorer connected to that server, etc. Run this:

USE [master];
GO

ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
USE mydb;
GO

That should allow you to bring it online, then you would run the commands you listed.

However:

  • This can take longer than you might, depending on what rollback activity has to happen to the sessions you're kicking out.
  • It's always possible that when you set it to single user, another process can be faster than you and take that single connection. If you find that this happens, you can figure out who it is using sp_whoisactive or DMVs like sys.dm_tran_locks - it may be that you need to suspend connecting applications and/or shut down SQL Server Agent.
like image 193
Aaron Bertrand Avatar answered Sep 26 '22 08:09

Aaron Bertrand