PlanetScale and Koyeb

Integrate a serverless PlanetScale 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.

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

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 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

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.

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.

Via the Koyeb control panel

To deploy the 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 main
  3. Set npm run mysql:init as the Build command
  4. Add a DATABASE_URL environment variable to indicate the application how to connect to the PlanetScale 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-planetscale
  6. Click the Deploy 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-planetscale \ --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>@us-east.connect.psdb.cloud/<database>?sslaccept=strict"

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

Access deployment logs

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

koyeb service logs express-planetscale/express-planetscale -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-planetscale ID NAME DOMAINS CREATED AT ec6a4311 express-planetscale ["express-planetscale-myorg.koyeb.app"] 10 Mar 23 10:12 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 PlanetScale database

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

  • From the PlanetScale dashboard, click the Settings tab and click the Delete database button. 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.