#!/bin/bash
#
# This is an entrypoint that runs the MySQL server in the 'slave' mode.
#
export_vars=$(cgroup-limits); export $export_vars
source ${CONTAINER_SCRIPTS_PATH}/common.sh
set -eu
# Just run normal server if the data directory is already initialized
if [ -d "${MYSQL_DATADIR}/mysql" ]; then
exec /usr/bin/run-mysqld "$@"
fi
export MYSQL_RUNNING_AS_SLAVE=1
[ -f ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh ] && source ${CONTAINER_SCRIPTS_PATH}/validate_replication_variables.sh
# Generate the unique 'server-id' for this master
export MYSQL_SERVER_ID=$(server_id)
log_info "The 'slave' server-id is ${MYSQL_SERVER_ID}"
# Process the MySQL configuration files
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-base.cnf.template > /etc/my.cnf.d/base.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-paas.cnf.template > /etc/my.cnf.d/paas.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-slave.cnf.template > /etc/my.cnf.d/slave.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-repl-gtid.cnf.template > /etc/my.cnf.d/repl-gtid.cnf
envsubst < ${CONTAINER_SCRIPTS_PATH}/my-tuning.cnf.template > /etc/my.cnf.d/tuning.cnf
# Initialize MySQL database and wait for the MySQL master to accept
# connections.
initialize_database "$@"
wait_for_mysql_master
# Get binlog file and position from master
STATUS_INFO=$(mysql --host "$MYSQL_MASTER_SERVICE_NAME" "-u${MYSQL_MASTER_USER}" "-p${MYSQL_MASTER_PASSWORD}" replication -e 'SELECT gtid from replication limit 1\G')
GTID_VALUE=$(echo "$STATUS_INFO" | grep 'gtid:' | head -n 1 | sed -e 's/^\s*gtid: //')
# checking STATUS_INFO here because empty GTID_VALUE is valid value
if [ -z "${STATUS_INFO}" ] ; then
echo "Could not read GTID value from master"
exit 1
fi
mysql $mysql_flags <<EOSQL
STOP SLAVE;
SET GLOBAL gtid_slave_pos = "${GTID_VALUE}";
CHANGE MASTER TO MASTER_HOST='${MYSQL_MASTER_SERVICE_NAME}',MASTER_USER='${MYSQL_MASTER_USER}', MASTER_PASSWORD='${MYSQL_MASTER_PASSWORD}', MASTER_USE_GTID=slave_pos;
START SLAVE;
EOSQL
log_info 'Sourcing post-init.sh ...'
[ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ] && source ${CONTAINER_SCRIPTS_PATH}/post-init.sh
# Restart the MySQL server with public IP bindings
shutdown_local_mysql
unset_env_vars
log_volume_info $MYSQL_DATADIR
log_info 'Running final exec -- Only MySQL server logs after this point'
exec ${MYSQL_PREFIX}/libexec/mysqld --defaults-file=$MYSQL_DEFAULTS_FILE \
--report-host=$(hostname -i) "$@" 2>&1