Bash mysqldump -uMyUserName --default-character-set= "UTF8" -pMyPassWord MyTableInDB | gzip > outputfile.sql.gz
Bash mysqldump -uMyUserName --default-character-set= "UTF8" -pMyPassWord MyTableInDB | bzip2 > outputfile.sql.bz2
Bash gunzip < MySQLdump.sql.gz | mysql --default-character-set= "UTF8" -uMyUserName -pMyPassWord MyTableInDB
Bash bunzip2 < MySQLdump.sql.bz2 | mysql --default-character-set= "UTF8" -uMyUserName -pMyPassWord MyTableInDB
Unter debian wird in der Datei /etc/mysql/debian.cnf Zugangsdaten für Wartungs-Aufgaben hinterlegt. Diese kann man nutzten (als root) um eine SQL Shell zu erhalten (z.B. um ein Password eines Users zu ändern):
Bash #!/bin/bash
if [ $( whoami) != 'root' ] ; then
echo "Error: You must start this script with sudo!"
exit
fi
set -x
USERNAME = ` grep user /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}' `
PASSWORD = ` grep password /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}' `
mysql -u${ USERNAME } -p${ PASSWORD }
Ersetzt in der Spalte "introtext"
"/images/alt" nach "/images/neu"
SQL update jos_content set ` introtext ` = replace ( ` introtext ` , '/images/alt/' , 'images/neu/' );
Bash mysqldump -uUsername \
-pPasswort \
-hlocalhost \
Tabellenname \
--compatible= mysql40 \
--default-character-set= "UTF8" \
--lock-tables= false \
--compact > /root/flashchat.sql
Anzeigen:
SQL SELECT * FROM database . table
WHERE DATE_SUB ( CURDATE (), INTERVAL 30 DAY ) >= ` dbfield `
Löschen:
SQL DELETE FROM database . table
WHERE DATE_SUB ( CURDATE (), INTERVAL 30 DAY ) >= ` dbfield `
SQL 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 mysqldump - uUsername - pPasswort - hlocalhost --all-databases \
--compatible=mysql40 \
--default-character-set=utf8 \
--lock-tables=false \
--compact \
--ignore-table=mysql.columns_priv \
--ignore-table=mysql.db \
--ignore-table=mysql.event \
--ignore-table=mysql.func \
--ignore-table=mysql.general_log \
--ignore-table=mysql.help_category \
--ignore-table=mysql.help_keyword \
--ignore-table=mysql.help_relation \
--ignore-table=mysql.help_topic \
--ignore-table=mysql.host \
--ignore-table=mysql.ndb_binlog_index \
--ignore-table=mysql.plugin \
--ignore-table=mysql.proc \
--ignore-table=mysql.procs_priv \
--ignore-table=mysql.servers \
--ignore-table=mysql.slow_log \
--ignore-table=mysql.tables_priv \
--ignore-table=mysql.time_zone \
--ignore-table=mysql.time_zone_leap_second \
--ignore-table=mysql.time_zone_name \
--ignore-table=mysql.time_zone_transition \
--ignore-table=mysql.time_zone_transition_type \
--ignore-table=mysql.user \
> dump_all . sql
Beispiel zeigt die Reparatur einer Plesk "psa" Datenbank.
anzeigen:
Bash mysqlcheck -uadmin -p` cat /etc/psa/.psa.shadow ` psa
reparieren:
Bash mysqlcheck --auto-repair -uadmin -p` cat /etc/psa/.psa.shadow ` psa
Eine Variable in einer weiteren späteren Abfrage weiter zu nutzen. In diesem Fall wird dann anschliessend die abgefragte ID in einer weiteren Abfrage dann gelöscht.
SQL select @ ID : = id , ip
from datenbank . tabelle
where ip = '80.141.217.138' ;
delete from datenbank . tabelle where id = @ ID ;
In diesem Fall werden alle Einträge gesucht wo wie Begriffe "google" und "robots" auftauchen
SQL select id , ip , agent
from datenbank . tabelle
where agent
rlike '(google|robots)'
In diesem Fall werden alle Einträge aufgelistet wo der Begriff "google" oder "spbot" oder "java" auftaucht und zählt deren Vorkommnisse.
Im DB Feld "agent" sind Browser und Bot Agent Strings gespeichert
SQL select id , ip , agent , count ( agent ) AS entrycounts
from datenbank . tabelle
where agent rlike '(google|spbot|java)'
group by agent
ORDER BY entrycounts DESC
Dieses skript muß man per sudo starten, weil die Zugangsdaten zum MySQL Server aus der Datei /etc/mysql/debian.cnf ausgelesen wird. Man kann allerdings auch USERNAME und PASSWORD eines normalen MySQL Benutzers eintragen.
Bash 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #!/bin/bash
if [ $( whoami) != 'root' ] ; then
echo "Error: You must start this script with sudo!"
exit
fi
USERNAME = ` grep user /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}' `
PASSWORD = ` grep password /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}' `
function mysql_query {
echo "____________________________________________________________________"
(
set -x
mysql -u${ USERNAME } -p${ PASSWORD } --execute= "${1}"
)
}
mysql_query "SELECT VERSION();"
mysql_query "SHOW GLOBAL VARIABLES LIKE 'character%';"
mysql_query "SHOW GLOBAL VARIABLES LIKE 'collation%';"
in der Datei "/etc/mysql/my.cnf" folgende Einträge zufügen bzw. ändern.
Unbekannter Typ
[client]
...
default-character-set=utf8
...
[mysqld]
...# Character Settings
default-character-set=utf8
collation_server=utf8_unicode_ci
character_set_server=utf8
skip-character-set-client-handshake
...
SQL ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci ;
ALTER DATABASE tabelle DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
SELECT CONVERT ( _latin1 'Müller' USING utf8 );
Scenario: Ich möchte ohne grosse Umwege ganze Datenbanken oder einzelne Tabellen von einem MySQL Server direkt auf einen anderen kopieren. Dabei werden im Beispiel die Datenbank/Tabelle im SQL Dump Skript gelöscht (DROP Befehle) und dann neu eingespielt.
Das ganze fragt bei der Aktion ein Kennwort ab. Dies läßt sich für eine höhere Automatisierung auf Zertifikatsbasis umstellen.
Bash 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #!/bin/bash
LOCAL_DB_USER = "root"
LOCAL_DB_PASS = "******"
LOCAL_DB_DATABASE = "MyDatabase"
LOCAL_DB_TABLE = "MyTablename" # if set only the table will be copied / if not than database
REMOTE_USER = "root"
REMOTE_HOST = "myDestinationServer.de"
REMOTE_DB_USER = "admin"
REMOTE_DB_PASS = "******"
# based on following work task:
# mysqldump myDatabase [myTable] | ssh root@myDestinationServer.de mysql [myDatabase]
# this works directly through Linux pipe
#
mysqldump -u${ LOCAL_DB_USER } -p${ LOCAL_DB_PASS } \
--compatible= mysql4 \
--default-character-set= latin1 \
--lock-tables= false \
${ LOCAL_DB_DATABASE } ${ LOCAL_DB_TABLE } \
| ssh -C -o CompressionLevel = 9 ${ REMOTE_USER } @${ REMOTE_HOST } \
mysql -u${ REMOTE_DB_USER } -p${ REMOTE_DB_PASS } ${ LOCAL_DB_DATABASE }