Skip to content
Snippets Groups Projects
Commit 4c0c6c14 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
inc_mysql_config.sh
# ------------------------------------------------------------
# config for mysql connections
# ------------------------------------------------------------
masterdb=mysql-master.example.com
slavedb=localhost
paramdbMaster="-h $masterdb -u root -psecretpassword"
paramdbSlave="-h $slavedb -u root "
# Mysql slave Skripte #
## Replikation neu aufsetzen ##
Aufruf:
reinit_mysql_replication.sh
Es gibt keine Parameter.
Wenn eine bestehende Mysql Replikation nicht mehr funktioniert, so half mir etliche
Male im Laufe des Webmaster/ Sysadmin Daseins das top-down-Skript weiter: es ...
* zeigt aktuellen Slave Status
* wartet dann auf ein RETURN, bevor die Replikation neu aufgesetzt wird
Aktionen zum Neuaufsetzen der Replikation: das Skript ...
* liest die Position des Binlog am Master
* sperrt den Master für Schreibaktionen
* holt aktuelle Datenbank-Dumps vom Master
* hebt Schreibsperre am Master auf
* stoppt den Slave
* importiert Dumps des Master auf dem Slave
* setzt am Slave binfile und Position
* startet den Slave
Konfiguration:
Man benötigt einen Mysql Client. Ich hatte die Skripte am Mysql-Slave zu liegen.
Die *dist Datei umkopieren und die Hostnamen und Root-Passwörter setzen.
## Slave status ##
Aufruf:
show_slave_status.sh
Es gibt keine Parameter.
Das Skript show_slave_status.sh muss auf dem Slave liegen.
Es wird der mysql-Slave status angezeigt. Nach einer Trennlinie wird angezeigt, ob
die Slave Prozesse laufen. Ein abschliessendes OK oder Fehler zeigen schnell den
Status auf.
```
...
----------------------------------------------------------------------
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
OK
```
#!/bin/bash
# ======================================================================
#
# Replikation Datenbanken neu einrichten
#
# Es wird die Master-DB gedumpt und auf dem Slave importiert.
# Auf dem Slave wird die Bin-log-Position vom Master Stand
# vor dem Dump eingestellt.
#
# ----------------------------------------------------------------------
# 2008++ Axel Hahn
# ======================================================================
# ----------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------
about="REINIT REPLICATION - V1.5"
daemonlog=/var/log/mysqld.log
mydate=`date +%Y%m%d_%H%M%S`
prefix=/tmp/reinit_
dumpMaster=${prefix}_{$mydate}_dump_master
dumpSlave=${prefix}_{$mydate}_dump_slave
masterstatus=${prefix}_{$mydate}_status_master
tmpfile=/tmp/reinit_tmp$$
EXCLUDEDB="mysql information_schema"
. `dirname $0`/inc_mysql_config.sh || exit 1
# ----------------------------------------------------------------------
# FUNKTIONEN
# ----------------------------------------------------------------------
# headline 1
function h1() {
echo
echo "##### $* #####"
echo
}
# headline 2
function h2() {
echo
echo ===== $* - `date` =====
}
# Abfrage des DB-Passworts - bei falschem PW wird Skript beendet.
function ask4RootPW() {
echo -n Passwort DB-Root \>
read pw1
echo "SHOW DATABASES;" | mysql $paramdbMaster${pw1} >>/dev/null 2>&1
if [ $? -ne 0 ]; then
echo Falsches Passwort. Exit.
exit 1
fi
paramdbMaster="${paramdbMaster}${pw1}"
paramdbSlave="${paramdbSlave}${pw1}"
}
function quit() {
rc=$?
set +vx
h1 FEHLER $rc aufgetreten
echo Bei der letzten Aktion ist ein Fehler \(Returncode $rc\) aufgetreten.
echo Details zum Fehler stehen allenfalls in der Ausgabe oben.
echo Das Skript `basename $0` wird abgebrochen.
exit 2
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
echo
echo "########## $about ###########"
echo
echo Master: $masterdb
echo Slave: $slavedb
echo
# ask4RootPW
# paramdbMaster=$DUMPPARAM
# paramdbSlave=
# -- $DBLIST erstellen (Liste aller DBs exclusive der in der Variable $EXCLUDEDB aufgefuehrten)
for mydb in `mysql --batch --skip-column-names --execute="SHOW DATABASES" $paramdbMaster`
do
echo " $EXCLUDEDB " | fgrep " $mydb " >/dev/null || DBLIST="$DBLIST $mydb"
done
h1 Slave Status
echo "SHOW SLAVE STATUS \G;"
echo "SHOW SLAVE STATUS \G;" | mysql $paramdbSlave >$tmpfile || quit
cat $tmpfile
echo
echo --- relevante Status-Infos:
fgrep "Slave_IO_Running" $tmpfile
fgrep "Slave_SQL_Running" $tmpfile
fgrep "Last_Error" $tmpfile
rm -f $tmpfile
echo "SHOW MASTER STATUS \G;" | mysql $paramdbMaster || quit
echo
echo Sicher, dass die Replikation neu eingerichtet werden muss?
echo Abbrechen mit Ctrl + C, Return startet Neueinrichtung der Replikation.
read dummy
# exit
# zum Debuggen:
# set -vx
# ----------------------------------------------------------------------
h1 Master-DB holen
h2 aktuellen Status sichern
echo "SHOW MASTER STATUS \G;"
echo "SHOW MASTER STATUS \G;" | mysql $paramdbMaster >$masterstatus || quit
statusBinfile=`fgrep "File:" $masterstatus | cut -f 2 -d ":" | sed "s# ##g"`
statusPos=`fgrep "Position:" $masterstatus | cut -f 2 -d ":" | sed "s# ##g"`
echo Binfile =$statusBinfile
echo Position=$statusPos
h2 Schreibaktionen auf Master anhalten
echo "FLUSH TABLES WITH READ LOCK;"
echo "FLUSH TABLES WITH READ LOCK;" | mysql $paramdbMaster || quit
h2 Dumpe Master-DB
echo ... ohne $EXCLUDEDB
echo mysqldump --all-databases --lock-all-tables
mysqldump --all-databases --lock-all-tables $paramdbMaster >$dumpMaster || quit
# echo Liste der DBs:
# echo $DBLIST
# echo
# echo mysqldump --add-locks -Q -f --databases [Liste der Datenbanken]
# mysqldump $paramdbMaster --add-locks -Q -f --databases $DBLIST > $dumpMaster || quit
h2 Schreibaktionen wieder zulassen
echo "UNLOCK TABLES;"
echo "UNLOCK TABLES;" | mysql $paramdbMaster || quit
# ----------------------------------------------------------------------
h1 Import auf dem Slave
h2 Slave anhalten
echo "STOP SLAVE \G;"
echo "STOP SLAVE \G;" | mysql $paramdbSlave || quit
h2 Import der Master DB auf dem Slave
echo "cat $dumpMaster | mysql"
cat $dumpMaster | mysql $paramdbSlave || quit
h2 auf dem Slave Binfile und Position setzen
echo "CHANGE MASTER TO MASTER_LOG_FILE = '$statusBinfile'"
echo "CHANGE MASTER TO MASTER_LOG_FILE = '$statusBinfile'" | mysql $paramdbSlave || quit
echo "CHANGE MASTER TO MASTER_LOG_POS = $statusPos"
echo "CHANGE MASTER TO MASTER_LOG_POS = $statusPos"| mysql $paramdbSlave || quit
h2 Slave starten
echo "START SLAVE \G;"
echo "START SLAVE \G;" | mysql $paramdbSlave || quit
h2 10 Sek. Pause ...
sleep 10
# ----------------------------------------------------------------------
h1 Kontrolle
h2 Logfile
echo tail $daemonlog
tail $daemonlog
h2 Slave Status
echo "SHOW SLAVE STATUS \G;"
echo "SHOW SLAVE STATUS \G;" | mysql $paramdbSlave
# ----------------------------------------------------------------------
h1 Cleanup
# echo Diese Dateien exisitieren noch - falls Dumps nicht gebraucht werden, dann löschen:
ls -l $prefix*
echo rm -f $prefix*
rm -f $prefix*
echo bye.
# ----------------------------------------------------------------------
#!/bin/bash
# ======================================================================
#
# Mysql slave status zeigen
#
# ----------------------------------------------------------------------
# 2008++ Axel Hahn
# ======================================================================
mysql -e "show slave status \G;"
echo ----------------------------------------------------------------------
mysql -e "show slave status \G;" | fgrep -i running
mysql -e "show slave status \G;" | fgrep -i running | fgrep -i no
if [ $? -eq 0 ]; then
echo "Fehler gefunden :-/"
else
echo OK
fi
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment