Mysql: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
Zeile 37: Zeile 37:
</pre>
</pre>
* TIMESTAMP ist normalerweise ohne NULL, Default-Wert ist NOW()
* TIMESTAMP ist normalerweise ohne NULL, Default-Wert ist NOW()
== Index erstellen =
<pre>create index geotestdate on testresult (`geo_bundesland`,`test_immunitaet`,`sample_date`);
</pre>
== Table-Definition abfragen ==
<pre>show create table geo;
</pre>


== DB-Export ==
== DB-Export ==

Version vom 19. August 2020, 07:42 Uhr


Debian Anpassungen

Debian prüft die Tabellen beim Booten. Damit das funktioniert, muss ein passender User eingerichtet sein:

  • Siehe /etc/mysql/debian.cnf
mysql -u root -p mysql
grant all on *.* to 'debian-sys-maint'@'localhost' identified by "6QLw8BSKFwzDnoUT";
grant all on mydb.* to 'dbadmin'@'192.168.2.%' identified by '6QLw8BSKFwzDnoUT';
grant SELECT on *.* to 'backup'@'localhost' identified by 'SAVE:alls!4711';
flush privileges;

MariaDB: Es ist für root ein Passwort vergeben. Man kann sich aber als OS-User root ohne Passwort anmelden.

Nachträglich Root-Passwort setzen

Wenn bei der Installation das Passwort nicht gesetzt wurde, kann das nachträglich passieren:

update user set 
authentication_string=password('ge:heimesp_w1'),
plugin='mysql_native_password'
where user='root';

Zugriff ohne Root-Passwort (als Benutzer root):

update user set 
authentication_string='',
plugin='auth_socket'
where user='root';

Datentypen

create table example(
   id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
   ts TIMESTAMP NULL default NULL,
   dt DATETIME,
   name VARCHAR(100) UNIQUE NOT NULL
);
  • TIMESTAMP ist normalerweise ohne NULL, Default-Wert ist NOW()

= Index erstellen

create index geotestdate on testresult (`geo_bundesland`,`test_immunitaet`,`sample_date`);

Table-Definition abfragen

show create table geo;

DB-Export

mysqldump -u $USR $PWPHRASE $DB >$OUT
# oder nur Tabelle users  mit Bedingung:
mysqldump "--where=name like 'a%'" -u $USR $PWPHRASE $DB users logs >$OUT

CSV-Import

  • Wichtig: CSV-Datei muss wie Tabelle heißen, also geo.csv
mysqlimport --ignore-lines=1 --fields-terminated-by=, --local \
  --columns=geo_id,geo_staat,geo_land,geo_bezirk,geo_kreis,geo_gemeindeags,geo_gemeinde,geo_plz \
  -u $USR $PWPHRASE $DB  geo.csv
LOAD DATA
 LOCAL INFILE '/tmp/data.csv'
 INTO TABLE geo
 COLUMNS TERMINATED BY '\t'
 IGNORE 1 LINES
 (geo_id,geo_staat,...)

CSV-Export

SELECT * FROM users
INTO OUTFILE '/tmp/users.csv' 
FIELDS ENCLOSED BY '"' 
TERMINATED BY ';' 
ESCAPED BY '"' 
LINES TERMINATED BY '\r\n';