Internet-Goods.com

MySQL Slave Creation script

by nate on Oct.20, 2009, under Open Source Linux

#!/bin/sh
#this script is meant to be run from a slave node, it will create a slave relationship to a master node
#the master node is defined as MASTER_HANDLE using the shmysql scripts to execute sql
#you will need shmysql installed for any of this to work
MASTER_HANDLE=`shmysql host=masterhost.com dbname=example_db user=repl_user password=password`

#lock tables before getting master settings keep session open with shellsql
shsql $MASTER_HANDLE “FLUSH TABLES WITH READ LOCK”
echo “locked”

#while locked, dump master info, create and dump database into slave
echo “creating example_db”
echo create database example_db | mysql -uroot
echo starting dump
mysqldump –skip-opt example_db -h masterhost.com -urepl_user -ppassword –databases | mysql -uroot
echo “aquire master nfo”
MASTER_LOG_FILE=`echo SHOW MASTER STATUS | mysql -h masterhost.com -urepl_user -ppassword -N | awk ‘ {print $1} ‘`
MASTER_LOG_POS=`echo SHOW MASTER STATUS | mysql -h masterhost.com -urepl_user -ppassword -N | awk ‘ {print $2} ‘`
echo $MASTER_LOG_FILE
echo $MASTER_LOG_POS
#EXAMPLE ROWS=`shsql $HANDLE “select count(*) from SERVER”`
cat << EOF
CHANGE MASTER TO MASTER_HOST=’masterhost.com’, MASTER_USER=’repl_user’, MASTER_PASSWORD=’password’, MASTER_LOG_FILE=’$MASTER_LOG_FILE’, MASTER_LOG_POS=$MASTER_LOG_POS;
EOF

#echo $ENSLAVE_SQL

shsql $MASTER_HANDLE “UNLOCK TABLES”
echo “unlocked”
shsqlend $MASTER_HANDLE

echo SHOW MASTER STATUS | mysql -uroot
#TODO: script to add [mysqld] server-id=2 log-bin=mysql-binentry on slave my.cnf


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!