-->
🏠 🔍
SHAREOLITE

Beginners Linux shell script to start MySQL process

Usually when you install MySQL software using RPMs - the server processes can  be started using linux services as below

service mysqld start

However in few instances where if you have installed MySQL using source packages like .tar or .bz files - you may have to use manual commands and start the required mysql process. Below mentioned is one such beginners linux script to start a MySQL server.

Copy the code below highlighted in blue , save it in a file and execute for output. There are 2 variables mysqld_file and mysqld_safe_file where the path of mysqld and mysqld_safe executables have to be configured based on your installation.

#################################################################################
# Configure MySQL commands mysqld and mysqld_safe path here.
# Ex. mysqld_file="/usr/sbin/mysqld"
#     mysqld_safe_file="/usr/bin/mysqld_safe"
####################################################################################
mysqld_file="/usr/sbin/mysqld"
mysqld_safe_file="/usr/bin/mysqld_safe"

###############################################################################################

echo -e "\n`date` : $0 is being executed.\n"

ls -ltrh $mysqld_file >> /dev/null;

if [ $? -eq 1 ]
then
echo -e "`date` : $mysqld_file not present.Please verify your MySQL installation"
exit
fi

pid=`ps -ef|grep "$mysqld_file" |grep -v 'grep' |awk '{print $2}'`
if [ -z $pid ]
then echo -e "`date` : MySQL is not running. Attempting to start now."
else
echo -e "`date`: MySQL is already running . Process id is :  $pid"
echo " "
exit;
fi




if [ `whoami` != 'root' ]
then
echo -e "`date`: You have to be a root user to start MySQL."
echo " "
exit
fi


ls -ltrh $mysqld_safe_file >> /dev/null;

if [ $? -eq 1 ]
then
echo -e "`date`: $mysqld_safe_file not present.Please verify your MySQL installation"
exit
fi

echo -e "`date` : Starting MySQL database process. Please wait"
$mysqld_safe_file &
if [ $? -eq 0 ]
then Status="Success"
else Status="Failure"
fi

sleep 2
echo -e "---------------------------------------------------------------------------"
echo -e "`date` : MySQL Startup is : $Status"
echo -e "---------------------------------------------------------------------------"
echo " "
###########################################################################

Hope this helps to Linux shell scripting beginners.
Comments

–>