Zum Inhalt

MariaDB einrichten

Nach der Paketinstallation richtest Du MariaDB und das Datenbankkonto für Nextcloud ein. Dialoge, Abfragen und Beispielausgaben bleiben zur Nachvollziehbarkeit erhalten. Die beschriebenen Kennwörter sind Übungswerte und müssen für eigene Systeme ersetzt werden.

MariaDB vorbereiten

Interaktive Einrichtung

root@nc:~# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): <Enter Taste>
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] <n für nein>      !(Erhöht die Sicherhit der Root Accounts)!
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] <Y>
New password: <SQL#RootPasswort>
Re-enter new password: <SQL#RootPasswort>
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] <Y>
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] <Y>
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] <Y>
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] <Y>
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
root@nc:~#

Manuelle Einrichtung mit SQL-Befehlen

-- 1. Root-Authentifizierung auf das moderne Plugin umstellen (und optional ein Passwort setzen, falls gewünscht)
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password;

-- 2. Anonyme Benutzerkonten löschen
DELETE FROM mysql.user WHERE User = '';

-- 3. Fernzugriff für den Root-Benutzer verbieten
DELETE FROM mysql.user WHERE User = 'root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');

-- 4. Die unsichere Test-Datenbank entfernen
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db = 'test' OR Db = 'test_%';

-- 5. Berechtigungen sofort neu einlesen lassen
FLUSH PRIVILEGES;

Einstellungen prüfen

mariadb -u root

-- 1. Aktive Benutzer und erlaubte Hosts anzeigen
SELECT User, Host, plugin FROM mysql.user;

-- 2. Prüfen, ob die Testdatenbank noch existiert:

SHOW DATABASES LIKE 'test';
exit;

-- 3. MariaDB-Konfigurationsdateien auf dem Server ansehen
cat /etc/mysql/mariadb.conf.d/50-server.cnf

Nextcloud-Datenbank anlegen

root@nc:~# mysql -u root
Enter password:

MariaDB [(none)]> CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> CREATE USER 'nc_user'@'localhost' IDENTIFIED BY 'nc_pass';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON nextcloud.* TO 'nc_user'@'localhost';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> exit;
Bye
root@nc:~# :

Datenbankzugriff prüfen

root@nc:~# mysql -u nc_user -p
Enter password:

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| nextcloud          |
+--------------------+
2 rows in set (0.001 sec)

MariaDB [(none)]> exit;

Fehlersuche und Zurücksetzen der Übungsdatenbank

Der folgende Ablauf löscht die bestehende Nextcloud-Datenbank und legt sie neu an. Er ist ausschließlich zum Neustart einer entbehrlichen Übungsinstallation gedacht, nicht zur Reparatur einer bereits genutzten Instanz.

mysql -u root
-- 1. Vorhandene Datenbanken auflisten
SHOW DATABASES;

-- 2. Vorhandene Benutzer auflisten:
SELECT User, Host FROM mysql.user;

-- 3. Vorherigen Versuch komplett löschen
DROP DATABASE IF EXISTS nextcloud;
DROP USER IF EXISTS 'nc_user'@'localhost';

-- 4. Alles frisch und sauber neu anlegen
CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'nc_user'@'localhost' IDENTIFIED BY 'nc_pass';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nc_user'@'localhost';
FLUSH PRIVILEGES;