Skip to content

Instantly share code, notes, and snippets.

@jonico
Last active June 1, 2023 09:04
Show Gist options
  • Save jonico/95ce15e18626dc227f2568d43903f127 to your computer and use it in GitHub Desktop.
Save jonico/95ce15e18626dc227f2568d43903f127 to your computer and use it in GitHub Desktop.
How to run PlanetScale alongside with an MySQL enabled app that does not have any other internet access
PLANETSCALE_DB=brandnewdb
PLANETSCALE_BRANCH=mybranch
PLANETSCALE_ORG=jonico
PLANETSCALE_SERVICE_TOKEN=pscale_tkn_loCzIH7NktDK-GWJ71eX97Qr5D3a9iEO_pgHCSHUtw
PLANETSCALE_SERVICE_TOKEN_NAME=69xrlIwgs4ms

How to run PlanetScale alongside with an MySQL enabled app that does not have any other internet access (like in the InformatiCup CI setup)

Get a free developer account at PlanetScale

  1. Sign up for a free forever MySQL compatible database with up to three branches
  2. Create a database and fill it with some sample data
  3. Create a service token and grant it permissions to connect to your database
  4. Set the following environmental variables
  • PLANETSCALE_DB: name of your database
  • PLANETSCALE_BRANCH: name of your branch (probably main at the beginning)
  • PLANETSCALE_ORG: the default org is named after your PlanetScale account
  • PLANETSCALE_SERVICE_TOKEN: the token you created in step 3
  • PLANETSCALE_SERVICE_TOKEN_NAME the name of the token you create in step 3

You can either set those environmental variables in your shell (export PLANETSCALE_DB=infocup) or put them into a file .env alongside with the docker-compose.yml file.

For format illustration purposes, an .env file (with outdated credentials) is included in this gist. If you make your .env file executale or export the variables manually in your shell, you can now test db connectivity:

. .env && docker run -e PLANETSCALE_SERVICE_TOKEN=$PLANETSCALE_SERVICE_TOKEN -e PLANETSCALE_SERVICE_TOKEN_NAME -e PLANETSCALE_BRANCH -e PLANETSCALE_ORG -e PLANETSCALE_DB --rm -it -p 3306:3306/tcp --entrypoint '/bin/sh' planetscale/pscale:latest -c 'pscale connect --host `hostname -i | awk "{print $1}"` $PLANETSCALE_DB $PLANETSCALE_BRANCH --org $PLANETSCALE_ORG'

Run docker-compose with the docker-compose.yml file

docker-compose run app

After the run, you can shut down the database service by running

docker-compose down

Install MySQL driver in your app and connect to the database using host planetscale, (port 3306)

  • See example from docker-compose file, this would be the way to connect to the DB while running in the CI server:
mysql -h planetscale
  • For other ways to test against the database locally (i.e. when not running in CI), check out this documentation

  • Add your own app / service with the "no-internet" network to get a feeling how your app will behave

  • use docker-compose down when you change docker-compose.yml, before you run docker-compose up again

version: '3'
services:
planetscale:
image: planetscale/pscale:latest
networks:
- no-internet
- internet
ports:
- "3306:3306"
entrypoint: ["/bin/sh", "-c"]
environment:
- PLANETSCALE_DB
- PLANETSCALE_BRANCH
- PLANETSCALE_ORG
- PLANETSCALE_SERVICE_TOKEN
- PLANETSCALE_SERVICE_TOKEN_NAME
command: ["pscale connect --host `hostname -i | awk '{print $$1}'` $$PLANETSCALE_DB $$PLANETSCALE_BRANCH --org $$PLANETSCALE_ORG"]
app:
image: imega/mariadb-client
depends_on:
- planetscale
networks:
- no-internet
entrypoint: ["/bin/sh", "-c", "echo Wait for DB connection ... && sleep 10 && mysql -h planetscale -e 'SHOW tables;' && echo done"]
networks:
no-internet:
driver: bridge
internal: true
internet:
driver: bridge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment