Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement service startup script in MySQL

2025-04-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to achieve service startup script in MySQL, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

When we build a MySQL environment, we usually follow the recommended standards and specifications, such as copying mysql.server to the self-boot directory.

Cp-rf $basedir/support-files/mysql.server / etc/init.d/mysql

Then set up the MySQL self-starting service, and after the configuration is complete, you can run the command service mysql.server start to start MySQL.

/ sbin/chkconfig-- add mysql

/ sbin/chkconfig-- level 2345 mysql on

Of course, this is an automatic operation, we can also do it manually. Let's take a look at what this amazing script is doing. The content of the script is long, so I will list part of it.

First of all, the name of this file has no direct impact, we can use mysql mysql.server and so on, register in this directory can be identified normally.

# service mysql status

SUCCESS! MySQL (Percona Server) running (15924) in the / etc/inid.d directory, the mysql named script file is actually not large, about 10K content, less than 400 lines of script. # ll mysql

-rwxr-xr-x 1 root root 11056 Aug 28 2013 mysql We take out the key parts to parse.

First of all, this script supports several options such as start,stop,restart,reload (or force-reload) and status.

Some of the core parts of start are:

# may be overwritten at next upgrade.

$bindir/mysqld_safe-datadir= "$datadir"-pid-file= "$mysqld_pid_file_path" $other_args > / dev/null 2 > & 1 &

Wait_for_pid created "$!"$mysqld_pid_file_path"; return_value=$?

In fact, this option is easy to understand, that is, mysqld_safe to start, you need to develop several startup parameters, some parameters are empty, but will be obtained from / etc/my.cnf, you can also support additional extension parameters.

Let's modify the script to print out these parameter values by hand.

Respectively $bindir $datadir $mysqld_pid_file_path $other_args

# service mysql start

Starting MySQL (Percona Server)

/ usr//bin

/ U01/mysql

/ U01/mysql/mysql.pid

. Success _ etc/my.cnf _ datadir will have a series of checks, but will also give priority to / check

# cat / etc/my.cnf | grep datadir

Datadir = / U01/mysql

In addition, basedir is also similar. You see, if the setting in my.cnf is not standard enough, it will also be / usr//bin in application.

# cat / etc/my.cnf | grep basedir

Basedir = / usr/

Next, there will be more checks under the mysqld_safe script.

Wait_for_pid created "$!"$mysqld_pid_file_path"; return_value=$? During startup, a lock file is generated under / var/lock/subsys, which is a tag for the process number.

# ll / var/lock/subsys/mysql

-rw-r--r-- 1 root root 0 May 9 23:03 / var/lock/subsys/mysqlwait_for_pid this function calls created (start mode) and removed (stop mode) to process the pid file.

The implementation of stop mode is relatively straightforward, which uses kill-0 to detect the existence of the process, and if so, uses the command of kill to kill the mysqld process.

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 $echo_n "Shutting down MySQL (Percona Server)"

Kill $mysqld_pid

# mysqld should remove the pid file when it exits, so wait for it.

Wait_for_pid removed "$mysqld_pid"$mysqld_pid_file_path"; return_value=$?

Else

Log_failure_msg "MySQL (Percona Server) server process # $mysqld_pid is not running!"

Rm "$mysqld_pid_file_path"

During the fi process, the background log is output step by step, and then the locked file is released.

The reload process uses relative moderation, using the kill-HUP option, which you can use if you want to change the configuration without stopping and restarting the service.

'reload' |' force-reload')

If test-s "$mysqld_pid_file_path"; then

Read mysqld_pid < "$mysqld_pid_file_path"

Kill-HUP $mysqld_pid & & log_success_msg "Reloading service MySQL (Percona Server)"

Touch "$mysqld_pid_file_path"

Else

Log_failure_msg "MySQL (Percona Server) PID file could not be found!"

Exit 1

Fi

The part of restart is to call the stop and start options indirectly.

'restart')

# Stop the service and regardless of whether it was

# running or not, start it again.

If $0 stop $other_args; then

$0 start $other_args

Else

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

Exit 1

The simpler part of fistatus is to read the process number information in the pid file.

Do not underestimate this script, which involves a lot of logical verification, and you can also make some changes according to your needs on this basis. At least at this point, this script can be customized to our needs.

Thank you for reading this article carefully. I hope the article "how to implement service startup script in MySQL" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report