# Mysql database connection

**URL:** <https://community.info-beamer.com/t/mysql-database-connection/1223>\
**Category:** Suggestions\
**Created:** [June 12, 2023, 5:06am UTC](https://community.info-beamer.com/t/mysql-database-connection/1223 "2023-06-12T05:06:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smolgra](https://community.info-beamer.com/letter_avatar_proxy/v3/letter/s/f19dbf/32.png) [@smolgra](https://community.info-beamer.com/u/smolgra)\
**Post date:** [June 12, 2023, 5:06am UTC](https://community.info-beamer.com/t/mysql-database-connection/1223/1 "2023-06-12T05:06:04Z")

</div>

Hi,

I know that in lua it is possible to implement a tcp/ip based Mysql client by loading a socket module and then creating the tcp/ip connection.

I need to get the values ​​from a table in Mysql database to display it on screen.  
Is this approach possible on info-beamer hosted , or will there be any restrictions?

Thanks!

---

<div class="post-metadata">

**Author:** ![infobeamer-fw](https://community.info-beamer.com/user_avatar/community.info-beamer.com/infobeamer-fw/32/3_2.png) [@infobeamer-fw](https://community.info-beamer.com/u/infobeamer-fw)\
**Post date:** [June 12, 2023, 7:08am UTC](https://community.info-beamer.com/t/mysql-database-connection/1223/2 "2023-06-12T07:08:08Z")

</div>

The info-beamer process embedding the Lua scripting abilities isn’t intended to make external connections. It’s not possible to load external modules or make connections to other servers from within Lua. The intended way to implement that is by adding a [package service](https://info-beamer.com/doc/package-services) alongside your visual Lua code.

The way this works is that you add a script or binary file named `service` to your package. When such a package is then assigned to the device, the service file is marked executable and ensured to run in the background. You can then do all kinds of background processing there and for example generate a JSON file with the data needed for the display logic.

In your Lua code you can use:

```auto
local title = "<loading..>"
node.json_watch("data.json", function(data)
     title = data.title
end)

```

This will instruct info-beamer to watch for any changes to the file `data.json`. The callback will be called and you can immediately update your internal state based on the values within that JSON file. In the example the value in the `title` variable changes if you provide a new JSON content like `{"title": "Breaking news: Something!"}`.

As for the package service: If possible, I would recommend using [golang](https://go.dev/) to implement them as it’s almost trivial to cross-compile such a service from your developer machine and the resulting single static binary, which you can then name `service` makes it very easy to deploy.

Minimal example again:

```auto
// this is the file main.go
package main

import (
        "encoding/json"
        "fmt"
        "io/ioutil"
)

func main() {
        data := map[string]string{
                "title": "Something",
        }

        jsonData, err := json.MarshalIndent(data, "", " ")
        if err != nil {
                fmt.Println("Error marshaling JSON:", err)
                return
        }

        err = ioutil.WriteFile("data.json", jsonData, 0644)
        if err != nil {
                fmt.Println("Error writing JSON file:", err)
                return
        }
}

```

Cross compile to arm using:

```auto
GOOS=linux GOARCH=arm go build -ldflags="-s -w" -o service main.go

```

The resulting `service` file can be directly added to the package and will create a `data.json` file read by the Lua code above. Using mysql from Golang shouldn’t be too difficult and the deployment flow is identical: Build that static binary and include it in the info-beamer package. Hope that helps?

---

<div class="post-metadata">

**Author:** ![smolgra](https://community.info-beamer.com/letter_avatar_proxy/v3/letter/s/f19dbf/32.png) [@smolgra](https://community.info-beamer.com/u/smolgra)\
**Post date:** [June 12, 2023, 3:11pm UTC](https://community.info-beamer.com/t/mysql-database-connection/1223/3 "2023-06-12T15:11:13Z")

</div>

Hi,  
Thanks for the detailed answer, it helps me a lot. It became clear to me how to get data from mysql by creating the binary file, which from what you tell me is quite simple with golang. Thanks for the guide on how to do it.
