> 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/en/mysql-library/beispiele/erste-verbindung-zur-datenbank-herstellen.md).

# Establish first connection to the database

### Establish first connection to the database

#### Prerequisites

* MySQL server is installed and running
* You know the server's IP address
* You have a MySQL username and password
* A database has already been created

{% stepper %}
{% step %}

#### Declare variables

```iecst
VAR
    // Connection parameters
    sHost           : STRING := '192.168.1.100';    // IP address of your MySQL server
    uiPort          : UINT := 3306;                 // Default MySQL port
    sDatabase       : STRING := 'testdb';           // Name of your database
    sUsername       : STRING := 'root';             // Your username
    sPassword       : STRING := 'MeinPasswort123';  // Your password
    
    // Control
    xDoOpen         : BOOL := FALSE;                // Trigger to open
    xConnected      : BOOL := FALSE;                // Status: Connected
    
    // Function block
    MySQL_ConnString : MySQL_ConnectionString;      // Connection object
    fbMySQL_Open     : MySQL_Open;                  // Function block
    
    // Error handling
    eError          : ERROR;                        // Error code
    sExecuteState   : STRING(200);                  // Status message
END_VAR
```

{% endstep %}

{% step %}

#### Open connection - code

```
// Open connection when xDoOpen is TRUE
IF xDoOpen THEN
    fbMySQL_Open(
        sHost           := '192.168.1.100',         // CHANGE: Your server IP
        uiPort          := 3306,
        sDatabase       := 'testdb',                // CHANGE: Your database name
        sUsername       := 'root',                  // CHANGE: Your username
        sPassword       := 'MeinPasswort123',       // CHANGE: Your password
        xStart          := xDoOpen,
        MySQL_Connection:= MySQL_ConnString,
        xConnected      => xConnected,
        eError          => eError,
        sExeute_State   => sExecuteState
    );
END_IF
```

{% endstep %}

{% step %}

#### Test connection

**In online mode:**

1. Set `xDoOpen` to `TRUE`
2. Monitor the variables:
   * `xConnected` should `TRUE` become
   * `eError` should `0` remain (no error)
   * `sExecuteState` shows "SUCCESSFULLY CONNECTED WITH DATABASE"

{% endstep %}

{% step %}

#### Check status

```iecst
// Output status
IF xConnected THEN
    // Connection successful!
    // You can now execute SQL commands here
ELSIF eError <> ERROR.NO_ERROR THEN
    // Error occurred
    // Check eError and sExecuteState for details
END_IF
```

{% endstep %}
{% endstepper %}

#### Important values to adjust

| Parameter | Example value     | Where can I find the value?                                              |
| --------- | ----------------- | ------------------------------------------------------------------------ |
| sHost     | '192.168.1.100'   | IP address of your server (for local server: '127.0.0.1' or 'localhost') |
| uiPort    | 3306              | Default port, usually do not change                                      |
| sDatabase | 'testdb'          | Name of your database in phpMyAdmin or MySQL Workbench                   |
| sUsername | 'root'            | MySQL username (default: 'root')                                         |
| sPassword | 'MeinPasswort123' | Password you set during MySQL installation                               |

#### Troubleshooting

| Error      | Cause                   | Solution                                          |
| ---------- | ----------------------- | ------------------------------------------------- |
| eError = 1 | Server unreachable      | Check IP address and network connection           |
| eError = 2 | Authentication failed   | Check username and password                       |
| eError = 3 | Timeout                 | Check firewall settings, port 3306 must be open   |
| eError = 4 | Database does not exist | Check database name, create database if necessary |


---

# 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/en/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.
