Get the App
SLTechnology News&Howtos  ›  Database  › 

How to develop MySQL startup scripts using shell at the enterprise level

Shulou Source: shulou.com Published: 2022-06-01 19:53:15 10月02日 Update

This article mainly introduces the enterprise-level use of shell is how to develop MySQL startup script, the contents of the article are carefully selected and edited by the author, with a certain pertinence, for everyone's reference significance is still relatively great, the following with the author to understand the enterprise-level use of shell is how to develop MySQL startup script bar.

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 finish writing the MySQL startup script

Requirements: implemented with functions, case statements, if statements, etc.

Answer:

[root@db02 scripts] # cat / etc/init.d/oldgirl

#! / bin/bash

# chkconfig: 2345 64 36

# description: MySQL startup

# Author:oldboy

# Blog: http://oldboy.blog.51cto.com

# Time:2017-07-07 09:24:34

# Name: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=

# Lock directory.

Lockdir='/var/lock/subsys'

Lock_file_path= "$lockdir/mysql"

Log_success_msg () {

Echo "SUCCESS! $@"

}

Log_failure_msg () {

Echo "ERROR! $@"

}

Case "$1" 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 be found!"

Fi

Restart)

If $0 stop; then

$0 start

Else

Log_failure_msg "Failed to stop running server, so refusing to try to start."

Exit 1

Fi

*)

Echo "Usage: $0 {start | stop | restart}"

Exit 1

Esac

Exit $return_value

Function version:

#! / bin/bash

# chkconfig: 2345 64 36

# description: MySQL startup

# Author:oldboy

# Blog: http://oldboy.blog.51cto.com

# Time:2017-07-07 09:24:34

# Name: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=

# Lock directory.

Lockdir='/var/lock/subsys'

Lock_file_path= "$lockdir/mysql"

Log_success_msg () {

Echo "SUCCESS! $@"

}

Log_failure_msg () {

Echo "ERROR! $@"

}

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 be found!"

Fi

}

Case "$1" in

Start)

Start

Stop)

Stop

Restart)

If $0 stop; then

$0 start

Else

Log_failure_msg "Failed to stop running server, so refusing to try to start."

Exit 1

Fi

*)

Echo "Usage: $0 {start | stop | restart}"

Exit 1

Esac

Exit $return_value

After reading the above about how the enterprise uses shell to develop MySQL startup scripts, many readers must have some understanding. If you need to get more industry knowledge and information, you can continue to follow our industry information column.

Tags: Script enterprise development function command author industry sentence information content meaning article more friends column knowledge reader information or pertinence Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Xiaomi Huawei Microsoft NVidia