Microsoft MS SQL server supports Linux operating systems, including Red Hat Enterprise Linux, Ubuntu, and container images on Virtual machine platforms like Kubernetes, Docker engine, and OpenShift. Regardless of the platform on which you are using SQL Server, the databases are prone to corruption and inconsistencies. If your MDF/NDF files on a Linux system get corrupted for any reason, you can repair them. In this post, we’ll discuss the procedure to repair and restore a corrupt SQL database on a Linux system.
Causes of corruption in MDF/NDF files in Linux:
The SQL database files stored in Linux system can get corrupted due to one of the following reasons:
- Sudden system shutdown.
- Bugs in the Server
- The system’s hard drive, where the database files are saved, has bad sectors.
- The operating system suddenly crashes at the time you are working on the database.
- Hardware or malware infection.
- The system runs out of space.
Ways to repair and restore corrupt SQL databases in Linux
To repair the corrupt SQL database file stored on Linux system, you can use SQL Server management studio on Ubuntu or Red hat enterprise itself Or use a professional SQL repair tool.
Steps to repair a corrupt SQL database on a Linux system:
- First, launch the SQL Server on your Linux system by the below steps:
- Open the terminal by Ctrl+Alt+T or ALT +F2
- Next, run the command below with the application name and then press the Enter key.
sudo systemctl start mssql-server
- In SSMS, follow the below steps to restore and repair the database file on Linux system:
Step 1- If you have an updated Backup file, you can use it to restore the corrupt Database. Here’s the command:
BACKUP DATABASE [AdventureWorks2019] TO DISK = N’C:\backups\DBTesting.bak’ WITH DIFFERENTIAL, NOFORMAT, NOINIT, NAME = N’AdventureWorks2019-Full Database Backup’, SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
Step 2- If you have no backup, then, with Admin rights, run the DBCC CHECKDB command on SQL Server Management Studio (SSMS). Here the corrupted database name is “DBTesting”. Before using the command, first change the status to SET SINGLE_USER. Here is the command:
ALTER DATABASE DBTesting SET SINGLE_USER
DBCC CHECKDB (‘DBTesting’, REPAIR_REBUILD)
GO
If REPAIR_REBUILD tool fails to repair the problematic MDF file then you can try the below REPAIR_ALLOW_DATA_LOSS command of DBCC CHECKDB command:
DBCC CHECKDB (N ’Dbtesting’, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS;
GO
Next, change the mode of the database from SINGLE_USER to MULTI_USER by executing the below command:
ALTER DATABASE DBTesting SET MULTI_USER
Using the above command can help you repair corrupt MDF file but it may removes majority of the data pages containing inconsistent data while repairing. Due to which, you can lose your data.
Step 3-Use a Professional SQL Repair tool:
If you don’t want to risk data in database then install a professional MS SQL recovery tool such as Stellar Repair for MSSQL. The tool is equipped with enhanced algorithms that can help you repair corrupt or inconsistent MDF/NDF file even in Linux system. Here are the steps to install and launch Stellar Repair for MS SQL:
- First open Terminal on Linux/Ubuntu
- Next, run the below command:
$ sudo apt install app_name
Here: Add the absolute path of the Stellar Repair for MSSQL tool.
- Next, launch the application in your Ubuntu using the below steps:
- On your desktop, find, and click
- In the Activities overview window, locate the Stellar Repair for MS SQL application and press the Enter key.
- Enter the system password to authenticate.
- Next, select the database in Stellar Repair for MS SQL’s user interface by clicking on Select Database.
After selecting an MDF file, click Repair. For detailed steps you can read the KB
To Conclude
If you are working on an SQL Server installed on a Linux system on the Virtual machine, your system suddenly crashes and the MDF file gets corrupted. In this case or any other scenarios where the SQL database file become inaccessible on Linux system, you can repair it using the two methods described above. To repair corrupt MDF files quickly, without data loss and file size restrictions, you can use the help of a professional MS SQL Repair tool. The tool supports repairing MDF files in both Windows and Linux systems.