Pages

Wednesday, 9 October 2019

How to Reduce the Risk of SQL Database Corruption


Introduction:

Today we are going to learn how to prevent SQL database corruption. Let’s start with some facts about how database corruption can occur actually. The SQL database corruption could be completely unsystematic. There are ways from which you find the reason for corruption, but sometimes you are on the wrong track and can’t find the primary reason behind the corruption of the database. And that’s why you have CHKDSK command to figure out the reason for complications. It is much better to reduce the risk of corruption so that you don’t have to face these issues. As a user of SQL Server, you should know that your Server is sharp-witted to predict the problem and already have the solution to that issue. While there are a unit ton of articles floating around the net on the way to fix corruption in SQL database, in this article, we are going to discuss the ways to stop SQL database corruption.


In data-driven businesses, the littlest of bug among the structure information will bring entire operations down. Whereas fixing issues is Associate in nursing after-thought, preventing issues is wiser. Here is a unit the highest six ways that you'll be able to implement to stop SQL server information corruption. There are a lot of ways to detect the corruption in SQL database and remove it from the SQL server. But today I am going to explain 5 ways to reduce the risk of SQL database corruption.

  1. You should Backup your database regularly
  2.  Split your database into more than one part.
  3. You should customize Cron jobs for your database
  4. You should optimize your database regularly
  5. You should check your database file size and hard disk size sometimes all database is lost due to this issue.
  6. You should do hardware and software maintenance regularly.
  7. Alternative Solution.

  • Backup your database regularly:

You’ll have detected it some of thousand-fold already; however, this is that the best safeguard against losses. Backups are an excellent facilitate once databases develop issues. In such cases, backup files square measure won’t restore the corrupted databases, thereby preventing period. You must build a backup of your information regularly so that the possibility of SQL information corruption is scaled back. If the database corrupted, then I will suggest the SQL database repair software. Here is show you how you can back up using MS SQL Server.


After pressing the button you will see the following window:


Press the “OK” button. In this way we backup our files.

  • Split your database:

Split your information. Instead of projecting each object in one MDF file and sharing the complete factor over the network, split the data into 2 pieces: the front-end forms, reports, queries, and knowledge access pages, and also the back-end tables containing your knowledge. Place a duplicate of your front-end information on the native disc of every machine and link back to the tables that are present on a fileserver. It implies that way, less knowledge has got to be dragged around the network and exposed to corruption. Your information will run additional quickly and can enable you to create style changes to the face, whereas still, victimization lives knowledge.

  • Customization of Cron Jobs for your database:

Start Cron jobs to run at regular intervals to confirm no lag in the period.To do that, select a database and press tasks and Back Up:



On the Backup window select Script and Script Action to Job:


Go to Schedules page and press the New button:


Add the schedule of your preference:


  • Optimize your database:

Create scripts that may monitor and determine slow running queries within the info. Often, slow Associates in nested queries end in information issues and an overall lag by taking on over necessary resources. Produce scripts that may determine and optimize these queries and stabilize the SQL server. At a regular basis, do not forget to sporadically monitor and tune-in the server for making certain high performance. Certify that the connections to the server square measure restricted and optimized. The most effective way to maintain the general health of the database is to stay updated with the most recent updates, thus don’t forget to install the last service packs.


Here I show you how you can optimize your database select the last database option. After pressing the button “Database” you will see a new window look like below.


In available space section you will see an option to optimize the size of the database.

  • Checking database file size and storage

When you start employing a new SQL server database, information file size won't be a problem.  Also, the possibilities of big databases obtaining corrupt area unit are tiny. Therefore, it's a decent plan to separate databases of enormous sizes and maintain multiple smaller versions. It additionally helps in liberating up adequate space on the server. As shortly as you see that free area is slippery below the perfect price, begin engaged in liberating it up.


  • Maintenance of hardware and software

Regularly perform memory checks, network observation, hardware-error checks, etc. to avoid bugs and hardware failures. This sort of preventive measure should be distributed sporadically to track the standing of all hardware proactively, and if the requirement arises, to repair all detected problems. Virus and malware will prove very dangerous to your SQL server info. Don't fall prey to such malicious attacks and perpetually keep an eye fixed out for dead of the info. Also, from time to time, check for any traces of malware infections inside the info and use a decent code resolution to stay them cornered.



Image Ref: Hardware Repairing

  • Alternative Solution
Stellar Repair for MS SQL Server is a software that helps us to reduce risk in our databases. This SQL Recovery Software repairs .mdf and .ndf files. It also recovers our deleted records and supports MS SQL Server 2017, 2016, 2014, 2012, 2008 and older versions. After rebuild and recovery of the databases, Stellar Repair for MS SQL Server allows us to save files in multiple file extensions like HTML, XLS, CSV formats and .mdf files. If you keep your recovered file as a .mdf file extension, it will save your data as a new or live database. We will solve our multiple problems using Stellar Repair for MS SQL Server.


Conclusion:

In this article, we studied how we can reduce the risks of MS SQL Database corruptions. We can make daily backups of our database. We will split, optimize, and customize, and schedule jobs in our database. We can also check our database size. We also studied an SQL recovery software. This software also helps us to solve multiple problems. 

Monday, 16 September 2019

An Effective Solution to Resolve SQL Database Corruption


I am a SQL Administrator and today I will share my experience about an issue on SQL server, which corrupted the transaction logs and left the database in a RECOVERY_PENDING state. Thankfully, I was able to resolve it.  

The SQL server experienced a faulty hardware issue and a faulty memory and it suffered a corruption in transaction log leading to unclean server shutdown. When the system was eventually online, it had a database which was not available in a usable state.

The transaction log is used by SQL at all times to ensure the consistency of the database. It provides SQL database the required consistency and durability which are necessary for any relational database engine. If the SQL service is stopped or the SQL database gets detached, it is the job of SQL server to shut down in a clean state, meaning it should run a checkpoint and write all dirty data pages on a disk. Further, the checkpoint also needs to make an entry into the logs that it has completed the task of running. If there is not enough space in the log, the checkpoint cannot run and SQL database cannot shut down cleanly.

When the database was attached and the service was restarted, SQL tried to restart-recovery on the database. Here the transaction log was used to bring the SQL database to a consistent transactional state. So far so good, but here the transaction logs were not available. And hence, the database could not be brought online, and based upon the failure stage, the database was marked in Suspect or Recovery Pending stage. 

The following sequence of activities were performed:


CREATE DATABASE AbcXyz
ON (NAME = ABC_dat,
FILENAME = ‘D:\Develop\Databases\ AbcXyz.mdf’,
SIZE = 10
MAXSIZE = UNLIMITED
FILEGROWTH = 5

LOG ON (NAME =  Abc_log,
FILENAME = ‘‘D:\Develop\Databases\ AbcXyz.ldf’,
SIZE = 5MB,
MAXSIZE = 100MB);
GO

ALTER DATABASE AbcXyz SET RECOVERY FULL;
GO

USE AbcXyz
GO

CREATE TABLE Filler (
ID INT IDENTITY PRIMARY KEY,
FillerStr CHAR(300)
)
GO

BACKUP DATABASE AbcXyz TO DISK = ‘D:\Develop\Databases\Backups\AbcXyz.bak’[/source]
Right, so that’s the setup all done. Now to fill the log up.
[source:sql]DECLARE @i INT;
SET @i = 0;
WHILE (@i < 10000)

BEGIN
INSERT INTO Filler (FillerStr) VALUES (CAST(@i AS VARCHAR(10)))
SET @i = @i+1;
END
GO

After just a 3779 inserts, an error message popped up:

Msg 9002, Level 17, State 2, Line 6
The transaction log for database ‘AbcXyz’ is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases

At this stage, stop the SQL service and delete/rename the log file (D:\Develop\Databases\ AbcXyz.ldf). Now restart the SQL service. When I tried to access the database with the following query,

select MAX(ID) from AbcXyz.dbo.Filler[/source]

SQL server throws below error message.

Msg 945, Level 14, State 2, Line 1
Database ‘AbcXyz’ cannot be opened due to inaccessible files or insufficient memory or disk space.  See the SQL Server error log for details.

With this error message, I came to the conclusion that the state of the database, as per sys.databases is in RECOVERY_PENDING state. ‘Restart Recovery’ cannot run due to the missing log file and the error log enumerates the following:

Error: 17207, Severity: 16, State: 1.

FileMgr::StartLogFiles: Operating system error 2(The system cannot find the file specified.) occurred while creating or opening file ‘D:\Develop\Databases\AbcXyz.ldf’. Diagnose and correct the operating system error, and retry the operation.
File activation failure. The physical file name “D:\Develop\Databases\AbcXyz.ldf” may be incorrect.
The log cannot be rebuilt because the database was not cleanly shut down.

Now, the first question for me was “What is the SLA for the database server and how much data can the user afford to lose?

And the answer was as expected: “the database needs to be up and running a.s.a.p., and we can’t afford to lose data”.

Methods applied to resolve MS SQL corruption

There are two ways to fix RECOVERY_PENDING issue – restore from backup, or repair and rebuild logs. Further, there are two methods for log repair and rebuild – the DBCC CHECKDB command using the REPAIR_ALLOW_DATA_LOSS option and the software-based method. 

First, I wanted to check the health of SQL database backup to restore the database from backup. This is the easiest and most feasible options, provided the backup is in a consistent state.

  • Restore MS SQL database from backup

The most feasible option to fix the database Transaction Log file corruption issue was to restore the database from the latest restorable backup and it had to be done from the ‘Full’ backup. The backup was available but it was not updated. This means, if we tried to restore from the available backup, we would be losing around last few hours data.

We opted for manual backup restoration process with the help of SQL Server Management Studio (available for free from Microsoft).
Note:
Process to restore MS SQL database from backup varies from version to version. Here our database was on SQL Server 2012.
We installed and connected the database to SQL Server Management Studio through Port 1433.

Database users could not afford such a huge data loss so the option of restoring MS SQL database through last available backup was completely ruled out unless there was no other option.

  • Repair database with DBCC


I needed to run a repair to bring the database in RECOVERY_PENDING state to an online state. The repair utility will fix the structural problems, as caused by the missing log and bring back the database to a normal online state. 

The following command was used:

ALTER DATABASE AbcXyz SET SINGLE_USER

DBCC CHECKDB(AbcXyz, REPAIR_ALLOW_DATA_LOSS)

After running this command, I found that the CheckDB complained about the missing log and started the process of log-rebuild with the following warnings:


File activation failure. The physical file name “D:\Develop\Databases\AbcXyz.ldf” may be incorrect.

The log cannot be rebuilt because the database was not cleanly shut down.
Transactional consistency has been lost. The RESTORE chain was broken, and the server no longer has context on the previous log files, so you will need to know what they were. You should run DBCC CHECKDB to validate physical consistency. The database has been put in dbo-only mode. When you are ready to make the database available for use, you will need to reset database options and delete any extra log files.


Again, running the repair command did not resolve the issue. As there was not enough space in the log for a clean shutdown, running a checkpoint also prompted an error message stating that the database could not be repaired and the log can’t be rebuilt because the database was not cleanly shutdown.

Now this was a catch-22 situation for me. I couldn’t restore the database because the backup was not full and I couldn’t repair the logs because transaction logs were missing. The recommended approach is to resolve the error with an MS SQL database repair software. But first, I need to search for such software which could repair the database and make it available for users within no time.

  • Repair with MS SQL Repair software

I did thorough online research for available software and decided to go with Stellar Repair for MS SQL. There were many reasons to support my selection-Along with the corrupted ndf and ldf file, the software also repaired corrupted log files. The software claimed to correct the bit-pattern and bring the database to a consistent state. Both these features were required to bring our MS SQL database online.

I also read a few case studies where the clients were happy in buying the software as they could easily repair the corrupted database and recovered the entire data, which they otherwise thought to be lost. Here’s the link.

Again, before investing in the software, I called up the customer support to know the procedure of database repair and what’s the probability of getting back the data. They informed me that I could download and test the software and the preview would show the recoverable database. That’s what I actually wanted.

I installed the software and submitted the file. To my surprise, I could preview the entire SQL database on my screen. Without wasting any more time, I activated the software. There also, the support team helped me in getting the activation key without any time lag. And, I could save the entire database as a new database. The new database was accessible and did not have any corrupt elements.

I breathed a sigh of relief!

I wished I had thought of this option before everything else, then the downtime would have been much lesser.

Conclusion

As MS SQL Administrators, it is our job to keep the database up and running. And if there is an issue, then we should resolve the issue at the earliest to keep the downtime to the minimum and recover the database in entirety. Hence, this is for us to decide which protocol, backup restore, repair with DBCC command or repair with third-party the software delivers the maximum value in the given circumstances. There is no better solution than restoring the MS SQL database from a good backup and the next best option is to repair the database by using software such as Stellar Repair for MS SQL.
This crisis situation taught me that the database should be backed up consistently and also a software should be available as a help to overcome such crisis. Here’s a small but informative guide for database administrators.