Enterprise-level development of MySQL startup script cases through shell scripting

Source: Internet
Author: User

Enterprise Shell face question 10: Develop MySQL startup script
Description
The MySQL startup command is:
/bin/sh mysqld_safe--pid-file= $mysqld _pid_file_path 2>&1 >/dev/null &
The Stop command is:
Mysqld_pid= ' cat ' $mysqld _pid_file_path "'
if (kill-0 $mysqld _pid 2>/dev/null)
Then
Kill $mysqld _pid
Sleep 2
Fi
Please complete the MySQL startup script writing
Requirements: Implemented with functions, case statements, if statements, and so on.
Answer:



[Email protected] scripts]# Cat/etc/init.d/oldgirl
#!/bin/bash
# chkconfig:2345 64 36
# Description:mysql Startup
#Author: Oldboy
#Blog: 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/ Zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>http://oldboy.blog.51cto.com
#Time: 2017-07-07 09:24:34
#Name: 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/ Zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>mysqld.sh
#Version: V1.0
#Description: This is a test script.
[-f/etc/init.d/functions] && source/etc/init.d/functions
port=3306
User= "Root"
Bindir= "/application/mysql/bin"
Datadir= "/application/mysql/data"
Mysqld_pid_file_path= "/application/mysql/' hostname '. pid"
Path= "/sbin:/usr/sbin:/bin:/usr/bin: $basedir/bin"
Export PATH
Return_value=0


# Lock directory.
Lockdir= '/var/lock/subsys '
Lock_file_path= "$lockdir/mysql"

Log_success_msg () {
echo "success! [Email protected] "
}
Log_failure_msg () {
echo "error! [Email protected] "
}



Case "$" in
Start
# Start Daemon
echo "Starting MySQL"
If Test-x $Bindir/mysqld_safe
Then
$Bindir/mysqld_safe--datadir= "$Datadir"--pid-file= "$mysqld _pid_file_path" >/dev/null &
Return_value=$?
Sleep 2

# Make lock for CentOS
If Test-w "$lockdir"
Then
Touch "$lock _file_path"
Fi
Exit $return _value
Else
Log_failure_msg "couldn ' t find MySQL server ($bindir/mysqld_safe)"
Fi
;;
Stop
If Test-s "$mysqld _pid_file_path"
Then
Mysqld_pid= ' cat ' $mysqld _pid_file_path "'

if (kill-0 $mysqld _pid 2>/dev/null)
Then
echo "Shutting down MySQL"
Kill $mysqld _pid
Return_value=$?
Sleep 2
Else
Log_failure_msg "MySQL Server process # $mysqld _pid is not running!"
Rm-f "$mysqld _pid_file_path"
Fi
# Delete Lock for CentOS
If Test-f "$lock _file_path"
Then
Rm-f "$lock _file_path"
Fi
Exit $return _value
Else
Log_failure_msg "MySQL Server PID file could not being found!"
Fi
;;
Restart
if $ stop; Then
$ start
Else
Log_failure_msg "Failed to stop running server, so refusing-to-try to start."
Exit 1
Fi
;;

*)
echo "Usage: $ {Start|stop|restart}"
Exit 1
;;
Esac
Exit $return _value














Function Version:

#!/bin/bash
# chkconfig:2345 64 36
# Description:mysql Startup
#Author: Oldboy
#Blog: 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/ Zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>http://oldboy.blog.51cto.com
#Time: 2017-07-07 09:24:34
#Name: 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/ Zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>mysqld.sh
#Version: V1.0
#Description: This is a test script.
[-f/etc/init.d/functions] && source/etc/init.d/functions
port=3306
User= "Root"
Bindir= "/application/mysql/bin"
Datadir= "/application/mysql/data"
Mysqld_pid_file_path= "/application/mysql/' hostname '. pid"
Path= "/sbin:/usr/sbin:/bin:/usr/bin: $basedir/bin"
Export PATH
Return_value=0


# Lock directory.
Lockdir= '/var/lock/subsys '
Lock_file_path= "$lockdir/mysql"

Log_success_msg () {
echo "success! [Email protected] "
}
Log_failure_msg () {
echo "error! [Email protected] "
}

Start () {
# Start Daemon
echo "Starting MySQL"
If Test-x $bindir/mysqld_safe
Then
$bindir/mysqld_safe--datadir= "$datadir"--pid-file= "$mysqld _pid_file_path" >/dev/null &
Return_value=$?
Sleep 2

# Make lock for CentOS
If Test-w "$lockdir"
Then
Touch "$lock _file_path"
Fi
Exit $return _value
Else
Log_failure_msg "couldn ' t find MySQL server ($bindir/mysqld_safe)"
Fi
}

Stop () {
If Test-s "$mysqld _pid_file_path"
Then
Mysqld_pid= ' cat ' $mysqld _pid_file_path "'

if (kill-0 $mysqld _pid 2>/dev/null)
Then
echo "Shutting down MySQL"
Kill $mysqld _pid
Return_value=$?
Sleep 2
Else
Log_failure_msg "MySQL Server process # $mysqld _pid is not running!"
Rm-f "$mysqld _pid_file_path"
Fi
# Delete Lock for CentOS
If Test-f "$lock _file_path"
Then
Rm-f "$lock _file_path"
Fi
Exit $return _value
Else
Log_failure_msg "MySQL Server PID file could not being found!"
Fi
}
Case "$" in
Start
Start
;;
Stop
Stop
;;
Restart
if $ stop; Then
$ start
Else
Log_failure_msg "Failed to stop running server, so refusing-to-try to start."
Exit 1
Fi
;;

*)
echo "Usage: $ {Start|stop|restart}"
Exit 1
;;
Esac
Exit $return _value





Enterprise-level development of MySQL startup script cases through shell scripting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.