So I have a third party vendor software that I have to call in an SQL Agent Job to restore my databases. So my first step is to set the database in single user mode, my second step is to call the software to start the restore. The problem is, the software takes so long to actually start the restore (its doing it all across the network) that another user can swoop in and take over the database.
Is there a way to put the DB in single user mode and then grab that connection right away? I have tried with a few "Select Getdate()" but I feel that is taking it from the system, and not the database. Do I have to select from a specific table in the database to get it to work?
Thanks
Rather than place the database in single user mode, take the database offline in the context of another database:
USE tempdb;
ALTER DATABASE DatabaseToRestore
SET OFFLINE WITH ROLLBACK IMMEDIATE;
Use the "Client Application Name" option along with the "-m" option that places SQL into single user mode.
That way only an application that matches the name given can connect and take the connection. Obviously you need to ensure that your client process that's doing the restore has a distinct name!
See here for info on the various SQL Server startup options: https://msdn.microsoft.com/en-us/library/ms190737.aspx
Also, I was under the impression you had to be an admin to connect in single user mode, which of course your normal users should not be. Please someone correct me if I'm wrong on that point!
Related
Our application has in-built functionality to update the database it uses (triggered when an app update is downloaded from our server.)
Usually, the update comprises of table and data conversion and then a drop of existing stored procedures and a create of all current stored procedures.
So we do the following from C#:
Create Connection
Set database to Single User Mode
Run upgrade script to upgrade tables and convert data
Run upgrade script to drop existing stored procedures and add new ones
Set database to Multi User Mode
Close connection
Step 2 -5 are done using the same connection. For some clients it occassionaly errors out in the middle of Step3 or Step 4. The log shows:
Error: Database 'MyDB' is already open and can only have one user at a time.
This is very strange because at that time we are already in Single User mode so no other user should be able to connect to the database, let alone somehow put it in Single User mode for its own connection.
We are wondering if some internal SQL Server process is taking over our Single User mode and preventing us from completing the update.
We are aware that if AUTO_UPDATE_STATISTICS_ASYNC is turned ON this may interrupt Single User Mode, but we have verified that it is turned off and it still occurred for the client.
Could a CHECKPOINT operation interrupt it? If so, what do we do about that, because that is a server-wide setting so we cannot modify it.
I have a question regarding database mirroring. I've tried reading the documentation, I think I understand it, but - it's a production database and I really don't want to mess this up!!
I have a primary server - called COMPANYSQL - with the primary database. In SQL Server Manager it says DBNAME (Principal, Disconnected).
I have a backup server - called COMPANYREMOTE - with the one-way mirror of the database for failover purposes. It says DBNAME (Mirrored, Suspended / Restoring).
To re-establish the mirror, I think I can right click on the COMPANYREMOTE database, choose Tasks > Launch Database Mirroring Manager.
The I see under the Database Mirroring Monitor:
DBNAME (Suspended, COMPANYSQL->COMPANYREMOTE\MIRRORED-DBNAME).
I can right click and choose "Register Mirrored Database".
From here I get confused. It tells me to select the principal DB, so from the first dropdown I choose COMPANYSQL and the primary database (DBNAME) appears in the list. Current role is Principal and Partner (Connect As) is COMPANYREMOTE\MIRRORED-DBNAME.
I think if I hit Ok here...it'll re-establish the mirror?
Just don't want to mirror it the other way by mistake and overwrite anything on my production.
THANKS!
Run below mention command in Principal Node
Alter database DBNAME set partner resume
Thanks,
Mani
Eh, no responses, it's ok. I got some advice and it turns out it's best to delete the mirror and re-create it when it gets messed up. In my case, there was approximately 30GB of unsent data that was going to take forever to mirror so it chokes out.
Ok, so I'm having a bit of an issue - I executed some code on my SQL Server and didn't realize that I didn't have the WHERE bit selected. Of course, when I saw the "608 rows affected" rather than "1 row affected", I freaked a bit.
Luckily, I have a backup saved, but for some reason, I'm getting a couple issues. Now, I took the server down, so I know that it's not being used by anyone, but it's giving me the following error
"Restore failed for Server 'myserver'.
System.Data.sqlclient.sqlerror: Exclusive access could not be obtained
because the database is in use. (Microsoft.SqlServer.Smo)"
I saw something that stated I should be using
Alter Database Databases
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE PRODUCT
FROM DISK = ''
but I'm having three reservations about this code. First, I'm completely unsure of how to turn multi_user back on. Second, I don't know where the program stores its backups. Third, this SQL is a bit above my head - I'm relatively new to the language, honestly, so I'm not sure how this will affect things.
Anyone have any answers to my troubles?
I might suggest instead of overwriting the existing database from the backup, that you instead recover the backup with a different database name... this way you can have the current and previous databases side-by-side.
Then, you can simply write an update statement to recover the data from just that one specific table instead of resetting the whole database.
EDIT: The specifics would depend on your environment, but the restore would look something like this:
restore database PRODUCT_OLD
from disk='C:\PRODUCT.bak'
with
move 'PRODUCT_Data' to 'C:\PRODUCT_OLD_Data.MDF',
move 'PRODUCT_Log' to 'C:\PRODUCT_OLD_Log.LDF'
And then the update statement would also be based on your specific table...
right on the database click tasks->takeoffline , when its succeed do the same thing but put it Bring Online
then try to restore your database
Set the database to single user is correct. When you are complete with your restoration you'll execute this
alter database YourDb
set multi_user
As for where your .bak file resides, you'll have to find it prior to restoring.
Scary.
Ok, some things to check:
Make sure you are the only person connected to the server.
Make sure no other applications, web servers, app servers, etc. hold connections to your DB.
Make sure your SQL manager has no open windows to tables or other objects in your database.
THEN you should be able to do the restore w/o single user stuff.
Go to the activity Monitor and see if users are still logged in then kill the process for that user using the respective db.
Then go ahead restore the backup
One of the most important databases that I administer is poorly designed. The program which uses it uses only 1 login which happens to be a System Administrator. Edits and deletes are done in place so change tracking is difficult.
Further, it lacks proper auditing functionality so I cannot tell which user edited or deleted a certain record.
I thought I might resort to using SQL triggers and output the record information along with information about the user who edited/deleted it. I can't use LoginName because the program only uses one login.
Can I export other information using a trigger such as the desktop machine name which sent the delete command?
Is there any other information that I could export that would assist me in this?
You might look at the sys.sysprocesses table (sysprocesses in SQL Server 2000 I think). It contains information that you normally see in the output of the sp_who2 stored procedure. It includes hostname, which is the computer that is connecting to SQL Server. Some of the other columns may be useful too.
I am using SQL Server 2008 Enterprise for development. I find from SQL Server logs, there are items like,
2009-09-20 19:54:33.55 spid53 Starting up database 'DummyOrderDB'.
My confusion is, I think we could only start/stop database server instance (the contained database will be started/stopped when the containing database server instance start/stop), can we just start/stop a database without touch database server instance? I did not find such menu from SSMS.
thanks in advance,
George
That is an auto close database. Auto-close databases are 'closed' when not in use and each time an user uses one, they run a short recovery and the text above is displayed. SQL Express creates databases as auto close ON by default. To turn off the auto-close behavior, run:
ALTER DATABASE <dbname> SEt AUTO_CLOSE OFF;
Yes, we can. Of course Starting and Stopping databases only make sense when the server itself is started (that helps ;-) ), but each individual database has to be, say, initialized before it can be used in earnest. Also when you detach a database, it first shuts down. (which ensures data integrity and other clean-up are taken care of etc.)