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:
- A Koyeb account (opens in a new tab) to deploy the application. You can optionally install the Koyeb CLI to deploy the application from the command line.
- A PlanetScale account (opens in a new tab) to deploy the database.
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.
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.
- Name your database. For example,
planetscale-koyeb
. - 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:
- Click Create Web Service on the Overview tab of the Koyeb control panel.
- Select GitHub as the deployment option.
- Set
github.com/koyeb/example-express-prisma
as the GitHub repository to deploy. - Expand the Builder section. Click the override toggle associated with the Build command and enter
npm run mysql:init
in the field. - 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 nameDATABASE_URL
and enter the connection string from earlier as the value. - Name your App and Service, for instance
express-planetscale
. - 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:
- From the PlanetScale dashboard (opens in a new tab), click the Settings tab and click the Delete database button. Follow the instructions.
- From the Koyeb control panel (opens in a new tab), select the App to delete. Under the Settings tab, click the Delete button and follow the instructions.