In this tutorial, you'll learn how to connect any application to your PlanetScale database.
pscale auth login
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.
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.
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.
In your application's mysql
config, use the following to connect:
3306(the value returned to you by pscale connect)
In production, we recommend using the
DATABASE_URL environment variable in your application's configuration for connecting to PlanetScale.
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.
Feel free to check out the full example node application here.
Find the latest release of
pscale CLI for your production environment here.
You will need to create a service token for use in your production environment.
pscale service-token create
Take note of the service token's name and value returned to you.
pscale service-token add-access your-token-name connect_production_branch --database your-db-name
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.
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.
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.