Skip to content

Connect any app

Introduction

In this tutorial, you'll learn how to connect any application to your PlanetScale database with either username and password or the PlanetScale proxy.

Prerequisites

After you install the PlanetScale CLI, sign in with:

Copied
pscale auth login

Connect with username and password

This section will show you how to create a username and password for your branch and use those credentials to connect your database to your application.

There are two ways to generate a new username and password for your branch:

  • In the PlanetScale dashboard
  • With the PlanetScale CLI

This section will cover creating credentials with the CLI, but if you'd prefer to do it in the PlanetScale dashboard, follow the instructions in the Connection Strings documentation.

Follow the instructions below to create a new username and password for your branch.

  1. Run the following command in the CLI to create a new user and password for your branch.
Copied
pscale password create <DATABASE_NAME> <BRANCH_NAME> <PASSWORD_NAME>
Note
The PASSWORD_NAME value represents the name of the username and password being generated. You can have multiple credentials for a branch, so this gives you a way to categorize them. To manage your passwords in the dashboard, go to your database overview page, click "Settings", and then click "Passwords".
  1. Take note of the values it returns to you. You won't be able to see this password again.
Copied
Password production-password was successfully created.
Please save the values below as they will not be shown again
NAME USERNAME ACCESS HOST URL ROLE PLAIN TEXT
--------------------- -------------- ----------------------------------- ------------------ -------------------------------------------------------
production-password jp388djdawnod gxvhbzcxfw01.us-east-2.psdb.cloud Can Read & Write pscale_pw_sVkiE_m1111111111111111
  1. In your application's MySQL configuration file, fill in the following values using the console output from the previous step:

    • host: The value for "ACCESS HOST URL" in the above step
    • port: 3306
    • user: The value for "USERNAME" in the above step
    • password: The value for "PLAIN TEXT" in the above step
    • database: <DATABASE_NAME>
  2. Make sure you correctly configure TLS / SSL for your specific MySQL driver. See Connecting to PlanetScale securely for more information.

Connect using the PlanetScale proxy

Another way to connect your application to your PlanetScale database is using the PlanetScale proxy. You won't have to fiddle with configuring any authentication details, as that's handled by PlanetScale. It's as simple as a single CLI command.

You'll use the CLI to establish a secure connection to PlanetScale. It will listen on a local port that your application can connect to. This gets rid of the need to generate and remember multiple passwords every time you're creating or switching to a new branch.

  1. Run the following command in the CLI:
Copied
pscale connect <DATABASE_NAME> <BRANCH_NAME>

This establishes a secure connection and opens a port on your local machine that you can use to connect to 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 configuration file, 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: <DATABASE_NAME>

Further reading

If you're interested in learning how to secure your application when connecting to PlanetScale, please read Connecting to PlanetScale securely.

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 January 18, 2022
Help us improve this page
PrivacyTerms© 2022 PlanetScale Inc.