Professional Documents
Culture Documents
/bin/bash
### Script to setup Master-Master replication or Master-Slave replication by Krishan Kumar
### ***** Start Pre-Req Section *****
### Always run this script on the node which you want to be slave for your master, if you do this
then script will take care of everything automatically for you.
###
### In my case user for replication is repl, you can find and replace it in this script as per your
username.
### For mysqldump, creating backup_usr in case you take backup using mysqldump or if you already
have user than find and replace it with your username in this script
### File $SDIR/backup.conf used to store the password for the user which has all privs, in my case
this user name is admin
### Make sure backup.conf exist on both the nodes i.e. Master and Slave
### Format of backup.conf file is
### mysql_admin_user_passwd=userpassword
### When copy this script from pdf file then SOME LINES WILL SPLIT TO MULTIPLE LINES
### SO ALL THOSE LINES MUST BE IN SINGLE LINE no MySQL OR echo command going accros mulitple lines.
### ***** End Pre-Req Section *****
###To alter repl user passwd in case it already exists to make sure it has autogenerated password
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$MHNAME
-e "select user,host from mysql.user where user like 'repl%' order by 1,2;" > /tmp/user.txt
$GREP repl /tmp/user.txt
if [ $? -eq 0 ]; then
/usr/bin/mysql -N -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
h$MHNAME -e "select concat('ALTER USER ''',user,'''@''',host,'''', ' IDENTIFIED BY
''','rep#${HNAME:4:3}#m1n''',';') from mysql.user where user like 'repl%';" > /tmp/user.sql
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
h$MHNAME -v < /tmp/user.sql
###Remove /tmp/user.sql
rm /tmp/user.sql
fi
echo -e "\nCheck if replication and backup_usr users exists or not. If not then create it on MASTER
node: $MHNAME"
echo -e "\nUse following to create replication user:-\ncreate user 'repl'@'${HOSTIP%.*}.%'
IDENTIFIED BY 'rep#${HNAME:4:3}#m1n';GRANT SELECT,REPLICATION SLAVE on *.* TO
'repl'@'${HOSTIP%.*}.%';"
echo -e "\nValue of server_id variable on node $HNAME, ***Make sure this is correct and unique value
per number of slave you have***\n"
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$HNAME
-e "show variables like 'server_id';"
echo -e "\nMake sure admin user password is same in backup.conf file on both hosts $MHNAME and
$HNAME\n"
read -p "If all looks good then press Enter otherwise CTRL C..." resp
if [ -s $SDIR/error.log ]; then
echo -e "\nMaster node Backup failed for the replication setup...\n"
exit 1
else
echo -e "Master node backup SUCCESSFUL on `hostname` moving on to setup the replication"
echo -e "Master node backup SUCCESSFUL on `hostname` moving on to setup the replication"|mailx -s
"Master node backup SUCCESSFUL on `hostname`" $VMAIL
fi
if [ $? -ne 0 ]; then
echo -e "\nChange master command or start slave failed, please check manually."
echo -e "Not executing setting up replication from $HNAME to $MHNAME\n"
exit 1
fi
echo -e "\n*******Master-Master Replication setup script is completed. Please check the slave
status on both nodes $MHNAME and $HNAME*******\n"
else
echo -e "\n*******Master-Slave Replication setup script is completed. Please check the slave status
on node $HNAME*******\n"
fi