Connect to any app

Introduction

In this tutorial, you'll learn how to connect any application to your PlanetScale database.

Prerequisites

  • Install the PlanetScale CLI.
  • Authenticate the CLI with the following command:
pscale auth login

Connect to your Database

You can use the pscale connect command to establish a secure connection to PlanetScale. It will listen on a local port that your application can connect to.

  1. Run the following command in the CLI:
pscale connect your-db-name main

This establishes a secure connection and opens a port on your local machine that you can use to connect any MySQL client.

  1. Take note of the address it returns to you. By default it is 127.0.0.1:3306. The CLI will use a different port if 3306 is unavailable.

  2. In your application's mysql config, use the following to connect:

    • host: 127.0.0.1
    • port: 3306 (the value returned to you by pscale connect)
    • user: root
    • password: leave this blank
    • database: your-db-name

Use Planetscale in production

In production, we recommend using the DATABASE_URL environment variable in your application's configuration for connecting to PlanetScale.

  1. Run the following command in the CLI:
pscale connect your-db-name main --execute "node app.js"

You can use the pscale connect command with the --execute command as a wrapper for your application. This will set the DATABASE_URL environment variable, making it available to your app. The --execute flag should be set to the command that starts up your application.

  1. Feel free to check out the full example node application here.

  2. Find the latest release of pscale CLI for your production environment here.

Authentication in production

You will need to create a service token for use in your production environment.

  1. Create a PlanetScale service token:
pscale service-token create

Take note of the service token's name and value returned to you.

  1. Grant that token access to your database:
pscale service-token add-access your-token-name connect_production_branch --database your-db-name
  1. Set the following environment variables in your production environment:
PLANETSCALE_ORG=your-org-name
PLANETSCALE_SERVICE_TOKEN_NAME=your-token-name
PLANETSCALE_SERVICE_TOKEN=your-token-value

That's it! Now when running the pscale connect command in production, the CLI will use the service token set in your environment variables for authentication.

Summary

In this tutorial, we learned how to use pscale connect to establish a connection to our database, and how to use the --execute flag to set a DATABASE_URL for our application.

What's next?

Now that you've successfully connected your application to PlanetScale, it's time to make schema changes to your tables and deploy your application! Learn more about how PlanetScale allows you to make non-blocking schema changes to your database tables, without locking or causing downtime for production databases.

Need help?

Get help from PlanetScale's support team, or join our GitHub Discussion board to see how others are using PlanetScale.

Was this page useful?
Last updated on June 15, 2021
PrivacyTerms
© 2021 PlanetScale Inc.