During the MySQL management process, some pcservers may occasionally crash or restart. In this case, I need to start the MySQL service after the host is started. Generally
During the MySQL management process, some PC servers may occasionally crash or restart. In this case, I need to start the MySQL service after the host is started. Generally
During the MySQL management process, some PC servers may occasionally crash or restart. In this case, I need to start the MySQL service after the host is started. In general, this work is simple. However, when there are hundreds or more MySQL hosts, there may be many "occasional" and "occasional" such "occasional" occurs in the middle of the night or early morning, it is cumbersome to perform manual operations each time. More importantly, it is not worthwhile to interrupt the early morning dream.
To enable automatic start of mysqld with startup, we need to solve the following problems:
1. Where is the Automatic startup script stored in Linux?
Generally, Linux, used as a server, is generally started at the "full Multi-User Mode with Networking" level, in this case, Linux runs/etc/rc at startup. all scripts under d/rc3.d. For example, we can see the script "/etc/rc. d/rc3.d/S90crond" in this directory, which means that the S90crond script will be run at startup.
2. How to run these scripts in Linux?
Now that you know where to put the Automatic startup script, everything is fine. We only need to release a script to start MySQL. The following is a simple STARTUP script v0.1 mysqldauto.
$ Vi mysqldauto
#! /Bin/sh
# Version: 0.1 by orczhou@gmail.com
/Opt/mysql/bin/mysqld_safe -- user = mysql & # here you need to modify it to your mysqld_safe directory
$ Chmod + x mysqldauto
$ Mv mysqldauto/etc/rc. d/init. d/
$ Ln-s/etc/rc. d/init. d/mysqldauto/etc/rc. d/rc3.d/S99mysqld
In this way, the created mysqldauto script is placed under/etc/rc. d/rc3.d/(note that the link method is used here), and mysqld can be automatically started.
There are two problems to be explained:
* *** Why not create a soft connection directly under the/etc/rc. d/rc3.d/directory? This is not necessary. But doing so has many advantages (which will be explained later), but at least it will look more professional.
* *** Why should I use S99mysqld for the file name? This is a rule. If the script under rc3.d starts with the letter S, Linux will pass a start parameter to it during execution (if it starts with a letter K, the stop parameter will be passed ); S is followed by a number, indicating the script startup sequence. If the directory rc3.d contains S98 *, it will run before s99. (Here, You can guess why we have created a soft connection)
Now, your mysqld can be automatically started with Linux boot.
3. How to be more professional?
The above steps can solve the problem, but there are some "shanzhai" in writing. Let's take a look at how to transform it.
* ***** Transformation 1: processing parameter start. Since the script that starts with the letter S will automatically pass a start parameter, K will pass the stop parameter. Then I will make the following changes:
$ Vi mysqldauto
#! /Bin/sh
# Version: 0.2 by orczhou@gmail.com
MYSQLHOME =/opt/mysql # here you need to modify the installation directory for your mysql
If [$ #-ge 1]; then
MysqldProc = 'ps-ef | grep-E "mysqld. + safe" | grep-v "grep" | wc-l'
If [$1 = "stop"]; then
If [$ mysqldProc-eq 1]; then
$ MYSQLHOME/bin/mysqladmin-uroot shutdown
Fi
Elif [$1 = "start"]; then
If [$ mysqldProc-eq 0]; then
$ MYSQLHOME/bin/mysqld_safe -- user = mysql &
Fi
Fi
Fi
After this transformation, our script needs to receive two parameters: start stop. If you want mysqld to automatically shut down the host, the stop parameter will take effect:
$ Ln-s/etc/rc. d/init. d/mysqldauto/etc/rc. d/rc0.d/K20mysqld
The benefits of soft connection are shown here. You only need to use one script to start and close the connection.
* ***** Transformation 2: When you face dozens or hundreds of hosts, the MySQL startup parameters may be different, for example, when the slave database is started, mysqld_safe-user = mysql-read_olny = 1 & may be required. What should I do in this case? Here is a solution.
Run a script on the host to detect the startup parameters of the current mysqld, and then write it to a specified file. Finally, read the startup parameters in this file in your startup script to start mysqld. It works.
,