StefanKonarski.de - Datenbanken https://stefankonarski.de/category/tags/datenbanken de MySQL 5.5: Master-Master-Replikation https://stefankonarski.de/content/mysql-55-master-master-replikation <div class="field field-name-taxonomy-vocabulary-3 field-type-taxonomy-term-reference field-label-above"> <div class="field-items"> <div class="field-item even"><a href="/category/tags/administration">Administration</a></div> <div class="field-item odd"><a href="/category/tags/datenbanken">Datenbanken</a></div> <div class="field-item even"><a href="/category/tags/mysql">MySQL</a></div> <div class="field-item odd"><a href="/category/tags/backup">Backup</a></div> </div> </div> <div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even"><p>Auf die Datenbanken von zwei MySQL-Server kann parallel schreibend und lesend zugegriffen werden - die Daten werden gleichzeitig synchronisiert.</p> <!--break--> <h2>Master-Master-Replikation - das Prinzip</h2> <p>Im Prinzip werden zwei Master-Slave-Replikationen eingerichtet. Jeder Master ist der Slave der anderen Installation.</p> <p>Damit es zu keinem Konflikt bei Primärschlüsseln kommt, die automatisch hochgezählt werden, wird in der Konfigurationsdatei ein Startwert und ein Intervall angegeben. </p> <p>Im folgenden soll der Startwert von Server 1 ebenfalls 1 betragen und der von Server 2 entsprechend 2. Das Intervall soll für beide 2 betragen. Für die Zählfolge ergibt sich damit:</p> <table> <tr> <td>Server 1:</td><td>1</td><td>3</td><td>5</td><td>7</td><td>9</td><td>[...]</td> </tr> <tr> <td>Server 2:</td><td>2</td><td>4</td><td>6</td><td>8</td><td>10</td><td>[...]</td> </tr> </table> <p>Das bedeutet aber auch, dass sehr häufig Werte übersprungen werden, insbesondere wenn ein MySQL-Server nur als Ausfallsicherung mitläuft. Dies sollte bedacht werden, wenn Primärschlüssel z.B. für Rechnungsnummern benutzt werden und Sprünge unschön wären.</p> <h2>Ausgangssituation</h2> <p>Der MySQL-Server 1 läuft bereits und hat einen gewissen Datenbestand. MySQL-Server 2 wurde neu aufgesetzt und soll den kompletten Datenbestand synchronisieren, mit Ausnahme der Datenbank mysql mit Benutzerdaten.</p> <p>Beide Server sind via VPN über die privaten IPs 192.168.5.2 und 192.168.5.4 miteinander verbunden, die in der Datei hosts als db1 und db2 definiert sind. Damit sind die Server unter ihren Namen im Netzwerk erreichbar. </p> <h2>Datenbestand sichern</h2> <p>Zunächst wird via mysqldump ein Backup des gesamten Datenbestandes von Master 1 inklusive der Master-Daten erstellt:</p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump <span style="color: #660033;">-p</span> <span style="color: #660033;">--master-data</span> <span style="color: #660033;">--all-databases</span> <span style="color: #660033;">--result-file</span>=dbdata1.sql</div></li></ol></pre></div> <p>Diese Daten werden erstmal von Master 2 eingelesen - die Benutzerdatenbank wird nun überschrieben! Ein Vorschlag führt über die mysql-shell, die mit <tt>mysql</tt> aufgerufen wird. Ist der Server bisher nur gestartet worden, besitzt er Benutzer root noch kein Kennwort. Nach dem Aufruf wird das eben erstellte Backup eingelesen:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">source dbdata1.sql;</div></li></ol></pre></div> <p>Vorteile von diesem Schritt sind für mich 2 Punkte, die beide mit der Benutzerdatenbank zu tun haben:</p> <ul> <li><strong>Passwort für root</strong><br /> Ein neues Kennwort für den Benutzer root ist schnell erstellt.<br /></li> <li><strong>Host der anderen Benutzer ändern</strong><br /> User, die vorher vom Server db2 auf db1 zugreifen, sollen dass nun von db2 dürfen.</li> </ul> <p>Die folgenden drei Zeilen erledigen das schnell:</p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">use mysql;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">UPDATE user <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #007800;">Password</span>=password<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'geh3im1'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> WHERE <span style="color: #007800;">User</span>=<span style="color: #ff0000;">'root'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">UPDATE user SET <span style="color: #007800;">Host</span>=<span style="color: #ff0000;">'db1'</span> WHERE <span style="color: #007800;">Host</span>=<span style="color: #ff0000;">'db2'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">UPDATE db SET <span style="color: #007800;">Host</span>=<span style="color: #ff0000;">'db1'</span> WHERE <span style="color: #007800;">Host</span>=<span style="color: #ff0000;">'db2'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">FLUSH PRIVILEGES;</div></li></ol></pre></div> <p>Für eine genauere Beschreibung siehe <a href="/content/mysql-55-master-slave-replikation">MySQL 5.5 Master-Slave-Replikation</a>.</p> <h2>Benutzer für die Replikation</h2> <p>Auf beiden Servern wird ein MySQL-User benötigt, mit dem die andere Datenbank die zu replizierenden Daten abholen kann. Die jeweiligen SQL-Befehle dazu lauten:</p> <h3>Für Master1 (db1)</h3> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">GRANT REPLICATION SLAVE ON *.* TO repl@'db2' IDENTIFIED BY 'geheim2';</div></li></ol></pre></div> <p>und für Master 2 entsprechend</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">GRANT REPLICATION SLAVE ON *.* TO repl@'db1' IDENTIFIED BY 'geheim1';</div></li></ol></pre></div> <h2>Konfiguration von Master 2</h2> <p>Jeder Server benötigt seine eigene Server-ID, Binary-Logging und die erwähnten Einstellungen für automatisch inkrementelle Werte. Außerdem können nur bestimmte Datenbanken ausgewählt oder ignoriert werden. Für den neuen Master 2 sieht eine Konfiguration bei der alle Datenbanken mit Ausnahme von der Datenbank mysql repliziert werden sollen folgendermaßen aus:</p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># einzigartige ID für jeden Server</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">server-id = <span style="color: #000000;">2</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Binary Log und Replizierung für DB mysql deaktivieren</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">binlog-ignore-db = mysql</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">replicate-ignore-db = mysql</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Eigene Befehle nicht erneut replizieren (standard)</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">replicate-same-server-id = <span style="color: #000000;">0</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Beim automatischen Hochzählen nicht 1, sondern 2 addieren</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">auto-increment-increment = <span style="color: #000000;">2</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Beginne automatisch inkrementelle Werte mit 1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">auto-increment-offset = <span style="color: #000000;">1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Protokolldaten nach 21 Tagen löschen. Ein Slave könnte für bis zu 21</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Tage offline sein und dann immer noch den Datenbestand replizieren</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">expire_logs_days = <span style="color: #000000;">21</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Bei einer Dateigröße über 500 MB wird in eine neue Datei geschrieben</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">max_binlog_size = 500M</div></li></ol></pre></div> <h2>Slave auf Master 2 starten</h2> <p>Nach Änderung der Konfigurationsdatei und erfolgreichem Neustart wird in der mysql-shell der Slave gestartet. Zunächst muss der Master-User mitgeteilt werden:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">CHANGE MASTER TO MASTER_HOST='db1', MASTER_USER='repl', MASTER_PASSWORD='geheim';</div></li></ol></pre></div> <p>Ob alle Daten richtig sind - neben den eben eingegebenen Daten sind dies insbesondere die Bezeichnung und die Position des Master-Logfiles auf db1 - lässt sich mit dem Status des Slaves feststellen:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">show slave status\G</div></li></ol></pre></div> <p>In der Ausgabe sollte folgende Zeilen geprüft werden:</p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Master_Host: db1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Master_User: repl</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Master_Port: <span style="color: #000000;">3306</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Master_Log_File: db1-bin.000007</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Relay_Log_File: hz01-relay-bin.000001</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Slave_IO_Running: No</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Slave_SQL_Running: No</div></li></ol></pre></div> <p>Ist alles in Ordnung, kann der Slave gestartet werden:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">start slave;</div></li></ol></pre></div> <p>Mit der Status-Abfrage <tt>show slave status\G</tt> kann man an den Zeilen</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Slave_IO_Running: Yes</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Slave_SQL_Running: Yes</div></li></ol></pre></div> <p>erkennen, dass der Slave läuft. Dies ist aber erst eine Master-Slave-Replikation.</p> <h2>Konfiguration von Master 1</h2> <p>Bis auf zwei kleine Änderungen für Server-ID und auto-increment-offset ist diese mit der oberen Konfiguration identisch:</p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">server-id = <span style="color: #000000;">1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">binlog-ignore-db=mysql</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">replicate-ignore-db=mysql</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">replicate-same-server-id = <span style="color: #000000;">0</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Beginne automatisch inkrementelle Werte mit 2</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">auto-increment-increment = <span style="color: #000000;">2</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">auto-increment-offset = <span style="color: #000000;">2</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">expire_logs_days = <span style="color: #000000;">21</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">max_binlog_size = 500M</div></li></ol></pre></div> <p>Damit die Konfiguration gültig ist, muss der MySQL-Server neu gestartet werden.</p> <h2>Slave auf Master 1 starten</h2> <p>Wenn die Zeile in der Statusanzeige von Slave2</p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">Seconds_Behind_Master: <span style="color: #000000;">0</span></div></li></ol></pre></div> <p>den Wert 0 ausgibt, haben beide Server den gleichen Datenbestand (bezogen auf die zu replizierenden Datenbanken). Mit dem Master-Status von Master2</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">SHOW MASTER STATUS\G</div></li></ol></pre></div> <p>erhält man die letzten relevanten Informationen</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> File: db2-bin.000003</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Position: 107</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> Binlog_Do_DB: </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">Binlog_Ignore_DB: mysql</div></li></ol></pre></div> <p>um den Slave auf db1 zu starten. Hierzu die mysql-Shell aufrufen und entsprechend wie oben schon beschrieben die Daten von Master2 eingeben:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">CHANGE MASTER TO MASTER_HOST='db2', MASTER_USER='repl', MASTER_PASSWORD='geheim2', MASTER_LOG_FILE='db2-bin.000003', MASTER_LOG_POS=107;</div></li></ol></pre></div> <p>Nach einer Kontrolle mit <tt>show slave status\G</tt> kann auch der Slave 1 mit <tt>start slave;</tt> gestartet werden.</p> <p>Alle SQL-Befehle, die nicht die Datenbank mysql betreffen, werden nun zeitnah auf dem anderen Server ausgeführt. Damit beide Server auch tatsächlich als Master - also für Schreibzugriffe - genutzt werden können, muss unbedingt der Slave-Status beider Server beobachtet werden.</p></div> </div> </div> Tue, 21 Feb 2012 22:12:54 +0000 stefan 280 at https://stefankonarski.de https://stefankonarski.de/content/mysql-55-master-master-replikation#comments MySQL 5.5: Master-Slave-Replikation https://stefankonarski.de/content/mysql-55-master-slave-replikation <div class="field field-name-taxonomy-vocabulary-3 field-type-taxonomy-term-reference field-label-above"> <div class="field-items"> <div class="field-item even"><a href="/category/tags/administration">Administration</a></div> <div class="field-item odd"><a href="/category/tags/datenbanken">Datenbanken</a></div> <div class="field-item even"><a href="/category/tags/mysql">MySQL</a></div> <div class="field-item odd"><a href="/category/tags/backup">Backup</a></div> </div> </div> <div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even"><h2>Allgemeines</h2> <p>Das Duplizieren oder Vervielfältigen der Daten eines MySQL-Servers wird replizieren genannt. Es ist möglich einen kompletten Server, ausgewählte Datenbanken oder sogar nur ausgewählte Tabellen einer Datenbank zu replizieren.</p> <p>In jedem Fall wird mindestens eine weitere MySQL-Installation auf einem anderen Rechner benötigt. Nicht zwingend erforderlich, aber aus Gründen der Kompatibilität sollten die MySQL-Versionen beider Installationen identisch sein.</p> <p>Im folgenden soll zwischen den zwei Varianten der Replikation vollständiger Datenbanken unterschieden werden:</p> <ol> <li>Vollständige Replikation aller Datenbanken des Masters</li> <li>Replikation aller Datenbanken, aber einzelne Datenbanken ausnehmen</li> </ol> <p>Die Replikation ausgewählter Datenbanken ist zwar auch möglich, birgt aber Risiken. Soll z.B. nur die die Daten DB1 repliziert werden, würde der folgende Befehl ignoriert werden:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">USE DB2;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">UPDATE db1.foo SET bar=bar+1 WHERE id = 1;</div></li></ol></pre></div> <h2>Vorbereiten der Master-Instanz</h2> <p>Es müssen einige Einstellungen in der Datei my.cnf angepasst werden.</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">[mysqld]</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"># Jede MySQL-Instanz benötigt eine eigene einzigartige Server-ID</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">server-id = 1 </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"># Binäres protokollieren aktivieren</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">log-bin</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"># Nach 21 Tagen sollen alte Einträge gelöscht werden</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">expire_logs_days = 21</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"># Maximale Größe eines Logfiles</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">max_binlog_size = 500M</div></li></ol></pre></div> <p>Es kann sinnvoll sein, die Datenbanken mysql und test nicht zu replizieren. Die my.cnf wird dann um folgende Zeilen ergänzt:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">binlog-ignore-db=mysql</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">binlog-ignore-db=test</div></li></ol></pre></div> <p>Außerdem wird noch ein Replikations-User benötigt. Der entsprechende SQL-Befehl dazu lautet:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">GRANT REPLICATION SLAVE ON *.* TO repl@'slave' IDENTIFIED BY 'geheim';</div></li></ol></pre></div> <ul> <li><strong>repl</strong>: Der Benutzername zur Replikation</li> <li><strong>slave</strong>: Der Name oder die IP des Slaves</li> <li><strong>geheim</strong>: Das Passwort des Benutzers repl</li> </ul> <p>Anschließend den MySQL-Master-Server neu starten.</p> <h2>Erstellen eines Backups</h2> <p>Ein Weg zur Sicherung der Daten führt über mysqldump. Zunächst sollte der schreibende Zugriff auf alle Tabellen blockiert werden:</p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysql <span style="color: #660033;">-u</span> root <span style="color: #660033;">-p</span> <span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'FLUSH TABLES WITH READ LOCK'</span></div></li></ol></pre></div> <p>Mit mysqldump (gehört zu jeder mysql-Installation) lässt sich der aktuelle Master-Stand schnell sichern:</p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump <span style="color: #660033;">-u</span> root <span style="color: #660033;">-p</span> <span style="color: #660033;">--master-data</span> <span style="color: #660033;">--all-databases</span> <span style="color: #660033;">--result-file</span>=.<span style="color: #000000; font-weight: bold;">/</span>master-data_all.sql</div></li></ol></pre></div> <p>Da es für mysqldump noch keine Option für den Ausschluss ganzer Datenbanken hat, habe ich <a href="/content/mysqldump-datenbanken-ignorieren">dieses kleine Shell-Script </a>geschrieben.</p> <p><strong>Danach die Schreibblockade wieder lösen mit:</strong></p> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysql <span style="color: #660033;">-u</span> root <span style="color: #660033;">-p</span> <span style="color: #660033;">-Bse</span> <span style="color: #ff0000;">'unlock tables'</span></div></li></ol></pre></div> <h2>Vorbereiten des Slave-Instanz</h2> <p>Zunächst sollten in der my.cnf vom Slave einige Zeilen ergänzt werden:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">[mysqld]</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"># Jede MySQL-Instanz benötigt eine eigene einzigartige Server-ID</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">server-id = 2</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"># Folgende Datenbanken sollen ignoriert werden </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">replicate-ignore-db=test</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">replicate-ignore-db=mysql</div></li></ol></pre></div> <p>Den Slave-Server neu starten.</p> <h2>Slave starten</h2> <p>Zuletzt werden nun noch drei SQL-Befehle auf dem Slave benötigt:</p> <p>1. Master-Informationen</p> <p>MASTER_LOG_FILE und MASTER_LOG_POS sollte bereits durch die Option "--master-data" im Dumpfile übermittelt worden sein. Es fehlen noch die Angaben zum Replikations-Benutzers auf dem Master, die vorhin angelegt worden sind:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysql&gt; CHANGE MASTER TO MASTER_HOST='master', MASTER_USER='repl', MASTER_PASSWORD='geheim';</div></li></ol></pre></div> <p>2. Backup einspielen</p> <p>von der mysql-Shell mit:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysql&gt; source $DUMPFILE.sql;</div></li></ol></pre></div> <p>oder von der System-Shell auch so:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysql -p &lt; $DUMPFILE.sql</div></li></ol></pre></div> <p>Dann kann die Replikation gestartet werden:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysql&gt; start slave;</div></li></ol></pre></div> <p>Ob die Replikation ordnungsgemäß ausgeführt wird, erfährt man mit:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">show slave status\G</div></li></ol></pre></div> <p>Wichtig sind die Zeilen</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">[...]</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">Slave_IO_Running: Yes</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">Slave_SQL_Running: Yes</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">[...]</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">Seconds_Behind_Master: 0</div></li></ol></pre></div> <p>Die ersten beiden Zeilen zeigen an, ob die Replikation überhaupt läuft, die letzte, wie erfolgreich die Replikation ist. Die oberen Zeilen zeigen das optimale Ergebnis an.</p> <h2>Mögliche Fehler</h2> <p><strong>Error Duplicate entry</strong></p> <p>Wie die Bezeichnung schon sagt, ein Eintrag ist bereits vorhanden. Wahrscheinlich ist etwas mit dem Dump nicht in Ordnung. Man sollte ein neues Backup machen. Wenn man weiß, was man tut, kann man die Fehlermeldung überspringen:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">set global sql_slave_skip_counter=1;start slave;</div></li></ol></pre></div> <h2>Informationen zur Master-Slave-Konfiguration</h2> <p><strong>Eine Slave ist kein Backup</strong></p> <p>Ähnlich wie ein RAID 1 zur Spiegelung von Festplatten kann eine Datenbank-Replikation nicht als Backup dienen. Alle SQL-Befehle auf dem Master werden zeitnah auf dem Slave ausgeführt. Ein unachtsamer drop, truncate oder delete Befehl vernichtet Daten sofort auf beiden Instanzen. Eine Replikation hilft nur bei Ausfall des Masters und beim Störungsfreien Backup.</p></div> </div> </div> Tue, 07 Feb 2012 19:02:38 +0000 stefan 265 at https://stefankonarski.de https://stefankonarski.de/content/mysql-55-master-slave-replikation#comments mysqldump: Datenbanken ignorieren https://stefankonarski.de/content/mysqldump-datenbanken-ignorieren <div class="field field-name-taxonomy-vocabulary-3 field-type-taxonomy-term-reference field-label-above"> <div class="field-items"> <div class="field-item even"><a href="/category/tags/administration">Administration</a></div> <div class="field-item odd"><a href="/category/tags/datenbanken">Datenbanken</a></div> <div class="field-item even"><a href="/category/tags/mysql">MySQL</a></div> <div class="field-item odd"><a href="/category/tags/backup">Backup</a></div> <div class="field-item even"><a href="/category/tags/shellscript">ShellScript</a></div> </div> </div> <div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even"><p>Eine Option um Datenbanken beim Sichern zu ignorieren gibt es nicht. Abhilfe schafft dieses kleine Script.</p> <!--break--> <h2>Funktionsweise</h2> <p>Nachdem die ausführbaren Dateien mysql und mysqldump gefunden wurden, wird nach dem Passwort des Datenbank-Benutzers root gefragt. Damit werden alle verfügbaren Datenbanken ermittelt aus denen eine Auswahl getroffen werden kann </p> <p>Es besteht dann die Möglichkeit zu wählen, ob die Option "--master-data" angewendet werden soll.</p> <p>Nach Eingabe eines Dateinamens wird der Inhalt der selektierten Datenbanken von mysqldump in diese Datei geschrieben.</p> <p>Das Script ist unsicher, weil:</p> <ul> <li>das Passwort des MySQL-Benutzers root kurzfristig in eine Datei geschrieben wird</li> <li>das Passwort dann unverschlüsselt im Hauptspeicher verbleibt und unter Umständen in den Cache geschrieben werden kann</li> <li>eine Datei mit der Bezeichnung der Ausgabedatei von mysqldump ohne Rückfrage überschreiben wird (sofern Rechte dazu bestehen)</li> </ul> <h2>Das Script</h2> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">#!/usr/bin/env sh</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Dump selected mysql databases via mysqldump and write</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># the complete dump in a single file including master-data</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">#</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># @copyright 2012 Stefan Konarski</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># @license BSD License</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># @version 0.1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">#</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Compress backup files</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">#pack=$(which bzip2)</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">pack</span>=<span style="color: #ff0000;">''</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># mysqldump binary</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">dump</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysqldump<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$dump</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$dump</span> not executable&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># mysql binary</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">mysql</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysql<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$mysql</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$mysql</span> not executable&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># file for temporary data</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">data</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">mktemp</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #007800;">$0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.XXXXXX<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># trap it</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #7a0874; font-weight: bold;">trap</span> <span style="color: #ff0000;">&quot;rm -f <span style="color: #007800;">$data</span>&quot;</span> <span style="color: #000000;">0</span> <span style="color: #000000;">1</span> <span style="color: #000000;">2</span> <span style="color: #000000;">5</span> <span style="color: #000000;">15</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Read password and concatenate login string</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">dialog</span> <span style="color: #660033;">--title</span> <span style="color: #ff0000;">&quot;Mysql user root&quot;</span> <span style="color: #660033;">--clear</span> <span style="color: #660033;">--insecure</span> <span style="color: #660033;">--passwordbox</span> <span style="color: #ff0000;">&quot;Enter password&quot;</span> <span style="color: #000000;">10</span> <span style="color: #000000;">30</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$data</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">pw</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$data</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;&quot;</span> = <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pw</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">dblogin</span>=<span style="color: #ff0000;">&quot;-u root&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">else</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">dblogin</span>=<span style="color: #ff0000;">&quot;-u root -p<span style="color: #007800;">$pw</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Fetch databases</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">alldbs</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$($mysql $dblogin -Bse 'show databases')</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># initialize database counter and checkbox options</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">dbc</span>=<span style="color: #000000;">0</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">opt</span>=<span style="color: #ff0000;">&quot;&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">for</span> db <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$alldbs</span>; <span style="color: #000000; font-weight: bold;">do</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">opt</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$opt</span> <span style="color: #007800;">$db</span> <span style="color: #007800;">$dbc</span> off&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">dbc</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$dbc</span>+<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">done</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Define height more flexible</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">height</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">5</span>+<span style="color: #007800;">$dbc</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$height</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">25</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">height</span>=<span style="color: #000000;">25</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">dialog</span> <span style="color: #660033;">--checklist</span> <span style="color: #ff0000;">&quot;Choose databases for dump:&quot;</span> <span style="color: #007800;">$height</span> <span style="color: #000000;">40</span> <span style="color: #000000;">30</span> <span style="color: #007800;">$opt</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #007800;">$data</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">mydb</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${data}</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Concatenate database string</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">databases</span>=<span style="color: #ff0000;">&quot;&quot;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">for</span> db <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$mydb</span>; <span style="color: #000000; font-weight: bold;">do</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">d</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$db</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;s/^.//&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;s/.$//&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">databases</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$databases</span> <span style="color: #007800;">$d</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">done</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Master data</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">dialog</span> <span style="color: #660033;">--yesno</span> <span style="color: #ff0000;">&quot;Write master informations (--master-data)&quot;</span> <span style="color: #000000;">0</span> <span style="color: #000000;">0</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000;">1</span> = <span style="color: #007800;">$?</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">mopt</span>=<span style="color: #ff0000;">''</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">else</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">mopt</span>=<span style="color: #ff0000;">&quot;--master-data&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">dialog</span> <span style="color: #660033;">--inputbox</span> <span style="color: #ff0000;">&quot;Enter dump file name&quot;</span> <span style="color: #000000;">0</span> <span style="color: #000000;">0</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$data</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">fn</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${data}</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">$dump</span> <span style="color: #007800;">$mopt</span> <span style="color: #007800;">$dblogin</span> <span style="color: #660033;">--databases</span> <span style="color: #007800;">$databases</span> <span style="color: #660033;">--result-file</span>=<span style="color: #007800;">$fn</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">dialog</span> <span style="color: #660033;">--msgbox</span> <span style="color: #ff0000;">&quot;Done! Databases <span style="color: #007800;">$databases</span> has been saved.&quot;</span> <span style="color: #000000;">0</span> <span style="color: #000000;">0</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">dialog</span> <span style="color: #660033;">--clear</span></div></li></ol></pre></div></div> </div> </div> Tue, 07 Feb 2012 18:41:16 +0000 stefan 263 at https://stefankonarski.de https://stefankonarski.de/content/mysqldump-datenbanken-ignorieren#comments MySQL Backup: Schreibe jede Datenbank in eine eigene Datei https://stefankonarski.de/content/mysql-backup-fuer-jede-datenbank-eine-datei <div class="field field-name-taxonomy-vocabulary-3 field-type-taxonomy-term-reference field-label-above"> <div class="field-items"> <div class="field-item even"><a href="/category/tags/administration">Administration</a></div> <div class="field-item odd"><a href="/category/tags/werkzeuge">Werkzeuge</a></div> <div class="field-item even"><a href="/category/tags/datenbanken">Datenbanken</a></div> <div class="field-item odd"><a href="/category/tags/mysql">MySQL</a></div> <div class="field-item even"><a href="/category/tags/shellscript">ShellScript</a></div> </div> </div> <div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even"><p>Dieses Script sichert alle MySQL-Tabellen und schreibt den Inhalt in jeweils eine eigene Datei.</p> <!--break--> <h2>Beschreibung der Funktionsweise</h2> <p>Das Skript sucht nach den ausführbaren Dateien mysql und mysqldump. Nach Eingabe des Passworts für den Datenbank Benutzer root wird mit der Sicherung begonnen. Der Inhalt einer Datenbank wird in eine eigene Datei im aktuellen Verzeichnis geschrieben.</p> <h2>Standard-Einstellungen</h2> <ul> <li>Datenbank-Benutzer: root</li> <li>Datenbank-Server: localhost</li> <li>Datenbank-Passwort: Eingabe erforderlich<br /></li> <li>Ausgeschlossene Datenbanken: information_schema performance_schema"</li> <li>Backup Files werden in das aktuelle Verzeichnis geschrieben</li> <li>Backup File Prefix: Aktuelles Datum und Name des Servers, z.B. 20120205_example</li> <li>Backup File Suffix: .sql</li> <li>Komprimieren: Nach Backup werden Dateien mit bzip2 komprimiert</li> </ul> <p>Die Einstellungen können im Skript geändert werden.</p> <h2>Das Script</h2> <div class="geshifilter"><pre class="bash geshifilter-bash" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">#!/usr/bin/env sh</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Dump mysql databases via mysqldump and write</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># each database in a single file</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">#</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># @copyright 2012 Stefan Konarski</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># @license BSD License</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># @version 0.1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">#</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Exclude the following databases</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">edb</span>=<span style="color: #ff0000;">&quot;information_schema performance_schema&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Use this prefix for backup file names </span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">prefix</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$(date +'%Y-%m-%d')</span>_<span style="color: #007800;">$(hostname)</span>_&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Use this suffix for backup file names</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">suffix</span>=<span style="color: #ff0000;">&quot;.sql&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Compress backup files</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">pack</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">dump</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysqldump<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$dump</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$dump</span> not executable&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">mysql</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> mysql<span style="color: #7a0874; font-weight: bold;">&#41;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$mysql</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$mysql</span> not executable&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Ask for password without prompt</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">stty</span> <span style="color: #660033;">-echo</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;Enter MySQL password for root: &quot;</span> pass; <span style="color: #7a0874; font-weight: bold;">echo</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">stty</span> <span style="color: #7a0874; font-weight: bold;">echo</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;&quot;</span> = <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pass</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Login without password&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">dblogin</span>=<span style="color: #ff0000;">'-u root'</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">else</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">dblogin</span>=<span style="color: #ff0000;">&quot;-u root -p<span style="color: #007800;">$pass</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">######################</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Execute backup #</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">######################</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">out</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$($mysql $dblogin -Bse 'show databases')</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># List for compressing files</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #007800;">fc</span>=</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Loop over all databases</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">for</span> db <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$out</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">do</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #666666; font-style: italic;"># Don't skip any database as default</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">skipdb</span>=<span style="color: #000000;">0</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #666666; font-style: italic;"># If excludable databases are defined</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$edb</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #666666; font-style: italic;"># Loop over excludable databases</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #000000; font-weight: bold;">for</span> n <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$edb</span>; <span style="color: #000000; font-weight: bold;">do</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$db</span>&quot;</span> = <span style="color: #ff0000;">&quot;<span style="color: #007800;">$n</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">skipdb</span>=<span style="color: #000000;">1</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">break</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #000000; font-weight: bold;">done</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$skipdb</span>&quot;</span> = <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Skip database <span style="color: #007800;">$db</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">continue</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #000000; font-weight: bold;">fi</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">fn</span>=<span style="color: #ff0000;">&quot;./<span style="color: #007800;">$prefix</span><span style="color: #007800;">$db</span><span style="color: #007800;">$suffix</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Dump database <span style="color: #007800;">$db</span> to <span style="color: #007800;">$fn</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">$dump</span> <span style="color: #007800;">$dblogin</span> <span style="color: #660033;">--databases</span> <span style="color: #007800;">$db</span> <span style="color: #660033;">--result-file</span>=<span style="color: #007800;">$fn</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">fc</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$fc</span> <span style="color: #007800;">$fn</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">done</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">######################</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;"># Compress backups #</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #666666; font-style: italic;">######################</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$pack</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$pack</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$fc</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Compress <span style="color: #007800;">$fc</span>&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"> <span style="color: #007800;">$pack</span> <span style="color: #007800;">$fc</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">fi</span></div></li></ol></pre></div></div> </div> </div> Sun, 05 Feb 2012 17:25:05 +0000 stefan 259 at https://stefankonarski.de https://stefankonarski.de/content/mysql-backup-fuer-jede-datenbank-eine-datei#comments mysqldump: Restore von InnoDB Tabellen https://stefankonarski.de/content/mysqldump-restore-von-innodb-tabellen <div class="field field-name-taxonomy-vocabulary-3 field-type-taxonomy-term-reference field-label-above"> <div class="field-items"> <div class="field-item even"><a href="/category/tags/werkzeuge">Werkzeuge</a></div> <div class="field-item odd"><a href="/category/tags/datenbanken">Datenbanken</a></div> <div class="field-item even"><a href="/category/tags/mysql">MySQL</a></div> <div class="field-item odd"><a href="/category/tags/backup">Backup</a></div> </div> </div> <div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even"><p>Der Restore von InnoDB-Tabellen ist ein wenig komplizierter, als der von MyISAM-Tabellen.</p> <!--break--> <p>Es gibt zwei Möglichkeiten:</p> <h2>Editieren des mysqldump Backup-File</h2> <p>Am Anfang des SQL-Scripts folgende Zeilen einfügen:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">SET AUTOCOMMIT = 0;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">SET FOREIGN_KEY_CHECKS=0;</div></li></ol></pre></div> <p>und am Ende des SQL-Scripts folgendes:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">SET FOREIGN_KEY_CHECKS = 1;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">COMMIT;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">SET AUTOCOMMIT = 1;</div></li></ol></pre></div> <p>Dann lässt sich diese Datei <a href="/content/mysqldump-restore-von-myisam-tabellen">wie ein MyISAM Backup zurückschreiben</a>. <h2>Restore über die Kommandozeile</h2> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysql -u$USER -p</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">use $DATENBANKNAME;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">SET FOREIGN_KEY_CHECKS = 0;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">source $MYSQLDUMP_FILE_Name;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">SET FOREIGN_KEY_CHECKS = 1;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">COMMIT;</div></li></ol></pre></div></div> </div> </div> Thu, 29 Dec 2011 11:15:14 +0000 stefan 215 at https://stefankonarski.de https://stefankonarski.de/content/mysqldump-restore-von-innodb-tabellen#comments mysqldump: die Option opt https://stefankonarski.de/content/mysqldump-die-option-opt <div class="field field-name-taxonomy-vocabulary-3 field-type-taxonomy-term-reference field-label-above"> <div class="field-items"> <div class="field-item even"><a href="/category/tags/werkzeuge">Werkzeuge</a></div> <div class="field-item odd"><a href="/category/tags/datenbanken">Datenbanken</a></div> <div class="field-item even"><a href="/category/tags/mysql">MySQL</a></div> </div> </div> <div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even"><p>Die Option --opt wird standardmäßig ausgeführt und bietet eine Sammlung von mysqldump Optionen.</p> <!--break--> <p>Die mysqldump option --opt ist eine Kürzel für folgende sinnvolle Optionen:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump --create-options --disable-keys --extended-insert --lock-tables --add-drop-tables --quick --set-charset [...]</div></li></ol></pre></div> <p>Die einzelnen Optionen haben folgende Bedeutung:</p> <h2>mysqldump Option --create-options</h2> <p>In die Anweisung zum Erstellen einer Tabellen werden alle Tabellenoptionen geschrieben.</p> <h2>mysqldump Option --disable-keys</h2> <p>Nur bei MyIsam Tebellen wirksam. Die Keys werden erst nach vollständigen Zurückschreiben erstellt. Das Zurückschreiben wird dadurch beschleunigt.</p> <h2>Option --extended-insert</h2> <p>Innerhalb einer INSERT-Anweisung werden mehrere Datensätze geschrieben, z.B.:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">INSERT INTO `help_category` VALUES (1,'Geographic',0,''),(2,'Polygon properties',33,''), [...]</div></li></ol></pre></div> <p>an Stelle von</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">INSERT INTO `help_category` VALUES (1,'Geographic',0,'');</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">INSERT INTO `help_category` VALUES (2,'Polygon properties',33,'');</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">[...]</div></li></ol></pre></div> <h2>Option --add-drop-tables</h2> <p>Vor dem Befehl zum Anlegen einer Tabelle wird versucht eine Drop-Anweisung auszuführen. Wenn man den Dump zurück spielt werden also alle Tabellen vorher auf jeden Fall gelöscht:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">DROP TABLE IF EXISTS `columns_priv`;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">CREATE TABLE `columns_priv` (</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">[...]</div></li></ol></pre></div> <h2>Option --add-locks</h2> <p>Vorm Zurückschreiben wird für jede Tabelle ein lock gesetzt und anschließend wieder aufgehoben, z.B.:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">LOCK TABLES `columns_priv` WRITE;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">[...]</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">UNLOCK TABLES;</div></li></ol></pre></div> <p>Während des Zurückspielens ist die Tabelle für alle Zugriffe gesperrt, aber Einfügevorgänge werden beschleunigt.</p> <h2>Option --quick</h2> <p>Jeder Datensatz einer Tabelle wird einzeln vom Server abgerufen. Ansonsten würde mysql versuchen, die gesamten Daten der Tabelle in den Speicher zu schreiben. Bei großen Tabellen kann das zu einer Fehlermeldung und Abbruch führen.</p> <h2>Option --set-charset</h2> <p>Fügt zum Beispiel folgende Zeile in das Dump-File ein:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">/*!40101 SET NAMES utf8 */;</div></li></ol></pre></div> <p>Natürlich kann es andere Standard-Zeichensätze geben.</p> <h2>mysqldump ohne Option --opt</h2> <p>Wer diese Option komplett überspringen will, gibt die Option --skip-opt an:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump -u root -p --skip-opt --all-databases --result-file=dbdata.sql</div></li></ol></pre></div> <p>Es lassen sich aber auch nur einzelne Optionen mit --skip deaktivieren. Die Option --skip-opt hat fast die gleiche Bedeutung wie (Gilt nicht für die Option --skip-add-drop-tables):</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump --skip-create-options --skip-disable-keys --skip-extended-insert --skip-lock-tables --skip-quick --skip-set-charset [...]</div></li></ol></pre></div></div> </div> </div> Thu, 29 Dec 2011 10:58:26 +0000 stefan 211 at https://stefankonarski.de https://stefankonarski.de/content/mysqldump-die-option-opt#comments mysqldump: Backup von MySQL-Datenbanken (MyISAM und InnoDB) https://stefankonarski.de/content/mysqldump <div class="field field-name-taxonomy-vocabulary-3 field-type-taxonomy-term-reference field-label-above"> <div class="field-items"> <div class="field-item even"><a href="/category/tags/services">Dienste</a></div> <div class="field-item odd"><a href="/category/tags/datenbanken">Datenbanken</a></div> <div class="field-item even"><a href="/category/tags/mysql">MySQL</a></div> <div class="field-item odd"><a href="/category/tags/backup">Backup</a></div> </div> </div> <div class="field field-name-body field-type-text-with-summary field-label-hidden"> <div class="field-items"> <div class="field-item even"><p>Mit mysqldump lassen sich Datenbanken schnell sichern, wie folgende Beispiele und Erläuterungen zeigen.</p> <!--break--> <h2>Allgemeiner Aufruf von mysqldump</h2> <p>Mit mysqldump lassen sich alle Datenbanken, einige Datenbanken oder bestimmte Tabellen einer Datenbank sichern. Alle Befehle werden standardmäßig <a href="/content/mysqldump-die-option-opt">mit der Option <tt>opt</tt></a> ausgeführt.</p> <h2 style="margin-top:1em;">Sichern aller Datenbanken mit mysqldump</h2> <p>Dies ist z.B. vor einem MySQL-Upgrade äußerst sinnvoll. Allgemeiner Aufruf:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump [Optionen] --all-databases [Optionen]</div></li></ol></pre></div> <p>Konkretes (sinnvolles) Beispiel zum Sichern aller Datenbanken mit mysqldump:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump -u root -p --all-databases --result-file=dbdata.sql</div></li></ol></pre></div> <p>Zunächst wird man aufgefordert, das Kennwort des Datenbank-Benutzers(!) root anzugeben. Danach wird die Tabellenstruktur und der Inhalt aller Datenbanken d in die Datei dbdata.sql geschrieben.</p> <p><strong>Achtung: Das beinhaltet auch die Datenbank mysql mit allen Benutzerdaten. Beim zurück spielen des Backups wird diese Tabelle komplett überschrieben.</strong></p> <h2 style="margin-top:1em;">Sichern einer oder mehrerer Datenbanken mit mysqldump</h2> <p>Häufig ist es sinnvoll nur bestimmte oder nur eine Datenbank komplett zu sichern. Für den Befehl muss nur eine Option geändert werden. Allgemein:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump [Optionen] --databases [Optionen] DB1 [DB2 DB3...]</div></li></ol></pre></div> <p>Will man den kompletten Inhalt der Datenbank db1 sichern, so bedeutet dies:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump -u root -p --databases db1 --result-file=dbdata.sql</div></li></ol></pre></div> <p>Die Schreibweise</p> mysqldump -u root -p db1 --result-file=dbdata2.sql </code> <p>schreibt auch Daten und Struktur in die Datei dbdata2.sql. Der Unterschied ist, dass mit der Option "--databases" tatsächlich die gesamte Datenbank gesichert wird, während im zweiten Fall nur alle Tabellen der Datenbank db1 in dbdata2.sql geschrieben werden.<br /> In dbdata.sql stehen zwei zusätzliche Zeilen: eine existierende Datenbank db1 würde zunächst gelöscht, dann neu angelegt und zuletzt als aktive Datenbank ausgewählt. In dbdata2.sql stehen nur Befehle zum Anlegen der Tabellen-Struktur. Vorm Zurückspielen des Backups muss eine Datenbank als aktive mit <tt>use db1;</tt> ausgewählt werden.</p> <p>Für den Inhalt mehrerer Datenbanken db1 db2 db3 gilt entsprechend:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump -u root -p --databases db1 db2 db3 --result-file=dbdata.sql</div></li></ol></pre></div> <p>Auch hier gilt, dass beim Ausführen des mysqldump Befehls nach dem Kennwort des Datenbankbenutzers root gefragt wird.</p> <h2 style="margin-top:1em;">Sichern ausgewählter Tabellen einer Datenbank mit mysqldump</h2> <p>Für ein Backup eher nicht zu empfehlen. Sinnvoll mag dies vielleicht zu einem Übertrag von Benutzerdaten dienen. Allgemein lassen sich einzelne Tabellen Datenbanken mit dem folgende mysqldump Befehl sichern:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump [Optionen] DB1 [Tabellen]</div></li></ol></pre></div> <p>Als sinnvolles Beispiel dient die Benutzerdatenbank mysql von MySQL. Die Tabellen db und user sollen gesichert werden:</p> <div class="geshifilter"><pre class="text geshifilter-text" style="font-family:monospace;"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">mysqldump -u root -p mysql db user --result-file=dbdata.sql</div></li></ol></pre></div> <p>Auch hier gilt wieder: Erst Passwortabfrage, dann die Ausführung des mysqldump Befehls.</p> <h2 style="margin-top:1em;">mysqldump: Weiterführende Informationen</h2> <ul> <li><a href="/content/mysqldump-benutzer-und-passwort">Optionen für Benutzer und Passworte</a></li> <li><a href="/content/mysqldump-die-option-opt">Informationen zur Option --opt</a></li> <li><a href="/content/mysqldump-restore-von-myisam-tabellen">Restore von MyISAM Tabellen (Standard)</a></li> <li><a href="/content/mysqldump-restore-von-innodb-tabellen">Restore von InnoDB Tabellen</a></li> <li><a href="/content/mysql-backup-fuer-jede-datenbank-eine-datei">Shell-Script, das jede Datenbank in eine eigene Datei als SQL-Script ablegt</a></li> <li><a href="/content/mysqldump-datenbanken-ignorieren">Shell-Script zur Auswahl von Datenbanken, die gesichert werden sollen</a></p> </ul> <h2 style="margin-top:1em;">mysqldump: myisam und innodb Tabellen</h2> <p>Für die Datensicherung, bzw. das Erstellen eines Backups ist der Tabellentyp (z.B. MyIsam grundsätzlich nicht von Bedeutung.</p> <h2>Timestamps</h2> <p>Hinweis zu älteren Versionen: timestamps werden unter Umständen auf den aktuellen Stand gebracht. Der Einfachheit hatte ich timestamp benutzt, um daraus das Datum der letzten &Auml;nderung f&uuml;r Webseiten zu generieren. Nach einem dump hatten alle das Datum des Dumps.</p> <p>Mit aktuellen MySQL- / mysqldump-Versionen sollte es dieses Problem bicht mehr geben.</p></div> </div> </div> Sun, 30 Aug 2009 15:41:26 +0000 stefan 31 at https://stefankonarski.de https://stefankonarski.de/content/mysqldump#comments