Heartbeat start and stop the Script Modification of mysqld service

Source: Internet
Author: User
Tags aliyun
When ha switches, it will stop the local resources and then start the resources of the other party.

Our actual environment has two resources:

One is ipaddr, which is used to manage VIP;

MySQL is used to manage MySQL resources;

In the actual application process, we found that MySQL cannot be flush dirty pages or it takes a long time to start (recovery). This greatly prolongs the HA switching time. So we are wondering if HA can only manage the MySQL status, rather than MySQL start and stop? We know that when ha manages resources of the LSB type, it calls/etc/init. d/or. /resource. d/Add the start/stop/status parameter to the resource-related commands in the directory.

For example, there are MySQL resources,

Start MYSQL: Service MySQL start

Stop MYSQL: Service MySQL stop

MySQL: Service MySQL status for the purpose, we can modify the shell script:/etc/init. d/MySQL

==========================================

'Status ')

# First, check to see if PID file exists

If test-s "$ server_pid_file"; then

Read mysqld_pid <$ server_pid_file

If kill-0 $ mysqld_pid 2>/dev/NULL; then

Log_success_msg "MySQL running ($ mysqld_pid )"

Exit 0

Else

Log_failure_msg "MySQL is not running, but PID file exists"

Exit 1

Fi

Else

# Try to find appropriate mysqld Process

Mysqld_pid = 'pidof $ sbindir/mysqld'

If test-Z $ mysqld_pid; then

If test "$ use_mysqld_safe" = "0"; then

Lockfile =/var/lock/subsys/mysqlmanager

Else

Lockfile =/var/lock/subsys/MySQL

Fi

If test-F $ lockfile; then

Log_failure_msg "MySQL is not running, but lock exists"

Exit 2

Fi

Log_failure_msg "MySQL is not running"

Exit 3

Else

Log_failure_msg "MySQL is running but PID file cocould not be found"

Exit 4

Fi

Fi

;;

==============================================================>>>

This section is changed:

========================================================== ======================================

'Status ')

# First, check to see if PID file exists

If test-s "$ server_pid_file"; then

Read mysqld_pid <$ server_pid_file

If kill-0 $ mysqld_pid 2>/dev/NULL; then

# ----------------- Modi-1

./Root/. bash_profile

Mysqlpid =$ $

Stat_file =/home/MySQL. 'date + % Y % m % d % H % m % s'. $ mysqlpid

Mysql-uroot-h127.0.0.1-s-e "select \" aliyun. MySQL \ ";"> $ stat_file 2> & 1 &

CNT = 1

Mysqlstatus = unknown

While ["$ mysqlstatus" = "unknown"]

Do

If test-F $ stat_file

Then

Read content <$ stat_file

If ["$ content" = "aliyun. MySQL"]

Then

Mysqlstatus = OK

Log_success_msg "MySQL running ($ mysqld_pid )"

\ Rm-F $ stat_file

Exit 0

Elif ["$ content "! = ""]; Then

Error_cnt = 'grep-I "error" $ stat_file | grep-V "Using password" | WC-l'

If ["$ error_cnt"-ge "1"]; then

Log_failure_msg "MySQL status warning in logfile $ stat_file"

Fi

Exit 0

Fi

Fi

Sleep $ CNT

CNT = 'expr $ CNT + 2'

If [$ CNT-GT 5]; then

Mysqlstatus = Error

Log_failure_msg "MySQL running ($ mysqld_pid), but no respond"

Echo "error: MySQL running ($ mysqld_pid), but no respond">/var/log/messages

Exit 1

Fi

Done

#-----------------/Modi-1 else

Log_failure_msg "MySQL is not running, but PID file exists"

Exit 1

Fi

Else

# Try to find appropriate mysqld Process

Mysqld_pid = 'pidof $ sbindir/mysqld'

If test-Z $ mysqld_pid; then

If test "$ use_mysqld_safe" = "0"; then

Lockfile =/var/lock/subsys/mysqlmanager

Else

Lockfile =/var/lock/subsys/MySQL

Fi

If test-F $ lockfile; then

Log_failure_msg "MySQL is not running, but lock exists"

Exit 2

Fi

Log_failure_msg "MySQL is not running"

Exit 3

Else

Log_failure_msg "MySQL is running but PID file cocould not be found"

Exit 4

Fi

Fi

;;

========================================================== ================================

The above changes mean that when the service msyql status is run,

If the host hang is running, the MySQL process is still running, but MySQL cannot respond to the query. We also set the status to failed.

(If the temporary file MySQL is in 10 seconds. 'date + % Y % m % d % H % m % s '. $ mysqlpid exists, and the content must be "aliyun. mySQL ", then MySQL is normal .)

If the above condition is true, MySQL is in the unresponsive state, and the program will write information to the file:/home/MySQL/aliyun. mysqlheartbeat if MySQL fails to be checked. it will stop/start MySQL resources n times (N is based on CIB. xml configuration ),

During the restart process, the Stop/start status is directly returned to 0. Do not really close or start MySQL.

In this way, an error is returned directly when MySQL detects the status next time. Quick Switch. Therefore, in/etc/init. d/MySQL, we need to change the following:

0) change the original MySQL parameter start/stop to startup/stopdown.

'Start')-> 'startup ')

'Stop')-> 'stopup') 1) Add a new start segment

========================================================== ===

'Start ')

# Start daemon

Log_success_msg "MySQL start failed with/etc/init. d/MySQL In Ha mode !"

Exit 0

;;

========================================================== = 2) add stop segment

========================================================== ===

'Stop ')

# Stop daemon.

Log_success_msg "MySQL stop failed with/etc/init. d/MySQL In Ha mode !"

Exit 0

;;

========================================================== === Test:

Start MYSQL: Service MySQL startup

Disable MYSQL: Service MySQL stopdown

Status MYSQL: Service MySQL statusservice MySQL start/stop

If neither of the two statements does anything, send a message to you! Summary:

After this is done, no matter how heartbeat restarts/closes, it will only affect the VIP. MySQL is not directly operated.

On the one hand, it will not affect MySQL because heartbeat is not working properly;

On the other hand, heartbeat switching can be made faster (because it saves time to start and stop MySQL), but there is a small drawback in doing so:

MySQL is not automatically started when heartbeat is started.

After heartbeat is started, MySQL becomes invalid. Then a switchover occurs.

Therefore, you must ensure that at least one node's MySQL status is normal before heartbeat is started!
The above script has been successfully tested in Linux rhel5.3 and heartbeat 2.1.4 environments.

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.