3 # mkmysqlslave -- import MySQL master data and start slave
4 # Copyright (c)2015-2016 Alexander Barton <alex@barton.de>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
22 echo "$NAME [<options> ...] <file>"
24 echo " --host|-h <host>"
25 echo " MySQL server hostname [\"$MYHOST\"]."
26 echo " --user|-u <user>"
27 echo " MySQL server user [\"$MYUSER\"]."
28 echo " --password|-p <password>"
29 echo " MySQL server password [\"$MYPW\"]".
32 echo " Path and name of SQL dump file."
41 while [ $# -gt 0 ]; do
69 if [ -z "$INFILE" ]; then
72 if [ ! -r "$INFILE" -a "$INFILE" != "-" ]; then
73 echo "$NAME: Can't read \"$INFILE\"!"
77 echo "Initializing MySQL server on \"$MYHOST\" (user \"$MYUSER\") as replication slave:"
79 [ -n "$MYPW" ] && PWSWITCH="--password='$MYPW'" || PWSWITCH=""
81 echo "Stopping running slave threads ..."
82 mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH -e 'stop slave'; r=$?
84 echo "Warning: FAILED to stop MySQL slave threads, code $r!" >&2
88 if [ "$INFILE" != "-" ]; then
89 echo "Importing \"$INFILE\" ..."
91 echo "Importing from standard input ..."
93 ( echo "SET SESSION SQL_LOG_BIN=0;"; cat "$INFILE" ) \
94 | mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH; r=$?
97 echo "MySQL import FAILED, code $r!" >&2
101 echo "Starting slave threads ..."
102 mysql -h "$MYHOST" -u "$MYUSER" $PWSWITCH -e 'start slave'; r=$?
103 if [ $r -ne 0 ]; then
104 echo "FAILED to start MySQL slave threads, code $r!" >&2
108 echo "MySQL slave initialized."