Inhaltsverzeichnis
mysql
mysql ist eine Datenbank.
Nützliche Befehle
Shell
Ausführen von mysql-Befehlen in der Shell:
mysql -e "SHOW TABLES;" -u root -p gogs
DB sichern & zurückspielen
Mit mysqldump sichern.
mysqldump -u DB-USER -pPASSWD --default-character-set=latin1 DB -c | gzip -9 > backup.sql.gz mysql -u DB_USER -pPASSWD DB < backup.sql.gz
Datenbanken
Datenbanken anzeigen
show DATABASES;
Datenbank auswählen
use MEINE_DB;
Tabellen anzeigen
Vorher DB auswählen (use MeineDB;)
show tables;
Tabelle(n) löschen
DROP TABLE MeineTabelle; DROP TABLE (Meinetabelle1, MeineTabelle2);
Tabellen mit bestimmten Prefix löschen (quick & dirty):
show tables like 'prefix_%';
copy the results and paste them into a text editor or output the query to a file, use a few search and replaces to remove unwanted formatting and replace \n with a comma put a ; on the end and add drop table to the front.
you'll get something that looks like this:
drop table myprefix_1, myprefix_2, myprefix_3;
Alle Tabellenspalten anzeigen
select * from pkme_formmaker_form;
Bestimmte Tabellenspalten anzeigen
select slug,data from pkme_formmaker_form;
Bearbeiten
update pkme_formmaker_form set data='{"classSfx":"","user_email_field":false"}' where slug='kontakt';
Tabellen-Reihen löschen
DELETE FROM ma_job WHERE job_id IN (620981, 620982, 620984, 620985, 620986)
Bestimmte Anzahl (ungeprüft!):
DELETE FROM ma_job limit 5;
Mit bestimmten Inhalten:
DELETE FROM orders WHERE id_users = 1 AND id_product = 2 LIMIT 1
DB erstellen:
CREATE DATABASE menagerie;
DB löschen:
DROP DATABASE menagerie;
Falls im Namen der Datenbank ein Bindestrich ist, kommt es zu einer Fehlermeldung:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-db' at line 1
Dann hilft es die DB in `` zu setzen, z.B.: `meineDB-db`
Ausgabe in CVS-datei schreiben
SELECT spalte1,spalte2,spalte3 FROM tabelle INTO OUTFILE '/tmp/mein.csv';
Benutzer
User anzeigen
SELECT User FROM mysql.user;
Mehr Info:
select host, user, password from mysql.user;
Userberechtigungen anzeigen
SHOW GRANTS FOR user@localhost;
User erstellen und Rechte für für Datenbank gewähren
(im Beispiel alle Rechte für alle DB):
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost'; FLUSH PRIVILEGES;
Nur bestimmt Privilegien:
GRANT [type of permission] ON [database name].[table name] TO ‘[username]'@'localhost';
Arten von Privilegien:
- ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to a designated database (or if no database is selected, across the system)
- CREATE- allows them to create new tables or databases
- DROP- allows them to them to delete tables or databases
- DELETE- allows them to delete rows from tables
- INSERT- allows them to insert rows into tables
- SELECT- allows them to use the Select command to read through databases
- UPDATE- allow them to update table rows
Privilegien nehmen:
REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]'@‘localhost';
Benutzer löschen:
DROP USER 'demo'@'localhost';
Passwort des Benutzx ändern
ALTER USER 'MyUser'@'localhost' IDENTIFIED BY 'NewPassword';
