rodesousa
6/17/2015 - 11:10 AM

Replication MySQL

Replication MySQL

#!/bin/bash

# PARAM
mysql_master=<%= @DBMY_MASTER_HOST %>
user_mysql=root
password_user_mysql=<%= @USER_ROOT_DBMY_SLAVE_PASSWORD %>
replication_user=<%= @REP_USER %>
replication_password=<%= @REP_PASSWORD %>

read -p "Etes vous sur de vouloir installer la replication ? [Y/N]" -n 1 -r

if [[ ! $REPLY =~ ^[YyOo]$ ]]
	then
		echo 'Abandon'
		exit 1
fi

master_status=$(mysql -h$mysql_master -u $replication_user -p$replication_password -e " SHOW MASTER STATUS; " | awk '(NR==2 || NR==3) {print $0}')

master_file=$(echo $master_status | cut --delimiter=' ' -f 1)
master_pos=$(echo $master_status | cut --delimiter=' ' -f 2)

mysql -u $user_mysql -p$password_user_mysql -e"STOP SLAVE;"

mysql -u $user_mysql -p$password_user_mysql -e"CHANGE MASTER TO MASTER_HOST='$mysql_master', MASTER_USER='$replication_user', MASTER_PASSWORD='$replication_password', MASTER_LOG_FILE='$master_file', MASTER_LOG_POS=$master_pos"

mysql -u $user_mysql -p$password_user_mysql -e"START SLAVE;"