SQL Database in Asynchronous AO relication - Log file not shrining after backup - sql-server

I have a database whose log file size is 527GB, showing almost 100% use. The DB is in AO Asynchronous replication with another SQL server. DB is in Full backup mode, and Log backup is happening every hour. I tried to shrink log file, it didn't work, gave me following message.
Msg 1468, Level 16, State 2, Line 2
The operation cannot be performed on database "MYDB" because it is involved in a database mirroring session or an availability group. Some operations are not allowed on a database that is participating in a database mirroring session or in an availability group.
Msg 5069, Level 16, State 1, Line 2
ALTER DATABASE statement failed.
The log for database 'MYDB' cannot be shrunk until all secondaries have moved past the point where the log was added..
(1 row(s) affected)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
I think its not doing it because of replication all logs are not transferred and its been all the time. How do solve the issue without taking Database out of AO replication group? I can move it to Synchronous mode, but I do not want it to move out of AO.

To check out the cause of your problem have a look at results of the query below (log_truncation_holdup_reason field):
select * from sys.dm_db_log_stats(db_id('YourDatabaseName'))
UPD: For versions lower than 2016 SP2 you can get the same using a query:
SELECT log_reuse_wait_desc
FROM sys.databases
WHERE name = 'YourDatabaseName'
If it is AVAILABILITY_REPLICA then check active transactions to understand which one could cause it.
dbcc opentran
Depending on results you will be able to decide what you should do.

Related

Can't Run DBCC CHECKDB on master DB - Azure Files

Storing SQL Server database files on new Azure Files share. Cannot run full / comprehensive CHECKDB against these databases - I think this has something to do with user account not having permissions to create snapshots. As a result, I offloaded these checks to an alternate server where I can also test .baks. Everything works fine except for the master db, which registers corruption when you restore it as a user db and run CHECKDB against it (https://www.itprotoday.com/my-master-database-really-corrupt), even though it's not corrupt.
Questions:
1) Has anyone run into the same problem running CHECKDB on SQL db files stored on an Azure Files share? Is there a workaround?
2) What's an alternative to running CHECKDB on master if I cannot run it in PROD? Can I somehow restore master to another SQL instance and check it there?
Error when I execute DBCC CHECKDB (master) in PROD:
Msg 5030, Level 16, State 12, Line 4
The database could not be exclusively locked to perform the operation.
Msg 7926, Level 16, State 1, Line 4
Check statement aborted. The database could not be checked as a database snapshot could not be created and the database or table could not be locked. See Books Online for details of when this behavior is expected and what workarounds exist. Also see previous errors for more details.
Message when I run DBCC CHECKDB on user db in PROD:
DBCC CHECKDB will not check SQL Server catalog or Service Broker consistency because a database snapshot could not be created or because WITH TABLOCK was specified.
Please reference this Azure Support document: Error message when you run any of the DBCC CHECK commands in SQL Server: "The database could not be exclusively locked to perform the operation"
In Microsoft SQL Server, you may receive an error message when you run any of the following DBCC commands:
DBCC CHECKDB
DBCC CHECKTABLE
DBCC CHECKALLOC
DBCC CHECKCATALOG
DBCC CHECKFILEGROUP
The error message contains the following text:
Msg 5030, Level 16, State 12, Line 1 The database could not be exclusively locked to perform the operation.
Msg 7926, Level 16, State 1, Line 1
Check statement aborted. The database could not be checked as a database snapshot could not be created and the database or table could not be locked. See Books Online for details of when this behavior is expected and what workarounds exist. Also see previous errors for more details.
Cause:
This problem occurs if the following conditions are true:
At least one other connection is using the database against which you
run the DBCC CHECK command.
The database contains at least one file group that is marked as
read-only.
Starting with SQL Server 2005, DBCC CHECK commands create and use an internal database snapshot for consistency purposes when the command performs any checks. If a read-only file group exists in the database, the internal database snapshot is not created. To continue to perform the checks, the DBCC CHECK command tries to acquire an EX database lock. If other users are connected to this database, this attempt to acquire an EX lock fails. Therefore, you receive an error message.
Resolution
To resolve this problem, follow these steps instead of running the DBCC CHECK command against the database:
Create a database snapshot of the database for which you want to
perform the checks. For more information about how to create a
database snapshot, see the "Create a Database Snapshot
(Transact-SQL)" topic in SQL Server Books Online.
Run the DBCC CHECK command against the database snapshot.
Drop the database snapshot after the DBCC CHECK command is
completed.
This document can give more helps to solve the problem.
Updates:
For the system databases it does not use database snapshots, but it will hold table locks.
You also an reference this blog: Checkdb giving error for master database:
Mike Walsh gives us more things about the error.
Hope this helps.

'The Transaction Log for database is full due to 'Active Transaction' - Unable to Backup, Enlarge, Truncate, or Shrink

I have a SQL Server database that keeps giving me the following error:
Msg 9002, Level 17, State 4, Line 30
The transaction log for database 'DSC_DW_Summary' is full due to 'ACTIVE_TRANSACTION'.
I can see that the log file is at 100% of "Log Space Used" despite my using Simple Recovery and 1 minute recovery time. When I try do any of the following (suggested online as potential solutions) I get the error listed above.
Increase the MaxSize of the log file
Backup the database
Shrink the database
I also don't see any running transactions when I run:
SELECT * FROM sys.dm_tran_database_transactions
or:
DBCC OPENTRAN
Which gives me the result:
No active open transactions.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
So I'm baffled. Somehow I'm being prevented from resolving the issue because of an "Active Transaction" but I can't find any active transactions.
Start with checking what is preventing your log reuse:
SELECT [name], [log_reuse_wait_desc]
FROM [sys].[databases];
Then also issue a CHECKPOINT to try and flush the log

Database Mirroring canot be removed error SQL

Ive a database which was mirrored before and now its showing as principal, disconnected/in recovery and we need to restore a back up into it.
Since restore option is disabled,for that purpose I tried to remove the mirroring from MS and whole properties are shows as greyed out disabled.
I tried
alter database mydb set partner off;
which returned
The operating system returned error 21(The device is not ready.) to
SQL Server during a read at offset 0000000000000000 in file 'ldf file
path'. Additional messages in the SQL Server error log and system
event log may provide more detail. This is a severe system-level error
condition that threatens database integrity and must be corrected
immediately. Complete a full database consistency check (DBCC
CHECKDB). This error can be caused by many factors; for more
information, see SQL Server Books Online.
So I tried
alter database mydb set offline;
which returned
Msg 1468, Level 16, State 1, Line 1
The operation cannot be performed on database "mydb" because it is involved in a database mirroring session or an availability group.
Some operations are not allowed on a database that is participating in
a database mirroring session or in an availability group.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
Then I tried to restore the DB from the backup file in recovery
RESTORE DATABASE Inetcat_mhra
FROM DISK = '.bak location'
WITH REPLACE,RECOVERY
which returned error
Msg 3104, Level 16, State 1, Line 1 RESTORE cannot operate on database
'mydb' because it is configured for database mirroring or has joined
an availability group. If you intend to restore the database, use
ALTER DATABASE to remove mirroring or to remove the database from its
availability group. Msg 3013, Level 16, State 1, Line 1 RESTORE
DATABASE is terminating abnormally.

SQL Server production server - all databases are in recovery pending state

All the databases in my SQL Server production server are in recovery pending state. I tried to execute different queries but they were of no use. Please help me as production work has been stopped at client side.
Tried to execute alter commands - but show error as following:
Msg 5120, Level 16, State 101, Line 1 Unable to open the physical file
"G:\Data\MSSQL\Database.mdf". Operating system error 3: "3(The system
cannot find the path specified.)". File activation failure. The
physical file name "G:\Data\MSSQL\Data\Database_log.ldf" may be
incorrect. Msg 945, Level 14, State 2, Line 1 Database 'Database'
cannot be opened due to inaccessible files or insufficient memory or
disk space. See the SQL Server errorlog for details
Msg 5069, Level 16, State 1, Line 1
Recovery pending means that for some reason SQL cannot run restart recovery on the database. Usually this is because the log is missing or corrupt.
Check to see if you can find the Database.mdf and Database_log.ldf files in the folder specified.
Check your system has not run out of disk space.
This could be caused by a hard drive failure. You may need to do a full restore of your last full back, any differentials and then restore the logs up until the log error occurred.
See similar issue here
My team encountered this error many times for my clients & I know, It is not easy to manage in the Production server. In your case Error 5120 –This error comes when the database is in Read Only Mode.
To fix this you can below code
USE [master]
GO
ALTER DATABASE [SQLAuthority] SET READ_WRITE WITH NO_WAIT
GO
After fixing 5120, you can process to fix "databases are in recovery pending state"
Recovery Pending – If the SQL Server knows that database recovery needs to be run but something is preventing it from starting, the Server marks the db in ‘Recovery Pending’ state. This is different from the SUSPECT state because it cannot be said that recovery is going to fail – it just hasn’t started yet.
Execute the following set of queries:
ALTER DATABASE [DBName] SET EMERGENCY; GO
ALTER DATABASE [DBName] set single_user GO
DBCC CHECKDB ([DBName], REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS; GO
ALTER DATABASE [DBName] set multi_user GO
Note: You can also read the Microsoft Warning on DBCC CHECKDB REPAIR ALLOW DATA LOSS.
It might be because of following possible causes:
Permissions
Find your SQL Server instance in the services list and double-click it, then select the Log On tab. It is this log on account that must have sufficient permissions to write to the temporary backup folder location. Check the permissions on the temporary backup folder by right-clicking it in Windows Explorer, selecting Properties, then navigating to the Security tab. Make sure that the account SQL Server is using has explicit read/write permissions for this folder.
Mapped Drives
Use a fully qualified UNC path instead of a mapped drive letter.
Lack Of Domain Trust
You can resolve this issue by ensuring that a trust between the two domains is established. You may also need to configure the SQL Server service account with pass-through authentication between the domains.
Please refer more here for recovery db
Execute these queries to fix SQL server database in recovery pending state:
ALTER DATABASE [DBName] SET EMERGENCY
GO
ALTER DATABASE [DBName] SET single_user
GO
DBCC CHECKDB ([DBName], REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS
GO
ALTER DATABASE [DBName] SET multi_user
GO
EMERGENCY mode marks the SQL Server database as READ_ONLY, deactivates logging, and gives the permission to system admin only. This method is capable of resolving any technical issue and bringing the database back to the accessible state. The database will automatically come out of the EMERGENCY mode.

How to catch an error message and send it by EMail?

Now when I take a backup I received error the problem not in error the problem in
How can I catch this body of the error message and send it by EMail or insert it into a table
I have stored for Backup
EXEC dbo.dothebackup2;
After execution I received this error:
Msg 3206, Level 16, State 1, Line 1
Backup device 'Professor_Tape' does not exist. To view existing backup devices, use the sys.backup_devices catalog view. To create a new backup device use either sp_addumpdevice or SQL Server Management Studio.
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.
Now how can I save this message in the table?
There are a couple of ways to do this task.
1 - Wrap the call to the backup stored procedure in a SQL Agent job. On failure of the job step, alert a operator, your DBA distribution list to react to the issue.
The Agent automatically keeps history by time or physical file size. See this MSDN article on how to adjust the rentention period.
Check out this MSDN article on how to notify an operator of a job status.
2 - Check out some of the more professional free backup solutions. I use the Ola Hallengren scripts for backups, index maintenance and consistency checks.
I hope this helps you.
Sincerely
John

Resources