|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Handles simple use case of enable/disable events and event handler when using |
| 4 | +# InnoDB Cluster. Only enable events on PRIMARY node. |
| 5 | +# |
| 6 | +# Works like: |
| 7 | +# if (primary) |
| 8 | +# if (event scheduler is disabled) -- means, we have had a failover/switchover |
| 9 | +# enable all events () |
| 10 | +# enable event scheduler () |
| 11 | +# else -- secondary |
| 12 | +# if (event scheduler enabled) |
| 13 | +# disable event scheduler () |
| 14 | +# |
| 15 | +# Run via crontab on MySQL nodes |
| 16 | +# |
| 17 | +# USE AT OWN RISK!! |
| 18 | +# |
| 19 | +# TODO: |
| 20 | +# - Login using login-path to avoid warnings: https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html |
| 21 | +# - Add lock via table/row in DB to avoid multiple instances running at the same time |
| 22 | +# Not sure how important this is, should be safe as it is .... |
| 23 | +# - Remove use of temporary file /tmp/events.tmp |
| 24 | +# |
| 25 | + |
| 26 | +MyHosts=("127.0.0.1:3310" "127.0.0.1:3320" "127.0.0.1:3330") |
| 27 | +dbUser="root" |
| 28 | +dbPwd="root" |
| 29 | +debug=0 |
| 30 | + |
| 31 | +for val in ${MyHosts[*]}; do |
| 32 | + host=`echo $val|cut -d: -f1` |
| 33 | + port=`echo $val|cut -d: -f2` |
| 34 | + if [ $debug -gt 0 ]; then echo "Server: $host $port"; fi |
| 35 | + server_uuid=`mysql -u$dbUser -p$dbPwd -h$host -P$port -se"SELECT @@server_UUID"` |
| 36 | + primary_uuid=`mysql -u$dbUser -p$dbPwd -h$host -P$port -se"SELECT MEMBER_ID FROM performance_schema.replication_group_members WHERE MEMBER_ROLE='PRIMARY'"` |
| 37 | + event_scheduler_status=`mysql -u$dbUser -p$dbPwd -h$host -P$port -se"SELECT VARIABLE_VALUE FROM performance_schema.global_variables WHERE VARIABLE_NAME='event_scheduler'"` |
| 38 | + |
| 39 | + if [ -z "$server_uuid" ] |
| 40 | + then |
| 41 | + echo "Did not find any server_uuid for ($host,$port), exiting..." |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + if [ $debug -gt 0 ]; then echo $server_uuid $primary_uuid $event_scheduler_status; fi |
| 45 | + |
| 46 | + if [ "$server_uuid" = "$primary_uuid" ] |
| 47 | + then |
| 48 | + if [ $debug -gt 0 ]; then echo "Primary"; fi |
| 49 | + if [ "$event_scheduler_status" = "OFF" ] |
| 50 | + then |
| 51 | + echo "PRIMARY($host:$port): Enable all the events and start event scheduler" |
| 52 | + mysql mysql -u$dbUser -p$dbPwd -h$host -P$port -se"SELECT CONCAT('ALTER EVENT ',EVENT_SCHEMA,'.',EVENT_NAME,' ENABLE;') FROM INFORMATION_SCHEMA.EVENTS WHERE STATUS != 'ENABLED'" > /tmp/events.tmp |
| 53 | + mysql mysql -u$dbUser -p$dbPwd -h$host -P$port < /tmp/events.tmp |
| 54 | + mysql mysql -u$dbUser -p$dbPwd -h$host -P$port -se"SET GLOBAL event_scheduler = ON" |
| 55 | + mysql mysql -u$dbUser -p$dbPwd -h$host -P$port -se"SET PERSIST event_scheduler = ON" |
| 56 | + fi |
| 57 | + else # slave/secondary |
| 58 | + if [ $debug -gt 0 ]; then echo "Secondary"; fi |
| 59 | + if [ "$event_scheduler_status" = "ON" ] |
| 60 | + then |
| 61 | + echo "SECONDARY($host:$port): Disable the event scheduler" |
| 62 | + mysql mysql -u$dbUser -p$dbPwd -h$host -P$port -se"SET GLOBAL event_scheduler = OFF" |
| 63 | + mysql mysql -u$dbUser -p$dbPwd -h$host -P$port -se"SET PERSIST event_scheduler = OFF" |
| 64 | + fi |
| 65 | + fi |
| 66 | +done |
| 67 | + |
0 commit comments