Question

How can we configure PlanetScale with App Platform?

I would like to use PlanetScale with my App Platform app, how can I configure it to use the proxy that is required?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Please, do NOT follow the answer from Jon Friesen anymore. It was probably written before PlanetScale implement its connection strings feature.

Instead:

  1. Create a password for your database
  2. Add the details of your password as env variables for Laravel (DB_HOST, DB_PASSWORD, DB_USER, DB_DATABASE, DB_USER, DB_PORT is 3306) in DigitalOcean web console You should use the web panel and mark your password as “encrypted”.
  3. Add MYSQL_ATTR_SSL_CA env variable also with the value /etc/ssl/certs/ca-certificates.crt
  4. Promote a branch to be your production branch

You can also find more details here

👋

Great question! Since PlanetScale has a neat client that creates a proxy to the database instance, we will have to install and authenticate that tool so that it can inject a DATABASE_URL environment variable into the app.

  1. Setup a service token using this PlanetScale tutorial
  2. The app component that will be connecting to your PlanetScale database will need to have it’s build command altered to install the client like so:
export PSCALE_VERSION="$(curl https://github.com/planetscale/cli/releases/latest -s -L -I -o /dev/null -w '%{url_effective}' | awk '{n=split($1,A,"/v"); print A[n]}')" && \
curl -sL https://github.com/planetscale/cli/releases/download/v$PSCALE_VERSION/pscale_${PSCALE_VERSION}_linux_amd64.tar.gz | tar -xz -C ./ && \
chmod +x ./pscale
  1. We will also need to wrap the run command in the PlanetScale command for URL injection like so:
./pscale connect app-platform-test main --execute "node app.js"

Don’t forget to set the required PlanetScale environment variables from the tutorial!

PLANETSCALE_ORG=your-org-name
PLANETSCALE_SERVICE_TOKEN_NAME=your-token-name
PLANETSCALE_SERVICE_TOKEN=your-token-value

And that’s it! PlanetScale connections in App Platform! If you want to support PlanetScale with an a dockerfile based app add the install command above prefixed with RUN to your dockerfile, for example:

export PSCALE_VERSION="$(curl https://github.com/planetscale/cli/releases/latest -s -L -I -o /dev/null -w '%{url_effective}' | awk '{n=split($1,A,"/v"); print A[n]}')" && \
curl -sL https://github.com/planetscale/cli/releases/download/v$PSCALE_VERSION/pscale_${PSCALE_VERSION}_linux_amd64.tar.gz | tar -xz -C ./ && \
chmod +x ./pscale

Don’t forget to ensure your run command is wrapped in the pscale connect... command :)

Here’s an example app spec:

name: pscale-express-example
region: nyc
services:
- build_command: |-
    export PSCALE_VERSION="$(curl https://github.com/planetscale/cli/releases/latest -s -L -I -o /dev/null -w '%{url_effective}' | awk '{n=split($1,A,"/v"); print A[n]}')" && \
    curl -sL https://github.com/planetscale/cli/releases/download/v$PSCALE_VERSION/pscale_${PSCALE_VERSION}_linux_amd64.tar.gz | tar -xz -C ./ && \
    chmod +x ./pscale
  environment_slug: node-js
  envs:
  - key: PLANETSCALE_ORG
    scope: RUN_AND_BUILD_TIME
    value: <org>
  - key: PLANETSCALE_SERVICE_TOKEN_NAME
    scope: RUN_AND_BUILD_TIME
    value: <service_token_name>
  - key: PLANETSCALE_SERVICE_TOKEN
    scope: RUN_AND_BUILD_TIME
    value: <service_token>
  github:
    branch: main
    deploy_on_push: true
    repo: planetscale/express-example
  http_port: 8080
  instance_count: 1
  instance_size_slug: basic-xs
  name: pscale-express-example
  routes:
  - path: /
  run_command: ./pscale connect <database_name> main --execute "node app.js"
  source_dir: /