> For the complete documentation index, see [llms.txt](https://support.powerio.com/hub/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://support.powerio.com/hub/codesys-hvac/mysql-library/beispiele/erste-verbindung-zur-datenbank-herstellen.md).

# Erste Verbindung zur Datenbank herstellen

### Erste Verbindung zur Datenbank herstellen

#### Voraussetzungen

* MySQL-Server ist installiert und läuft
* Sie kennen die IP-Adresse des Servers
* Sie haben einen MySQL-Benutzernamen und Passwort
* Eine Datenbank wurde bereits erstellt

{% stepper %}
{% step %}

#### Variablen deklarieren

```iecst
VAR
    // Verbindungsparameter
    sHost           : STRING := '192.168.1.100';    // IP-Adresse Ihres MySQL-Servers
    uiPort          : UINT := 3306;                 // Standard MySQL-Port
    sDatabase       : STRING := 'testdb';           // Name Ihrer Datenbank
    sUsername       : STRING := 'root';             // Ihr Benutzername
    sPassword       : STRING := 'MeinPasswort123';  // Ihr Passwort
    
    // Steuerung
    xDoOpen         : BOOL := FALSE;                // Trigger zum Öffnen
    xConnected      : BOOL := FALSE;                // Status: Verbunden
    
    // Funktionsbaustein
    MySQL_ConnString : MySQL_ConnectionString;      // Verbindungsobjekt
    fbMySQL_Open     : MySQL_Open;                  // Funktionsbaustein
    
    // Fehlerbehandlung
    eError          : ERROR;                        // Fehlercode
    sExecuteState   : STRING(200);                  // Statusmeldung
END_VAR
```

{% endstep %}

{% step %}

#### Verbindung öffnen - Code

```
// Verbindung öffnen wenn xDoOpen TRUE ist
IF xDoOpen THEN
    fbMySQL_Open(
        sHost           := '192.168.1.100',         // ÄNDERN: Ihre Server-IP
        uiPort          := 3306,
        sDatabase       := 'testdb',                // ÄNDERN: Ihr Datenbankname
        sUsername       := 'root',                  // ÄNDERN: Ihr Benutzername
        sPassword       := 'MeinPasswort123',       // ÄNDERN: Ihr Passwort
        xStart          := xDoOpen,
        MySQL_Connection:= MySQL_ConnString,
        xConnected      => xConnected,
        eError          => eError,
        sExeute_State   => sExecuteState
    );
END_IF
```

{% endstep %}

{% step %}

#### Verbindung testen

**Im Online-Modus:**

1. Setzen Sie `xDoOpen` auf `TRUE`
2. Überwachen Sie die Variablen:
   * `xConnected` sollte `TRUE` werden
   * `eError` sollte `0` bleiben (kein Fehler)
   * `sExecuteState` zeigt "SUCCESSFULLY CONNECTED WITH DATABASE"

{% endstep %}

{% step %}

#### Status überprüfen

```iecst
// Status ausgeben
IF xConnected THEN
    // Verbindung erfolgreich!
    // Hier können Sie jetzt SQL-Befehle ausführen
ELSIF eError <> ERROR.NO_ERROR THEN
    // Fehler aufgetreten
    // Prüfen Sie eError und sExecuteState für Details
END_IF
```

{% endstep %}
{% endstepper %}

#### Wichtige Werte zum Anpassen

| Parameter | Beispielwert      | Wo finde ich den Wert?                                                      |
| --------- | ----------------- | --------------------------------------------------------------------------- |
| sHost     | '192.168.1.100'   | IP-Adresse Ihres Servers (bei lokalem Server: '127.0.0.1' oder 'localhost') |
| uiPort    | 3306              | Standard-Port, normalerweise nicht ändern                                   |
| sDatabase | 'testdb'          | Name Ihrer Datenbank in phpMyAdmin oder MySQL Workbench                     |
| sUsername | 'root'            | MySQL-Benutzername (Standard: 'root')                                       |
| sPassword | 'MeinPasswort123' | Passwort, das Sie bei der MySQL-Installation festgelegt haben               |

#### Fehlerbehebung

| Fehler     | Ursache                          | Lösung                                                   |
| ---------- | -------------------------------- | -------------------------------------------------------- |
| eError = 1 | Server nicht erreichbar          | IP-Adresse und Netzwerkverbindung prüfen                 |
| eError = 2 | Authentifizierung fehlgeschlagen | Benutzername und Passwort prüfen                         |
| eError = 3 | Timeout                          | Firewall-Einstellungen prüfen, Port 3306 muss offen sein |
| eError = 4 | Datenbank existiert nicht        | Datenbanknamen prüfen, ggf. Datenbank erstellen          |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://support.powerio.com/hub/codesys-hvac/mysql-library/beispiele/erste-verbindung-zur-datenbank-herstellen.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
