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.

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 ...