Integrations
Databases
PlanetScale

PlanetScale and Koyeb

PlanetScale (opens in a new tab) is a MySQL-compatible serverless database that combines the reliability of MySQL with the scaling power of Vitess (opens in a new tab). Vitess is a technology originally developed at YouTube to solve their scaling issue and is used today by the likes of Slack (opens in a new tab) and GitHub (opens in a new tab).

Using a PlanetScale database lets you benefit from powerful features like horizontal sharding, non-blocking schema changes, and data branching without having to implement them. Plus, you can deploy your PlanetScale database in multiple regions (opens in a new tab) to keep data near your application servers.

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

The application that connects to the PlanetScale 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 PlanetScale database running and want to quickly preview how to connect your PlanetScale database to an application running on Koyeb, use the Deploy to Koyeb button below.

Deploy to Koyeb (opens in a new tab)

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

Create a PlanetScale serverless MySQL database

To create a PlanetScale MySQL database, sign into your PlanetScale account (opens in a new tab).

On the PlanetScale control panel, click the Create database button. A database creation modal will pop up.

  1. Name your database. For example, planetscale-koyeb.
  2. Pick the Region to run the database in. Here, we put Europe (Frankfurt) as our compute will be deployed in this region on Koyeb.

When you are done configuring your database, click the Create database button. The database will be initialized and provisioned.

Retrieve your PlanetScale connection string

On your PlanetScale database page, click the Connection button. On this dialog, click the New password button to generate a unique username and password pair that you can use to access the main branch of your database. Store this password in a safe place; you will not be able to view it again.

Since we will be using Prisma as an ORM for the sample application that we are deploying in this demo, select Prisma in the "Connect with" dropdown menu.

The connection string will be similar to the following:

DATABASE_URL='mysql://<USER>:<PASSWORD>@us-east.connect.psdb.cloud/<DATABASE>?sslaccept=strict'

Copy the connection string and make sure to save it in a safe place, we will use it when we deploy the application.

Deploy the application on Koyeb

You can deploy the sample application connected to a PlanetScale database on Koyeb using the control panel or via the Koyeb CLI.

To deploy the example application (opens in a new tab) using the control panel (opens in a new tab), follow these steps:

  1. Click Create Web Service on the Overview tab of the Koyeb control panel.
  2. Select GitHub as the deployment option.
  3. Set github.com/koyeb/example-express-prisma as the GitHub repository to deploy.
  4. Expand the Builder section. Click the override toggle associated with the Build command and enter npm run mysql:init in the field.
  5. Expand the Environment variables section. Add a DATABASE_URL environment variable to tell the application how to connect to the PlanetScale database. Give the environment variable the name DATABASE_URL and enter the connection string from earlier as the value.
  6. Name your App and Service, for instance express-planetscale.
  7. Click the Deploy button.

A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. 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"
  }
]

Delete the example application and PlanetScale database

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