mysql+keepalived Master-slave switch script to go

Source: Internet
Author: User

keepalived mysql Failure auto-switch script

The MySQL schema is master-slave (master-slave), master failure is automatically switched to slave. Of course, can also be set to double master, but here is a disadvantage: when the main pressure is very large, from the delay is very large, such as backward 2000 seconds, at this time the main hung, from the takeover (VIP drift to from), the user just published the article, at this time because of synchronization delay large, has not copied over, so the user has , when the original master repaired, since the IO and the SQL thread is still open, and will continue to synchronize the data has not been synchronized just now, it is possible to change the user's newly published article, resulting in user data loss.

In view of this situation, I still use the Master-slave (master-slave) architecture.

KeepAlive installation is very simple, no longer verbose here. Mainly look at the configuration files and scripts:

  1. # more/etc/keepalived/keepalived.conf

  2. Global_defs {

  3. router_id Keepalive_mysql

  4. }

  5. Vrrp_script Check_run {

  6. Script "/root/sh/mysql_check.sh"

  7. Interval 300

  8. }

  9. Vrrp_sync_group VG1 {

  10. Group {

  11. Vi_1

  12. }

  13. }

  14. Vrrp_instance Vi_1 {

  15. State BACKUP

  16. Interface eth0

  17. VIRTUAL_ROUTER_ID 51

  18. Priority 100

  19. Advert_int 1

  20. Nopreempt

  21. Authentication {

  22. Auth_type PASS

  23. Auth_pass 1111

  24. }

  25. Track_script {

  26. Check_run

  27. }

  28. notify_master/root/sh/master.sh

  29. notify_backup/root/sh/backup.sh

  30. notify_stop/root/sh/stop.sh

  31. virtual_ipaddress {

  32. 192.168.8.150

  33. }

  34. }

Notify_master <STRING>|<QUOTED-STRING>    # The script executed after the state changes to master Notify_backup <STRING>|< quoted-string>    # Status changed to backup script executed after Notify_fault <STRING>|<QUOTED-STRING>    # Scripts executed after the state changes to fault notify_stop <STRING>|<QUOTED-STRING>    # VRRP after a stop after execution of the script notify <STRING>|< Quoted-string>        # (1) scripts executed after any state change

The following explains the usage of these 4 scripts:

mysql_check.sh (Health Check script, when found MySQL connection is not on, will put the keepalive process off, and switch. )
  1. # more Mysql_check.sh

  2. #!/bin/bash

  3. . /root/.bash_profile

  4. count=1

  5. While True

  6. Do

  7. Mysql-e "Show status;" >/dev/null 2>&1

  8. I=$?

  9. PS aux | grep mysqld | grep-v grep >/dev/null 2>&1

  10. J=$?

  11. If [$i = 0] && [$J = 0]

  12. Then

  13. Exit 0

  14. Else

  15. If [$i = 1] && [$J = 0]

  16. Then

  17. Exit 0

  18. Else

  19. If [$count-GT 5]

  20. Then

  21. Break

  22. Fi

  23. Let count++

  24. Continue

  25. Fi

  26. Fi

  27. Done

  28. /etc/init.d/keepalived stop

master.sh (the script executed after the state changes to master) (first determine if synchronous replication is complete, if not, wait 1 minutes, and then skip and stop the synchronous copy process, whether or not it finishes.) (Second, change the permissions and passwords for the business account admin that the front-end program connects to, and record the logs and POS points that are currently switched.) )
  1. # more Master.sh

  2. #!/bin/bash

  3. . /root/.bash_profile

  4. master_log_file=$ (mysql-e "show slave status\g" | grep-w master_log_file | awk-f ":" ' {print $} ')

  5. relay_master_log_file=$ (mysql-e "show slave status\g" | grep-w relay_master_log_file | awk-f ":" ' {print $} ')

  6. read_master_log_pos=$ (mysql-e "show slave status\g" | grep-w read_master_log_pos | awk-f ":" ' {print $} ')

  7. exec_master_log_pos=$ (mysql-e "show slave status\g" | grep-w exec_master_log_pos | awk-f ":" ' {print $} ')

  8. i=1

  9. While True

  10. Do

  11. If [$master_log_file = $Relay _master_log_file] && [$Read _master_log_pos-eq $Exec _master_log_pos]

  12. Then

  13. echo "OK"

  14. Break

  15. Else

  16. Sleep 1

  17. If [$i-GT 60]

  18. Then

  19. Break

  20. Fi

  21. Continue

  22. Let i++

  23. Fi

  24. Done

  25. Mysql-e "Stop Slave;"

  26. MYSQL-E "flush logs; GRANT all privileges on * * to ' admin ' @ '% ' identified by ' admin '; flush privileges; "

  27. Mysql-e "Show master status;" >/tmp/master_status_$ (Date "+%y%m%d-%h%m"). txt

backup.sh (the script executed after the state changes to backup)
    1. # more Backup.sh

    2. #!/bin/bash

    3. . /root/.bash_profile

    4. MYSQL-E "GRANT all privileges on * * to ' admin ' @ ' percent ' identified by ' 1q2w3e4r '; flush privileges;"

stop.sh (script executed after keepalived stop) (first change the admin password) (second, set the parameters to ensure that no data is lost) (Finally, see if there is a write operation, regardless of whether or not the execution is complete, 1 minutes after the exit. )
  1. # more Stop.sh

  2. #!/bin/bash

  3. . /root/.bash_profile

  4. MYSQL-E "GRANT all privileges on * * to ' admin ' @ ' percent ' identified by ' 1q2w3e4r '; flush privileges;"

  5. MYSQL-E "Set global innodb_support_xa=1;"

  6. MYSQL-E "Set global sync_binlog=1;"

  7. MYSQL-E "Set global innodb_flush_log_at_trx_commit=1;"

  8. m_file1=$ (mysql-e "show Master status\g" | awk-f ': '/file/{print $} ')

  9. m_position1=$ (mysql-e "show Master status\g" | awk-f ': '/position/{print $} ')

  10. Sleep 1

  11. m_file2=$ (mysql-e "show Master status\g" | awk-f ': '/file/{print $} ')

  12. m_position2=$ (mysql-e "show Master status\g" | awk-f ': '/position/{print $} ')

  13. i=1

  14. While True

  15. Do

  16. If [$m_file1 = $M _file2] && [$M _position1-eq $M _position2]

  17. Then

  18. echo "OK"

  19. Break

  20. Else

  21. Sleep 1

  22. If [$i-GT 60]

  23. Then

  24. Break

  25. Fi

  26. Continue

  27. Let i++

  28. Fi

  29. Done

mysql+keepalived Master-slave switch script to go

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.