These examples demonstrate how to connect a Ruby application to a PlanetScale database using two different methods:
- Rails — Connect a Rails application to PlanetScale
mysql2gem — Connect a Ruby application to PlanetScale using themysql2gem.
Follow the instructions below to find and insert your PlanetScale credentials.
- In the PlanetScale dashboard, select the database you want to connect to.
- Click "Branches" and select the branch you want to connect to.
- Click "Connect".
- Select "General" or "Rails" from the "Connect from" dropdown.
- If the password is blurred, click "New password".
- Copy the credentials. You won't be able to see the password again.
To connect, find the method you're connecting with below and follow the instructions in that section.
- Copy the contents of
rails/database.ymlinto your connection file. - Replace the placeholders for
HOSTNAME,DATABASE,USERNAME, andPASSWORDwith the copied values from the previous section. We encourage you to move these placeholder values into your.envfile. - To ensure a secure connection, you must fill in the SSL certificate path. This is the value for
sslca.
This path depends on your system, so you need to paste in the appropriate value.
You can find configuration information for your system in our Secure connections documentation.
- Require
mysql2in your Gemfile:
gem "mysql2"- Copy the contents of
mysql.rbinto your connection file. - Replace the placeholders for
HOSTNAME,DATABASE,USERNAME, andPASSWORDwith the copied values from the previous section. We encourage you to move these placeholder values into your local environment variables file. - To ensure a secure connection, you must fill in the SSL certificate path. This is the value for
sslca.
This path depends on your system, so you need to paste in the appropriate value.
You can find configuration information for your system in our Secure connections documentation.
Rails resources
- For more in-depth Rails instructions, check out the Rails quickstart.
- You can learn how to automatically copy Rails migration data during deployment in our Automatic Rails migrations documentation.
Next steps
- Learn more about the PlanetScale workflow.
- Learn how to branch your PlanetScale database for development.
- Learn how to ship schema changes to production with deploy requests.