Aiven MySQL and Koyeb

Integrate a managed Aiven MySQL database to your application and benefit from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network with zero configuration.

Aiven for MySQL allows you to get a fully managed MySQL database deployed in the cloud of your choice in a few minutes. Aiven takes care of everything for you including installing security patches, upgrades, automatic backups, and allows you to scale up or down your MySQL databases as you need.

This guide explains how to connect an Aiven managed MySQL database to an application running on Koyeb. To successfully follow this documentation, you will need to have:

  • A Koyeb account to deploy the application. You can optionally install the Koyeb CLI to deploy the application from the command line
  • An Aiven account to deploy the Aiven MySQL database.

The application that connects to the Aiven managed MySQL database uses Prisma as an ORM. Prisma is an open-source ORM. We use it to synchronize our database schema with our Prisma schema.

If you already have a freshly created Aiven managed MySQL database running and want to quickly preview how to connect your database to an application running on Koyeb, use the Deploy to Koyeb button below.

Deploy to Koyeb

Make sure to replace the DATABASE_URL environment variable with the connection string of your Aiven database.

Create an Aiven MySQL database

To create an Aiven managed MySQL database, sign into your Aiven account.

On the Aiven control panel, click the Create Service button to provision a database and access the service creation page.

  1. Select the service you want to create, in our case, MySQL
  2. Choose the Cloud Provider your Aiven database will run. You can choose between AWS, GCP, Azure, DigitalOcean, and UpCloud. For this guide, we use Google Cloud
  3. Pick the Region to run the database in. Here, we selected google-europe-west3 (Frankfurt) as our compute will be deployed in this region on Koyeb
  4. Select the service Plan. Different plans are available depending on what resources and features you need. For this guide, we use the Hobbyist plan
  5. Give your service a name, e.g. aiven-koyeb.

When you are done configuring your database, click the Create service button. Your database will be provisioned.

Retrieve your Aiven MySQL connection string

From your Aiven MySQL service page, click the Copy button icon on the right side of the Service URI field. The connection string should be similar to the following:

mysql://<user>:<password>@<host>:<port>/defaultdb?ssl-mode=REQUIRED

Save it in a safe place, we will need it in the next step to connect our application to the database.

Deploy the application on Koyeb

You can deploy on Koyeb using the control panel or via the Koyeb CLI.

Via the Koyeb control panel

To deploy the example application using the control panel, follow these steps:

  1. Create a new Koyeb App.
  2. Setgithub.com/koyeb/example-express-prisma as the GitHub repository to deploy and keep the default branch as main
  3. Setnpm run mysql:init as the build command Build command
  4. Add a DATABASE_URL environment variable to indicate the application how to connect to the Aiven MySQL database. Give the environment variable a name, DATABASE_URL and enter the connection string from earlier as the value
  5. Name your service, for instance express-aiven-mysql
  6. Click the Create service button

Koyeb is now building the application. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.

The example application exposes a /planets endpoint that you can use to list planets from the database. Once your deployment is live, you should see the following results when navigating to https://<YOUR_APP_URL>.koyeb.app/planets:

[ { "id": 1, "name": "Mercury" }, { "id": 2, "name": "Venus" }, { "id": 3, "name": "Mars" } ]

Via the Koyeb CLI

To deploy the example application using the Koyeb CLI, run the following command in your terminal:

koyeb app init express-aiven-mysql \ --git github.com/koyeb/example-express-prisma \ --git-branch main \ --git-build-command "npm run mysql:init" \ --ports 8080:http \ --routes /:8080 \ --env PORT=8080 \ --env DATABASE_URL="mysql://<user>:<password><host>:<port>/defaultdb?ssl-mode=REQUIRED"

Make sure to replace user, password, and port with your own.

Access deployment logs

To track the app deployment and visualize build logs, execute the following command:

koyeb service logs express-aiven-mysql/express-aiven-mysql -t build

Access your app

Once the deployment of your application has finished, you can retrieve the public domain to access your application by running the following command:

$ koyeb app get express-aiven-mysql ID NAME DOMAINS CREATED AT 1a074269 express-aiven-mysql ["express-aiven-mysql-ed.koyeb.app"] 02 Dec 22 15:06 UTC

The example application exposes a /planets endpoint that you can use to list planets from the database. Once your deployment is live, you should see the following results when navigating to https://<YOUR_APP_URL>.koyeb.app/planets:

[ { "id": 1, "name": "Mercury" }, { "id": 2, "name": "Venus" }, { "id": 3, "name": "Mars" } ]

Delete the example application and Aiven MySQL database

To delete the example application and the Aiven MySQL database and avoid incurring any charges, follow these steps:

  • From the Aiven MySQL dashboard, click on the service you created and click Delete service. Follow the instructions.
  • From the Koyeb control panel, select the App to delete. Under the "Settings" tab, click the "Delete" button and follow the instructions.