MySQL/Startup Guide/de

Dieses Dokument hilft die MySQL Datenbank einzurichten und zu benutzen.

Hintergrund
MySQL ist ein populärer Datenbankserver der in verschiedensten Programmen benutzt wird. SQL steht im Englischen für (S)tructured (Q)uery (L)anguage, auf Deutsch in etwa Strukturierte Abfrage Sprache. Dieses SQL wird für die Kommunikation zwischen Server und anderen Programmen benutzt. Zusätzlich hat MySQL eigene Erweiterungen der Standard SQL-Funktionen um extra Deatures bereit zu stellen. In diesem Dokument wird die minimale MySQL Installation beschrieben, sowie die Einrichtung von Datenbasen, Tabellen und neuen Benutzern. Starten wir mit der Installation.

MySQL Installation
Zuerst ist das Programmpaket MySQL auf dem Rechner zu installieren. Für spezielle Features sind die entsprechenden USE-Flags zu setzen.

Am Ende der Installation wird folgende Nachricht angezeigt:

Da dies eine neue Installation ist, führen wir den Befehl aus. Sie müssen bei der Konfiguration der MySQL-Datenbank  drücken, wenn Sie dazu aufgefordert werden. Die Konfiguration richtet die Haupt-MySQL-Datenbank ein, die administrative Informationen wie Datenbanken, Tabellen, Benutzer, Berechtigungen und vieles mehr enthält. Die Konfiguration empfiehlt, dass Sie Ihr Root-Passwort so schnell wie möglich ändern. Wir werden das definitiv tun, sonst könnte jemand zufällig kommen und unseren Standard-Setup MySQL Server hacken.

Einige MySQL spezifische Informationen, die nicht den EBuild betreffen, wurden von hier entfertn, um dieses Dokument so konsistent wie möglich zu halten.

Das config-Skript hat bereits die Befehle ausgedruckt, die wir ausführen müssen, um unser Passwort einzurichten.

Wenn Sie OpenRC verwenden, geben Sie diesen Befehl ein:

Bei der Benutzung von systemd kommt das folgende Kommando zur Anwendung:

Danach ist das root Passwort zu setzen:

Sie können nun testen, dass Ihr Root-Passwort erfolgreich konfiguriert wurde, indem Sie versuchen, sich bei Ihrem MySQL-Server anzumelden:

Die Option  gibt den Datenbank-Benutzer an, die Option   spezifiert den Host. Wird kein Server angegeben wird  verwendet. Mit der Option  wird die Passwortabfrage aktiviert. Nun erscheint die  Eingabeaufforderung. Hier werden die Kommandos an den  Server abgesetzt. Da wir nun als Root am Datenbankserver angemeldet sind, können wir mit dem setup des Servers beginnen.

Erstellen einer Datenbank
Wir sind eingeloggt und die mysql Eingabeaufforderung wird angezeigt. Zuerst lassen wir uns die aktuell vorhanden Datenbasen anzeigen. Dafür benutzen wir den  Befehl.

Obwohl schon eine Test-Datenbank eingerichtet ist, wollen wir troztdem unsere eigene Datenbank erstellen. Datenbanken werden mit dem Befehl  erzeugt. Unsere Datenbank soll den Namen "gentoo" erhalten.

Aus der Antwort des Datenbank Servers ist ersichtlich, dass der Befehl ohne Fehler ausgeführt wurde. In unserem Fall wurde 1 Zeile verändert. Die ist ein Verweis auf die Hauptdatenbank, welche eine Liste der vorhandenen Datenbanken vorhält. Sie müssen sich nicht zu viele Gedanken über die Hintergrunddetails machen. Die letzte Zahl ist, wie schnell die Abfrage ausgeführt wurde. Wir können überprüfen, ob die Datenbank erstellt wurde, indem Sie den Befehl  erneut ausführen.

In der Tat wurde unsere Datenbank erstellt. Um mit dem Erstellen von Tabellen für unsere neue gentoo Datenbank arbeiten zu können, müssen wir sie als unsere aktuelle Datenbank auswählen. Dazu verwenden wir den Befehl. Der Befehl  übernimmt den Namen der Datenbank, die Sie als aktuelle Datenbank verwenden möchten. Eine weitere Möglichkeit besteht darin, sie nach dem Schalter  auf die Befehlszeile zu setzen. Gehen wir weiter und wechseln in die gentoo Datenbank.

Die aktuelle Datenbank ist nun unsere erzeugte Gentoo Datenbank und wir werden nun einige Tabellen erstellen und diese mit Daten füllen.

Erzeugen einer Tabelle
Die Gliederung von MySQL ist wie folgt aufgebaut: Datenbanken, Tabellen, Records und Felder. Datenbanken beinhalten Tabellen, Tabellen halten Records zusammen, Records sind aus Felder zusammen gesetzt. Die Felder enthalten die akutellen Informationen. Diese Gliederung lässt den Benutzer die Wahl wie auf die Informationen zugegriffen werden können. Bis hierher wurden nur Datenbanken benutzt, nun sind Tabellen an der Reihe. Eine Auflistung der Tabellen erhält man mit. Noch gibt es keine Tabellen in unserer Datenbank, wie die Ausgabe des Kommandos zeigen wird:

Das bedeutet, dass wir einige Tabellen erstellen müssen. Dazu verwenden wir den Befehl. Dieser Befehl unterscheidet sich jedoch deutlich von dem einfachen Befehl. Dieser Befehl nimmt eine Liste von Argumenten entgegen. Das Formular ist wie folgt:

table_name is the name of the table we wish to create. In this case, let's make a table named. This table will contain the developer's name, email and job.

field_name will contain the name of the field. We have 3 required names in this case: name, email, and job.

The field_data_type is what type of information will be stored. The different formats available can be found at the MySQL Column Types Page. For our purposes, we'll use the  data type for all of our fields. is one of the simplest of data types when it comes to working with strings.

size is how much of data a single field will store. In this case, we'll use 128. This means that the field can have  data that is 128 bytes. You can safely think of this as 128 characters for the time being, though there is a somewhat more technical explanation that the above site will provide you with. Now that we know how we are going to create the table, let's do it.

Looks like our table was created ok. Let's check it with the  command:

Yes, there's our table. However, it doesn't seem to have any information on the types of fields we setup. For that, we use the  command (or   for short), which takes the name of the table as its argument. Let's see what that gives us for our developers table.

This shows the different fields and their types. It also shows a few extra attributes that are beyond the scope of this howto. Feel free to refer to the MySQL Reference Manual for more information. We now have a table to work with. Now let's go ahead and populate it.

Populating Our MySQL Database
We populate a table (or add data) using the  command. Like the  command, it also has a specific format:

This command is used to insert a record into table. table contains the MySQL table we wish to enter the information into. The table name may be followed by the list of columns to insert data into and  contains the values you wish to insert into the table. You may omit the list of columns if you insert a value into each one and if you write the values in the same order the columns have been defined. In this case, we want to insert data into the developers table. Let's insert sample records:

According to our return result, it appears that the record was inserted correctly. What if we want to input more information than just one record? That's where the  command comes into play. This loads records from a tab separated file. Let's try that by editing a file in our home directory with the records. We'll call this file. Here's a sample:

Now the  command has a somewhat elongated definition, but we'll use the simplest form here.

is the directory and filename that will be used. table is the name of our table. In this case, our file is and the table is developers.

That worked well. However, this simply inserts records, and does not give you any sort of control over MySQL. Many web applications use sql scripts in order to setup MySQL quickly and easily. If you want to use an sql script, you'll need to run mysql in batch mode, or source the file. Here's an example of running mysql in batch mode:

Like , be sure you can tell what  does. Failure to do so may cause your database to be compromised! Another way you can accomplish this is to use the  command. This command will run mysql commands from an sql file while in the mysql interactive mode. Here is how to source an sql file:

If you see a web application wanting you to run an sql file, the two above commands can be used to accomplish that task. We have our table setup, so how do we check our fields? We do this by searching our table with queries.

Browsing MySQL Tables With Queries
Queries are one of the main features of any SQL database. They help us turn data in our tables into something useful. Most queries are done with the  command. The  command is fairly complex, and we're only going to look at three main forms of the command in this document.

Let's take a quick look at the first form. It's relatively simple and gives you an overall look of your table. We'll go ahead and run it to see what data we have so far.

We see both the data we inserted through  and those inserted by   present. Now, let's say that we just want to see the record for Chris White. We can do so with the second form of select as shown below.

As expected, the specific entry that we were looking for has been selected. Now, let's say we only wanted to know the person's job and email address, not their name. We can do so with the third form of  as shown here.

This method of selection is a lot easier to manage, expecially with larger amounts of information, as we'll see later on. Right now, being the root mysql user, we have unlimited permissions to do what we wish with the MySQL database. In a server environment, a user with such privileges can be quite problematic. In order to control who does what with the databases, we setup privileges.

Granting Privileges with GRANT
Privileges are what kind of access users have to databases, tables, pretty much anything. Right now in the gentoo database, the MySQL root account is the only account that can access it, given its permissions. Now, let's create two somewhat generic users, guest and admin, who will access the gentoo database and work with the information in it. The guest account will be a restricted one. All it will be able to do is get information from the database, and that's it. admin will have the same control as root, but only for the gentoo database (not the main mysql databases). Before we start on that, let's have a closer look at this somewhat simplified format of the  command.

First we have the privileges we wish to assign. With what we've learned so far, here are some of the privileges you can set:


 * - Gives the all privilege control for the database
 * - Allows users to create tables
 * - Allows users to query tables
 * - Allows users to insert data into a table
 * - Allows users to see a list of databases
 * - User has no privileges
 * - Allows users to grant privileges

For our admin user, ALL will do. For the guest user,  will be sufficient for read only access. database is the database we wish the user to have these permissions on. In this example, gentoo is the database. The .* means all tables. If you wanted to, you could apply per table access. user is the name of the user and host is the hostname the user will be accessing from. In most cases, this will be localhost. Finally, password is the user's password. Given the information, let's go ahead and create our users.

Now that we have the users setup, let's test them out. First we quit mysql by typing  at the command prompt:

We're now back at the console. Now that we have our users setup, let's go ahead and see what they can do.

Testing User Permissions
We shall now attempt to login as the guest user. Currently, the guest user has  only privileges. This basically comes down to being able to search and nothing more. Go ahead and login with the guest account.

Now we should test the user restriction(s). Let's switch to the gentoo database:

Now let's try to do something we are not supposed to. We'll attempt to create a table.

As you can see, this function fails, as our user does not have the appropriate access. However, one access we did grant is the  statement. Let's go ahead and try that:

The command succeeds, and we're given a glimpse of what user permissions can do. We did, however, create an admin account as well. This was created to show that even all permissions granted users can still have limitations. Go ahead and quit MySQL and login as the admin.

To begin, we'll try creating a new database with our admin user. This admin user will have access similiar to the root MySQL account, and will be able to do any kind of modification to the gentoo database it chooses. This will test the user's access to the main MySQL database. Remember ealier that we only set permissions to a specific database.

Indeed, the admin user cannot create databases on the main MySQL database, despite all his permissions on the gentoo database. However, we're still able to use the admin account to modify the gentoo database, as shown here by this example data insertion.

The admin user can access the database as they please. Now sometimes, we need to get rid of user permissions. This could be anything from a problematic user to a retired employee. Let's take a look at how to disable user permissions with the  command.

Removing User Access With The REVOKE Command
The  command lets us deny access to a user. We can either deny full access, or specific access. In fact, the format is very similiar to.

Options here are explained in the  command section. In this section however, we're going to deny full access to a user. Let's say we find out the guest account is causing some problems security wise. We decide to revoke all privileges. We login as root and do the needful.

Now let's quit and attempt to login as a guest user.

Although we're able to login, our access to gentoo is now gone.

And our problematic user is no longer able to access the gentoo database. Please note that the user was still able to login. That is because they remain in the main MySQL database. Let's take a look at how to completely remove an account with  and the MySQL user table.

Removing Accounts Using DELETE
The MySQL user table is a listing of all users and information about them. Make sure you're logged in as root. Then go ahead and use the main MySQL database.

Now let's see what tables the mysql database has:

The user table is the table we're after. However, the user table contains 30 different fields, making it very hard to read. In order to make things easier to read, we'll go ahead and use the third version of the  statement. The fields we're after are Host and User.

Now that we have our information, we can get rid of the guest user. This is done with the  command and the syntax is shown below.

You may notice that  is somewhat similiar to the   statement in its format. In this case, the field will be User, and the value guest. This will delete the record in the user table where the user is guest, successfully deleting our guest user account. Let's go ahead and do that:

It seems to have worked ok. Let's test by logging out and attempting to login as our guest user.

Our user is now successfully deleted!

Conclusion
While this guide focuses mainly on setting up MySQL on the command line, a few alternatives are available in GUI form:


 * phpMyAdmin - Popular php based MySQL administration tool.
 * mysqlnavigator - QT frontend to MySQL.
 * gmyclient - A GNOME based MySQL client.
 * knoda - A KDE MySQL client.

This ends the MySQL introductory tutorial. I hope this gives you a better understanding of the basics behind MySQL and getting a database set up.