From 5bc9410a2837acc52250ed0538b89c1fa8b9b802 Mon Sep 17 00:00:00 2001 From: Andrew Johnstone Date: Mon, 9 Feb 2015 10:50:30 +0000 Subject: [PATCH 1/2] read binlog position from master db --- 5.5/run.sh | 7 +++++++ 5.6/run.sh | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/5.5/run.sh b/5.5/run.sh index 424bd26..208ba91 100644 --- a/5.5/run.sh +++ b/5.5/run.sh @@ -3,6 +3,7 @@ VOLUME_HOME="/var/lib/mysql" CONF_FILE="/etc/mysql/conf.d/my.cnf" LOG="/var/log/mysql/error.log" +REPLICATION_SLAVE_OPTS=${REPLICATION_SLAVE_OPTS:-''}; # Set permission of config file chmod 644 ${CONF_FILE} @@ -134,6 +135,12 @@ if [ -n "${REPLICATION_SLAVE}" ]; then echo "=> Starting MySQL ..." StartMySQL echo "=> Setting master connection info on slave" + if [ -n "${REPLICATION_SLAVE_CURRENT}" ] ; then + mysql -h${MYSQL_PORT_3306_TCP_ADDR} -u${MYSQL_ENV_REPLICATION_USER} -p$MYSQL_ENV_REPLICATION_PASS} -e 'SHOW MASTER STATUS\G' > /tmp/mysql.master.info; + FILE=$(awk -F': ' '/File/ {print $2}' /tmp/mysql.master.info); + POSITION=$(awk -F': ' '/Position/ {print $2}' /tmp/mysql.master.info); + REPLICATION_SLAVE_OPTS="${REPLICATION_SLAVE_OPTS}, MASTER_LOG_FILE='${FILE}', MASTER_LOG_POS=${POSITION}" + fi mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT}, MASTER_CONNECT_RETRY=30" echo "=> Done!" mysqladmin -uroot shutdown diff --git a/5.6/run.sh b/5.6/run.sh index 424bd26..f23738d 100644 --- a/5.6/run.sh +++ b/5.6/run.sh @@ -3,6 +3,7 @@ VOLUME_HOME="/var/lib/mysql" CONF_FILE="/etc/mysql/conf.d/my.cnf" LOG="/var/log/mysql/error.log" +REPLICATION_SLAVE_OPTS=${REPLICATION_SLAVE_OPTS:-''}; # Set permission of config file chmod 644 ${CONF_FILE} @@ -134,7 +135,14 @@ if [ -n "${REPLICATION_SLAVE}" ]; then echo "=> Starting MySQL ..." StartMySQL echo "=> Setting master connection info on slave" - mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT}, MASTER_CONNECT_RETRY=30" + if [ -n "${REPLICATION_SLAVE_CURRENT}" ] ; then + mysql -h${MYSQL_PORT_3306_TCP_ADDR} -u${MYSQL_ENV_REPLICATION_USER} -p$MYSQL_ENV_REPLICATION_PASS} -e 'SHOW MASTER STATUS\G' > /tmp/mysql.master.info; + FILE=$(awk -F': ' '/File/ {print $2}' /tmp/mysql.master.info); + POSITION=$(awk -F': ' '/Position/ {print $2}' /tmp/mysql.master.info); + REPLICATION_SLAVE_OPTS="${REPLICATION_SLAVE_OPTS}, MASTER_LOG_FILE='${FILE}', MASTER_LOG_POS=${POSITION}" + echo "Replication binlog position set to '${FILE}' '${POSITION}'" + fi + mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT} ${REPLICATION_SLAVE_OPTS}, MASTER_CONNECT_RETRY=30" echo "=> Done!" mysqladmin -uroot shutdown touch /replication_configured From 73abec30a5ccf83bde922b59401e1008ede6d381 Mon Sep 17 00:00:00 2001 From: Andrew Johnstone Date: Mon, 9 Feb 2015 11:17:57 +0000 Subject: [PATCH 2/2] Handle failed connections and correct bash variable typo --- 5.5/run.sh | 13 ++++++++----- 5.6/run.sh | 12 +++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/5.5/run.sh b/5.5/run.sh index 208ba91..e72a7fc 100644 --- a/5.5/run.sh +++ b/5.5/run.sh @@ -136,12 +136,15 @@ if [ -n "${REPLICATION_SLAVE}" ]; then StartMySQL echo "=> Setting master connection info on slave" if [ -n "${REPLICATION_SLAVE_CURRENT}" ] ; then - mysql -h${MYSQL_PORT_3306_TCP_ADDR} -u${MYSQL_ENV_REPLICATION_USER} -p$MYSQL_ENV_REPLICATION_PASS} -e 'SHOW MASTER STATUS\G' > /tmp/mysql.master.info; - FILE=$(awk -F': ' '/File/ {print $2}' /tmp/mysql.master.info); - POSITION=$(awk -F': ' '/Position/ {print $2}' /tmp/mysql.master.info); - REPLICATION_SLAVE_OPTS="${REPLICATION_SLAVE_OPTS}, MASTER_LOG_FILE='${FILE}', MASTER_LOG_POS=${POSITION}" + mysql -h"${MYSQL_PORT_3306_TCP_ADDR}" -u"${MYSQL_ENV_REPLICATION_USER}" -p"${MYSQL_ENV_REPLICATION_PASS}" -e 'SHOW MASTER STATUS\G' > /tmp/mysql.master.info && { + cat /tmp/mysql.master.info + FILE=$(awk -F': ' '/File/ {print $2}' /tmp/mysql.master.info); + POSITION=$(awk -F': ' '/Position/ {print $2}' /tmp/mysql.master.info); + REPLICATION_SLAVE_OPTS="${REPLICATION_SLAVE_OPTS}, MASTER_LOG_FILE='${FILE}', MASTER_LOG_POS=${POSITION}" + echo "Replication binlog position set to '${FILE}' '${POSITION}'" + } || echo "FAILED to connect to master database @${MYSQL_PORT_3306_TCP_ADDR}"; fi - mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT}, MASTER_CONNECT_RETRY=30" + mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT} ${REPLICATION_SLAVE_OPTS}, MASTER_CONNECT_RETRY=30" echo "=> Done!" mysqladmin -uroot shutdown touch /replication_configured diff --git a/5.6/run.sh b/5.6/run.sh index f23738d..e72a7fc 100644 --- a/5.6/run.sh +++ b/5.6/run.sh @@ -136,11 +136,13 @@ if [ -n "${REPLICATION_SLAVE}" ]; then StartMySQL echo "=> Setting master connection info on slave" if [ -n "${REPLICATION_SLAVE_CURRENT}" ] ; then - mysql -h${MYSQL_PORT_3306_TCP_ADDR} -u${MYSQL_ENV_REPLICATION_USER} -p$MYSQL_ENV_REPLICATION_PASS} -e 'SHOW MASTER STATUS\G' > /tmp/mysql.master.info; - FILE=$(awk -F': ' '/File/ {print $2}' /tmp/mysql.master.info); - POSITION=$(awk -F': ' '/Position/ {print $2}' /tmp/mysql.master.info); - REPLICATION_SLAVE_OPTS="${REPLICATION_SLAVE_OPTS}, MASTER_LOG_FILE='${FILE}', MASTER_LOG_POS=${POSITION}" - echo "Replication binlog position set to '${FILE}' '${POSITION}'" + mysql -h"${MYSQL_PORT_3306_TCP_ADDR}" -u"${MYSQL_ENV_REPLICATION_USER}" -p"${MYSQL_ENV_REPLICATION_PASS}" -e 'SHOW MASTER STATUS\G' > /tmp/mysql.master.info && { + cat /tmp/mysql.master.info + FILE=$(awk -F': ' '/File/ {print $2}' /tmp/mysql.master.info); + POSITION=$(awk -F': ' '/Position/ {print $2}' /tmp/mysql.master.info); + REPLICATION_SLAVE_OPTS="${REPLICATION_SLAVE_OPTS}, MASTER_LOG_FILE='${FILE}', MASTER_LOG_POS=${POSITION}" + echo "Replication binlog position set to '${FILE}' '${POSITION}'" + } || echo "FAILED to connect to master database @${MYSQL_PORT_3306_TCP_ADDR}"; fi mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT} ${REPLICATION_SLAVE_OPTS}, MASTER_CONNECT_RETRY=30" echo "=> Done!"