|
| 1 | +#!/usr/bin/env bash |
| 2 | +args=$@ |
| 3 | +USER="$MYSQL_ROOT_USERNAME" |
| 4 | +PASSWORD="$MYSQL_ROOT_PASSWORD" |
| 5 | +localhost=127.0.0.1 |
| 6 | + |
| 7 | +function timestamp() { |
| 8 | + date +"%Y/%m/%d %T" |
| 9 | +} |
| 10 | + |
| 11 | +function log() { |
| 12 | + local type="$1" |
| 13 | + local msg="$2" |
| 14 | + echo "$(timestamp) [$script_name] [$type] $msg" |
| 15 | +} |
| 16 | + |
| 17 | +function retry { |
| 18 | + local retries="$1" |
| 19 | + shift |
| 20 | + |
| 21 | + local count=0 |
| 22 | + local wait=1 |
| 23 | + until "$@"; do |
| 24 | + exit="$?" |
| 25 | + if [ $count -lt $retries ]; then |
| 26 | + log "INFO" "Attempt $count/$retries. Command exited with exit_code: $exit. Retrying after $wait seconds..." |
| 27 | + sleep $wait |
| 28 | + else |
| 29 | + log "INFO" "Command failed in all $retries attempts with exit_code: $exit. Stopping trying any further...." |
| 30 | + return $exit |
| 31 | + fi |
| 32 | + count=$(($count + 1)) |
| 33 | + done |
| 34 | + return 0 |
| 35 | +} |
| 36 | +echo $BASE_NAME |
| 37 | +svr_id=$(($(echo -n "${HOSTNAME}" | sed -e "s/${BASE_NAME}-//g") + 11)) |
| 38 | +log "INFO" "server_id = $svr_id" |
| 39 | + |
| 40 | +mkdir -p /etc/mysql/semi_sync.conf.d/ |
| 41 | +echo "!includedir /etc/mysql/semi_sync.conf.d/" >>/etc/mysql/my.cnf |
| 42 | +cat >>/etc/mysql/semi_sync.conf.d/read.cnf <<EOL |
| 43 | +[mysqld] |
| 44 | +datadir=/var/lib/mysql/data |
| 45 | +disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY" |
| 46 | +# General replication settings |
| 47 | +gtid_mode = ON |
| 48 | +enforce_gtid_consistency = ON |
| 49 | +bind-address = "0.0.0.0" |
| 50 | +server_id = ${svr_id} |
| 51 | +EOL |
| 52 | + |
| 53 | +export pid |
| 54 | + |
| 55 | +function start_mysqld_in_background() { |
| 56 | + log "INFO" "Starting mysql server with 'docker-entrypoint.sh mysqld $args'..." |
| 57 | + docker-entrypoint.sh mysqld $args & |
| 58 | + pid=$! |
| 59 | + log "INFO" "The process id of mysqld is '$pid'" |
| 60 | +} |
| 61 | + |
| 62 | +function install_clone_plugin() { |
| 63 | + log "INFO" "Checking whether clone plugin on host $1 is installed or not...." |
| 64 | + local mysql="$mysql_header --host=$1" |
| 65 | + |
| 66 | + # At first, ensure that the command executes without any error. Then, run the command again and extract the output. |
| 67 | + retry 120 ${mysql} -N -e 'SHOW PLUGINS;' | grep clone |
| 68 | + out=$(${mysql} -N -e 'SHOW PLUGINS;' | grep clone) |
| 69 | + if [[ -z "$out" ]]; then |
| 70 | + log "INFO" "Clone plugin is not installed. Installing the plugin..." |
| 71 | + retry 120 ${mysql} -e "INSTALL PLUGIN clone SONAME 'mysql_clone.so';" |
| 72 | + reading_first_time=1 |
| 73 | + retry 120 ${mysql} -e "reset master;" |
| 74 | + log "INFO" "Clone plugin successfully installed" |
| 75 | + else |
| 76 | + log "INFO" "Already clone plugin is installed" |
| 77 | + fi |
| 78 | +} |
| 79 | + |
| 80 | +function install_semiSync_plugin() { |
| 81 | + log "INFO" "Checking whether semi_sync plugin on host $1 is installed or not...." |
| 82 | + local mysql="$mysql_header --host=$1" |
| 83 | + |
| 84 | + # At first, ensure that the command executes without any error. Then, run the command again and extract the output. |
| 85 | + retry 120 ${mysql} -N -e 'SHOW PLUGINS;' | grep semisync |
| 86 | + out=$(${mysql} -N -e 'SHOW PLUGINS;' | grep semisync) |
| 87 | + if [[ -z "$out" ]]; then |
| 88 | + log "INFO" "semisync plugin is not installed. Installing the plugin..." |
| 89 | + retry 120 ${mysql} -e "INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';" |
| 90 | + retry 120 ${mysql} -e "INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';" |
| 91 | + reading_first_time=1 |
| 92 | + log "INFO" "semi_sync plugin successfully installed" |
| 93 | + else |
| 94 | + log "INFO" "Already semi_sync plugin is installed" |
| 95 | + fi |
| 96 | +} |
| 97 | + |
| 98 | +# wait for mysql daemon be running (alive) |
| 99 | +function wait_for_mysqld_running() { |
| 100 | + local mysql="$mysql_header --host=$localhost" |
| 101 | + |
| 102 | + for i in {900..0}; do |
| 103 | + out=$(mysql -N -e "select 1;" 2>/dev/null) |
| 104 | + log "INFO" "Attempt $i: Pinging '$report_host' has returned: '$out'...................................." |
| 105 | + if [[ "$out" == "1" ]]; then |
| 106 | + break |
| 107 | + fi |
| 108 | + |
| 109 | + echo -n . |
| 110 | + sleep 1 |
| 111 | + done |
| 112 | + |
| 113 | + if [[ "$i" == "0" ]]; then |
| 114 | + echo "" |
| 115 | + log "ERROR" "Server ${report_host} failed to start in 900 seconds............." |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | + log "INFO" "mysql daemon is ready to use......." |
| 119 | +} |
| 120 | + |
| 121 | +# create mysql client with user exported in mysql_header and export password |
| 122 | +# this is to bypass the warning message for using password |
| 123 | + |
| 124 | +start_mysqld_in_background |
| 125 | + |
| 126 | +export mysql_header="mysql -u ${USER} --port=3306" |
| 127 | +export MYSQL_PWD=${PASSWORD} |
| 128 | + |
| 129 | +wait_for_mysqld_running |
| 130 | + |
| 131 | +install_clone_plugin "localhost" |
| 132 | + |
| 133 | +install_semiSync_plugin "localhost" |
| 134 | + |
| 135 | +while true; do |
| 136 | + kill -0 $pid |
| 137 | + exit="$?" |
| 138 | + if [[ "$exit" == "0" ]]; then |
| 139 | + echo "mysqld process is running" |
| 140 | + else |
| 141 | + echo "need to start mysqld and wait_for_mysqld_running" |
| 142 | + start_mysqld_in_background |
| 143 | + wait_for_mysqld_running |
| 144 | + fi |
| 145 | + log "INFO" "waiting for mysql process $pid." |
| 146 | + wait $pid |
| 147 | +done |
0 commit comments