I have a database that we are having problems with. Somehow the log has became 400 Gb and the database has been rendered useless. I want to drop all existing connections to the database and then detach the database.
Basically what I'm going to do is get rid of the giant log file and create a new one and reattach if it works. If not, we're going going to restore from backups.
If the log is useless, you can use these commands, but please document on them yourself before applying on a production server.
BACKUP LOG WITH NO_LOG for disgard the pages from log,
sp_helpdb for looking the name of the files of the db
DBCC SHRINKFILE('your log filename ', 0) -- for trunking the physical file to the size specified.
If you are sure there are no open transactions you can put the database in single user mode.
ALTER DATABASE [YourDB] SET SINGLE_USER WITH NO_WAIT
When you are done put it back in multi user mode
ALTER DATABASE [YourDB] SET MULTI_USER WITH NO_WAIT
Does
backup log yourdb with truncate_only then dbcc shrinkdatabase(yourdb) not shrink the logfile for you ?
Well basically no commands would execute agaisnt the database, at all. What we ended up doing was turning off the service and creating empty copies of the mdf and ldf files and replacing the ones being used by sql server. After that we restored the database from the last backup and voila, it's working again (mostly).
ALTER DATABASE [DB_NAME_HERE] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE [DB_NAME_HERE] SET MULTI_USER
Related
Has anyone run into this error even when the database is deleted? I deleted the database and checked the "close existing connections" box before deleting.
I have a Live db and a Test Db. I have made a backup of Live. When I try to restore Live.bak to a database with name Test, I get the exclusive access error. I need to copy the Live db over Test.
Funny thing is I can restore a backup of Test if needed.
It is always better to close existing connections before deleting
ALTER DATABASE MyDB
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
Reattach it with same name and rename the database. Or restore with norecovery
RESTORE DATABASE TEST FROM DISK = 'C:\Live.BAK' WITH NORECOVERY
RESTORE LOG TEST FROM DISK = 'C:\LIVELOG.trn'
-- Moving file lcoations
WITH MOVE 'MDFLogicalName' TO 'C:\test.mdf',
MOVE 'LDFLogicalname' TO 'D:\Test.ldf'
Having executed a DB deploy (from a VS SQL Server database project) on a local database, which failed, the database has been left in a state where it has single user mode left on (the deploy runs as single user mode).
When I connect to it from SSMS and try something like the following:
ALTER DATABASE MyDatabase
SET MULTI_USER;
GO
I get the error:
Changes to the state or options of database 'MyDatabase' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it.
I tried taking the database offline, which SSMS tells me succeeds, but it doesn't appear to actually do anything. So far, I've only been able to get around this by dropping and recreating the database (which is kind of okay, because it's only a local test database). However, I'd like to be able to reset the status.
How can I convince SQL Server to take this database out of single user mode?
In first run following query in master database
exec sp_who
If you can't find the culprit, try
SELECT request_session_id FROM sys.dm_tran_locks
WHERE resource_database_id = DB_ID('YourDatabase')
Then kill all process that use your database with following query:
KILL spid
Then run following query:
USE Master
ALTER DATABASE YourDatabase SET MULTI_USER
Try the below commands
First run these three commands
USE [master]
SET DEADLOCK_PRIORITY HIGH
exec sp_dboption MyDBName, 'single user', 'FALSE';
Second run these two commands
ALTER DATABASE MyDBName SET MULTI_USER WITH NO_WAIT
ALTER DATABASE MyDBName SET MULTI_USER WITH ROLLBACK IMMEDIATE
This was answered here, the code is:
use master
ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
--do you stuff here
ALTER DATABASE YourDatabase SET MULTI_USER
Use DAC (Dedicated Admin Connection). Make sure you have enabled it first
In SSMS type in admin: for Server Name
after connecting to master ALTER DATABASE SET MULTI_USER
To force the update use " with rollback immediate"
ALTER DATABASE [DATABASE_NAME] SET MULTI_USER with rollback immediate
I have deleted the .ldf file. But after that I am unable to recover database.
I have tried to detach and attach database but it is throwing exception.
“The database [dbName] is not accessible. (ObjectExplorer)”
I have also tried to create a new .ldf file with 0 byte size but database recovery fails.
There is no backup file for database.
I have gone through a post
The database [dbName] is not accessible. (ObjectExplorer)
But this is related to permission where as my issue is related to deletion of .ldf file.
I do not need log file. I just want to recover my data. Transaction log is not important for me.
When I deleted .ldf file SQL server was running. I didn't stopped it at that time.Later I had restarted it.
Suppose if your database name is xyz, then run following command:
ALTER DATABASE xyz REBUILD LOG ON ( NAME = xyz_log, FILENAME ='c:\.....\xyz_log.ldf');
DBCC CHECKDB (xyz);
ALTER DATABASE xyz SET SINGLE_USER;
DBCC CHECKDB (xyz, REPAIR_ALLOW_DATA_LOSS);
ALTER DATABASE xyz SET MULTI_USER;
You may lose some data. This command doesn’t guaranteed full recovery.
I'm trying to get a copy of an MDF file but coming across the standard "file is in use" message which I believe is the SQL service holding a lock on it. Is it possible to make a copy of the mdf file without having to stop the service or in any way affect users/applications?
If not, is it possible to create a once-off full backup mdf to a different location than the existing one so that it wouldn't be locked by SQL service?
(Variations on this have been asked on this site already, but I don't think this is a duplicate. I am not trying to relocate the database, simply attempting to take a copy of the mdf - without interrupting Live operations - so I can place it on my dev machine at home and play around with the database)
this will let u make a backup :
BACKUP DATABASE MyDatabase TO DISK = 'W:\DBs\MyDatabase.bak' WITH INIT;
and if u want to backup the log file as well then just add this command before the previous one :
ALTER DATABASE MyDatabase SET RECOVERY FULL;
Then if you want to recover the database from the backup file try this command:
USE [master]
ALTER DATABASE MyDatabase
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
-- The previous command will used to close all connections to the database
-- until you recover it
Restore Database MyDatabase From Disk = 'W:\DBs\MyDatabase.bak' WITH REPLACE;
ALTER DATABASE MyDatabase SET MULTI_USER
-- This will reopen the database for users connections
I have a database 'My Database' which I would like to rename so that there is no white space. I tried to rename it using
use master
exec sp_renamedb 'I 3 SCI Study','I3SciStudy'
and was greeted with the error
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.
This server is my local machine and I have no other query windows open but the window in which I ran the rename query. Is there some sort of close connection command that I need to run before I can rename the database?
Try this command, but caution is advised:
USE master;
ALTER DATABASE [dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [dbname] SET MULTI_USER;
GO
Also you can interrogate information about currently active lock manager resources.
SELECT *
FROM sys.dm_tran_locks DTL
WHERE DTL.[resource_database_id] = DB_ID()
Each row represents a currently active request to the lock manager for a lock that has been granted or is waiting to be granted. You will see not only your request on current database(most likely with resoure_type DATABASE). It is impossible to change a database name while these resources are locked
Use SSMS to rename the database, you shouldn't have any problem doing it that way.