Thursday, November 8, 2018

Configure a Linux Service to Start Automatically After a Crash or Reboot

Configure a Linux Service to Start Automatically After a Crash or Reboot

Below articles defines how to start a service automatically after restart or server or when service is stopped by some other service or accidental user kill.

Note: this will work only with systemd

If service are enable under systemctl, service will start automatically after system reboot. However it does not restart when service was killed accidentally or by any other process.

To check if the service enable for auto restart run below command ( in this case I am running for mongodb)

systemctl status mongod.serivce

output should contain "mongod.service; enabled;" if yes you are good at server restart.
If not run below command to enable service:
systemctl enable mongod.serivce

Below are the steps to enable service to start automatically after unexpected service down:

1. open below file using text editor for mongodb for other serivces use different service name instead of mongod


/etc/systemd/system/multi-user.target.wants/mongod.service


2. Add "Restart=always" under [Service] section of the file, save and close the file.
3. Reload the systemctl demon


sudo systemctl daemon-reload


4. Restart the service in this case mongodb service


systemctl restart mongod.service

For testing we can kill the service, below are steps:
6. Get the pid with the help of below command for mongodb 


ps -ef | grep service


7. kill the process with the help process number which we have received from above command.


kill -9 process_number


8. wait for a second or two and run below command to check the status of serivce in this case mongodb.


systemctl status mongod.service


Note: All the information provided in this article is based on my experience usage or application  based on your own interest.

Thanks for the checking in yours Nag.

Tuesday, July 31, 2018

Start mongodb automatically after it gets crashed or down.


Start mongodb automatically after it gets crashed or down.
Recently we have started getting an issue where on one of our server we are running both Redis and Mongodb. When we have memory issue Redis is killing Mongod process on Linux box.

One of the best possible solutions until we separate mongodb and Redis server is we have to start Mongodb service automatically after it crash. Did lots of research on internet could not find direct and easy solution but most of them helped me to find right solution. Writing this article by hoping this might help someone.

Environment:
We are running on Mongodb on RHEL 7 which uses systemctl for handling all services and below details applies to same.

First we need to find the service file location which can be found as below:
Sudo systemctl status mongod.service

Check for “loaded” line to find the service location.

Example: Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)

Edit the file mongod.service using your favorite editor and add below two lines:
Restart=on-failure
RestartSec=5s
Restart on failure will help restart mognodb service automatically and restartsec parameter will allow services to wait for 5 seconds in this case. Completed reference for can be found on link:

https://www.freedesktop.org/software/systemd/man/systemd.service.html

Post this we need to reload units with the help of below command:
sudo systemctl daemon-reload

Once done restart your mongodb service with the help of below commands:
sudo systemctl stop mongod.service
sudo systemctl start mongod.service

For testing you can kill the mongodb service with the help of below commands:

ps -ef | grep mongod.service
Will get you PID number.

sudo kill -9 process_number
Will kill mongodb PID.
Wait for 5 seconds you will see mongodb back online again.

All the very best Thank you, Nag.

Thursday, July 19, 2018

How to automate multiple script execution using SQLCMD and bath file.


How to automate multiple script execution using SQLCMD and bath file.

Problem
As a DBA’s many times we receive requests to execute multiple scripts, during some release times we end up having more than 100 scrips as well. Executing each script one at a time is always time consuming and chances of missing a script or two is high.
Solution
We can automate this by few simple steps:
1.      Create folder and place all scripts in the folder.
2.      Open notepad and add below script to and saves Execute_ALL_SQL.bat or whatever you feel comfortable.
@echo off
ECHO %USERNAME% started the batch process at %TIME%  >output.txt
for %%f in (*.sql) do (
echo %%f >>output.txt
sqlcmd.exe  -S -E -d yourDB -i "%%f" >>output.txt
    )
Pause
ECHO %USERNAME% started the batch process at %TIME%  >output.txt” name will write name of the user who is executing script to output file.
Next line is For loop to loop through all .SQL files.
echo %%f >>output.txt” while write SQL file name to output file.
Definition of “sqlcmd.exe  -S ServerName -E -d youDB -i "%%f" >>output.txt” SQLCMD.exe will help us run SQL commands. In this case
·        -S is SQL Server name on which you want to execute script.
·        -E is for windows or trusted authentication or we can use -U username and -P password for SQL Authentication.
·        -i input SQL files.
Added Pause at the last line just to let you know script execution completed and read out for any errors. Press space button it will close the window.
3.      Double click on the BAT file boom you are done with executing all scripts in one short.
4.      You can share the content of output file which will the script you have executed, how many rows effected if any.
Note: make sure both “BAT” file and “SQL” files are in same folder.

Wednesday, April 11, 2018

Capturing and decrypting Deadlock information from SQL Server Part 1


Capturing and decoding Deadlock information from SQL Server Part 1


We will have two articles in this series.
1. Different ways to capture deadlock
2. Decoding deadlock data from captured deadlock data

I know this is very common issue which DBA might not be able to completely resolve but DBA can provide detailed information regarding Deadlock.

Let us see what are the different ways we can capture deadlock data from SQL Server.

From SQL Server 2012 onwards we can capture deadlock data directly from System Health Extended event session, for this we don’t need enable any trace flags.
Earlier to SQL Server 2012, we need to enable flags 1204 and 1222 to get detailed deadlock information.
You can check my blog to find how to enable trace flags.

Below is the script to capture deadlock, This code is robust enough to check for SQL Server version, to get data from System health Event session or from SQL Server error log. If it does not find any information it will give suggestions as well.

SET NOCOUNT ON
DECLARE @stDate AS DATETIME = '2016-06-13 04:49:26'                                --Please key in the UTC time at which alert created
DECLARE @DecelockText AS VARCHAR (100) = 'Process ID 113'     --Replace 113 with the process ID part of deadlock

DECLARE @endDate AS DATETIME
SELECT @stDate = DATEADD(MILLISECOND,DATEDIFF(MILLISECOND,getutcdate(),GETDATE()),@stDate)
SET @endDate = DATEADD(MINUTE, 1, @stDate)
SET @stDate  = DATEADD(MINUTE, -2, @stDate)


IF (CAST(LEFT(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max)),CHARINDEX('.',CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max))) - 1) + '.' + REPLACE(RIGHT(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max)), LEN(CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max))) - CHARINDEX('.',CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(max)))),'.','') AS numeric(18,10)) ) >= 11
BEGIN
                DECLARE @PATH VARCHAR(500)
                SELECT  @PATH= [path] FROM sys.dm_os_server_diagnostics_log_configurations WITH (NOLOCK) OPTION (RECOMPILE);

                SET @PATH = @PATH + 'system_health*.xel'

                CREATE TABLE #Deadloc_Trace (DLGraph XML, Execution_time datetime)

                INSERT INTO #Deadloc_Trace

                SELECT CONVERT(xml, event_data).query('/event/data/value/child::*'),
                                   CONVERT(xml, event_data).value('(event[@name="xml_deadlock_report"]/@timestamp)[1]','datetime') as Execution_Time
                FROM sys.fn_xe_file_target_read_file(@PATH, null, null, null)

                WHERE object_name like 'xml_deadlock_report'


                SELECT * FROM  #Deadloc_Trace WHERE Execution_Time BETWEEN @stDate and @endDate

                DROP TABLE #Deadloc_Trace
END
ELSE
                BEGIN
                CREATE TABLE #TraceStatus(
                TraceFlag varchar(100)
                , [Status] int
                , [Global] int
                ,  [Session] int)

                insert #TraceStatus(TraceFlag, [Status], [Global], [Session])
                EXEC('DBCC TRACESTATUS(1204,1222) WITH NO_INFOMSGS;')
               
                IF EXISTS (select * from #TraceStatus where Global > 0)
                BEGIN
                                CREATE TABLE #ErrLog(LogDate datetime, ProcessInfo sysname, LogText nvarchar(1000));

                                INSERT INTO #ErrLog
                                EXEC xp_readerrorlog 0, 1, @DecelockText;

                                SELECT TOP 1 @stDate = LogDate
                                FROM #ErrLog
                                ORDER BY LogDate DESC;

                                SET @endDate = DATEADD(minute,2,@stDate)

                                --select @stDate, @endDate

                                EXEC master.dbo.xp_readerrorlog 0, 1, null, null,@stDate, @endDate

                                DROP TABLE #ErrLog;
                END
                ELSE
                BEGIN
                                PRINT 'Currently deadlock traces are not enabled and hence we cannot gather more information.
                We need to enable Deadlock traces 1204,1222 to capture deadlock information in future.
                '
                END

                DROP TABLE #TraceStatus;
END


Hope this helps someone thanks for checking in.


Monday, April 9, 2018

Start SQL Server Management Studio (SSMS) Or connect to SQL Server using NT Authority\ System


Start SQL Server Management Studio (SSMS) Or connect to SQL Server using NT Authority\ System


Recently we have faced a problem where sysadmin access for all of our accounts were removed accidently and SA was in disabled state.

Later we have noticed that one of the possible solution to this case is to connect SQL Server using NT Authority\System as this is default user till SQL Server 2008 R2 who will have sysadmin access on DB instance. From SQL Server 2012 onwards NT Authority\System user will not have sysadmin access and this article might not help. Below are quick steps to connect SSMS using NT Authority\System:

1. Download PS tools from below link:
2. Extract them to a folder.
3. Right click on CMD and run as administrator.
4. Navigate to folder where you have extracted zip file downloaded part of above link.
5. Run below command to start SSMS using NT Authority\System user

psexec -i -s SSMS

Done you should be able to access DB instance and provide access to the users you need. Hope this helps someone as it did to me.

Sunday, April 8, 2018

SQL Server patching and Fixing Missing MSI and MSP files issue


SQL Server patching and Fixing Missing MSI and MSP files issue


I know by looking at this article name many of you already might think that is well known issue and addressed on below MS Link.
https://support.microsoft.com/en-us/help/969052

However in this article I will describe an interesting new way I came across in recent past. Which I found be better and easy way  as this uses a GUI tool.

Recently one of my friend found this interesting tool and shared details with me. I thought why to stop with me and here is this article.

In this article we will see how easily FixSQLMSI.exe can fix Missing MSI and MSP files issues. This tool is engineer name “Simon Su” and available on below GitHub link.



Please find how to use steps below:
1. Download the package from above link.
2. Execute the exe.
3. Select the SQL Server Install path and click scan.
4. It will check for both Missing MSI, MSP files and any miss matched files (missed matched files would be shown if you have cached any wrong files in installer folder)
5. Once scanning is done check for Status and Action columns to find and fix any issues. You will find many more details like Source path, command to fix and so on.
6. If you have any missing files, click on Fix It button, if source path is intact it will fix automatically if not it will ask for source path, please provide the same to fix automatically.
7. Once you are done you may select log button to generate log and share details.
8. More detailed document is published by author as part of attached link you may check the same.

It is better to use this exe all the times before you patch or if you find any issues while patching any SQL Server instance.

Note: this is just a guide that might help you. Please take your own decision to use it or not.

Tuesday, March 27, 2018

Being honest to yourself is one of the biggest success mantra.

Being honest to yourself is one of the biggest success mantra.


This is all about my personal experience and I found being honest to myself is one of the biggest success mantra. Many times we plan to change our habits or we plan to achieve something and every time we comprise on them and we don’t achieve them by consoling ourselves that it is ok. This where we are not honest with us.

If we have to achieve success just follow 3 simple steps, after sometime you will wonder how things can be achieved.

1. Plan your carrier (clarity).
If you already have a carrier plan that is really great. If not, This won’t come by so easily, keep searching start looking around and start asking yourself what you want, you will find a moment where you will know what you want to be. Once you are ready don’t change this and be honest to yourself.
2. Plan your next 3, 6 months or an year based on what you want to achieve. I would say start with 3 months or even 1 month is also fine.

3. Chunk your goals to small parts so that some progress can be found every day. 
This is where actual test begins if you want succeed, you have to write down what you want to achieve every day either same day morning or previous day evening. At the end of the day make sure to revisit and check what you have completed and what you have not. Make sure to tick them right or wrong. I suggest use Red pen for wrong and blue for right.
You might fail to meet all your small goals everyday but eventually you will see that you will be able to finish everything on your daily activity list. This is possible only if you are honest to yourself and tick your goals right or wrong every day and include them in your next day activities. Don’t move to another activity as long as you don’t finish the one which you have not completed previous day.

That's it you might feel this is very simple, but practice them to see how difficult they are at the beginning and how great things can be achieved once you have built this habbit or created a pattern in your brain.

Don’t give up be honest is the mantra. 

Hope this is useful to someone. All the very best to everyone.

Configure a Linux Service to Start Automatically After a Crash or Reboot

Configure a Linux Service to Start Automatically After a Crash or Reboot Below articles defines how to start a service automatically after ...