Attachment 'alter_mysql_slave.sh'

Download

#!/bin/sh
#
###
#
# alter_mysql_slave - alter slave replication parameters
# Copyright (C) 2008 Anchor Systems - http://www.anchor.com.au/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
###
#
# This script has two functions:
# 1. Make necessary changes on a slave mysql server to allow it to become the
#    new master server for a cluster.
# 2. Point an existing slave server to a new mysql master server.
#
# Usage:
# alter_mysql_slave [--slavetomaster | --newmaster <HOST>]
#
# --slavetomaster selects function 1 above.
# --newmaster <HOST> selects function 2 above, and attempts to reuse
# the password defined in my.cnf already for replication. It will
# not specify a binlog name or position so as to begin at the start of
# the first log.

usage() {
        cat << BLAH
Usage:

alter_mysql_slave [--slavetomaster | --newmaster <HOST>]

BLAH
}

errorsub() {
        echo "$@"
        exit 1
}

slavetomaster() {
        # Stop the slave replication threads
        echo "mysql -e \"STOP SLAVE\""
        /usr/bin/mysql -e "STOP SLAVE" || errorsub "Error stopping slave threads."

        # Remove the master.info slave replication details file
        datadir=`/usr/bin/mysqladmin variables | /usr/bin/tr -d '|' | /bin/grep datadir | /usr/bin/awk '{ print $2 }'`
        if [ -z "$datadir" ]
        then
                errorsub "Error getting MySQL data directory path."
        fi
        echo "rm -f $datadir/master.info"
        rm -f "$datadir/master.info" || errorsub "Error removing master.info file."

        # Perform RESET MASTER to ensure we start from the beginning of the first log
        echo "mysql -e \"RESET MASTER\""
        /usr/bin/mysql -e "RESET MASTER" || errorsub "Error performing RESET MASTER."

        # We're just about done
        cat << STUFF
Done.

Now you must do the following:
1. Ensure the IP address for the dbmaster is removed from any machine
it may have been on, and assign the IP address to this machine before slaves
will connect to it:

ip addr add X.X.X.X/YY dev eth0

2. Ensure this server's MySQL is not configured in read-only mode, which
will prevent any writes being made to the database (essential for a master).

3. If this server will be the master for an extended period of time you
may wish to change the my.cnf to remove any slave replication statements.
Without making these changes the next time MySQL is restarted on this
server it will attempt to reconnect to the old master.
STUFF
}

newmaster() {
        master=$1
        if [ -z "$master" ]
        then
                usage
                exit 1
        fi

        # Stop the Slave before changing master
        echo "mysql -e \"STOP SLAVE\""
        /usr/bin/mysql -e "STOP SLAVE" || errorsub "Error stopping slave threads."

        # Change the master server
        echo "mysql -e \"CHANGE MASTER TO MASTER_HOST = '${master}', MASTER_LOG_FILE='', MASTER_LOG_POS=4\""
        /usr/bin/mysql -e "CHANGE MASTER TO MASTER_HOST = '${master}', MASTER_LOG_FILE='', MASTER_LOG_POS=4" || errorsub "Error changing master server."

        # Start the Slave threads again
        echo "mysql -e \"START SLAVE\""
        /usr/bin/mysql -e "START SLAVE" || errorsub "Error restarting slave threads."

        echo "Done."
}

if [ $# -lt 1 -o $# -gt 2 ]
then
        usage
elif [ "$1" = "--slavetomaster" ]
then
        slavetomaster
elif [ "$1" = "--newmaster" ]
then
        newmaster $2
else
        usage
fi
exit 0

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-05-31 11:14:57, 8.0 KB) [[attachment:IPaddr2_vlan.sh]]
  • [get | view] (2013-05-31 11:14:58, 361.3 KB) [[attachment:LCA2008-talk.odp]]
  • [get | view] (2013-05-31 11:14:58, 832.2 KB) [[attachment:LCA2008-talk.pdf]]
  • [get | view] (2013-05-31 11:14:57, 3.7 KB) [[attachment:alter_mysql_slave]]
  • [get | view] (2013-05-31 11:14:57, 3.7 KB) [[attachment:alter_mysql_slave.sh]]
  • [get | view] (2013-05-31 11:14:57, 1.5 KB) [[attachment:authkeys]]
  • [get | view] (2013-05-31 11:14:57, 1.5 KB) [[attachment:authkeys.txt]]
  • [get | view] (2013-05-31 11:14:58, 1.1 KB) [[attachment:check-drbd.sh]]
  • [get | view] (2013-05-31 11:14:57, 11.4 KB) [[attachment:check_replication.pl]]
  • [get | view] (2013-05-31 11:14:57, 6.1 KB) [[attachment:cib.xml.template]]
  • [get | view] (2013-05-31 11:14:58, 8.0 KB) [[attachment:db-export-mysqlstandby.pl]]
  • [get | view] (2013-05-31 11:14:57, 3.8 KB) [[attachment:drbd.conf.txt]]
  • [get | view] (2013-05-31 11:14:58, 1.3 KB) [[attachment:drbddisk.sh]]
  • [get | view] (2013-05-31 11:14:57, 1.5 KB) [[attachment:ha.cf.txt]]
  • [get | view] (2013-05-31 11:14:58, 9.0 KB) [[attachment:init_mysql_slave.pl]]
  • [get | view] (2013-05-31 11:14:58, 1.2 KB) [[attachment:logd.cf.txt]]
  • [get | view] (2013-05-31 11:14:58, 2.2 KB) [[attachment:lvs-helper.sh]]
  • [get | view] (2013-05-31 11:14:57, 1.8 KB) [[attachment:my.cnf.txt]]
  • [get | view] (2013-05-31 11:14:57, 2.2 KB) [[attachment:myslave.cnf.txt]]
  • [get | view] (2013-05-31 11:14:57, 5.5 KB) [[attachment:mysqld.sh]]
  • [get | view] (2013-05-31 11:14:58, 2.0 KB) [[attachment:mysqlslave.cnf.txt]]
  • [get | view] (2013-05-31 11:14:57, 5.7 KB) [[attachment:mysqlslaved.sh]]
  • [get | view] (2013-05-31 11:14:57, 3.5 KB) [[attachment:write_heartbeat.pl]]

You are not allowed to attach a file to this page.