First, go to the PlanetScale (https://planetscale.com/) website and complete the account creation process.
Next, to install the PlanetScale CLI, open a PowerShell terminal window and enter the following command to install scoop. (Reference: https://scoop.sh/)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # Optional: Needed to run a remote script the first time
irm get.scoop.sh | iex
After installing scoop, install pscale. (Reference: https://github.com/planetscale/cli#installation)
scoop bucket add pscale https://github.com/planetscale/scoop-bucket.git
scoop install pscale mysql
Now that the PlanetScale CLI is installed, create a new NextJS project to test it.
npx create-next-app@latest
Enter the project folder and install the prisma package.
npm i prisma -D
npm i @prisma/client
Enter the prisma initialization command.
npx prisma init
After running the initialization command, the schema.prisma and .env files will be created.
Install the vscode extension to conveniently edit the schema.
Define the schema of the table to be used in the schema.prisma file as shown below. For detailed instructions on how to define the prisma schema, please refer to https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#model-field-scalar-types.
Change the db provider in the default schema from postgresql to mysql.
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model Test {
id Int @id @default(autoincrement())
name String
}
To connect to PlanetScale, enter the following command to log in to the PlanetScale site.
pscale auth login
After logging in, check the login completion message in the terminal window as shown below.
Now, to connect to the database created in PlanetScale, enter the following command. (For example, if the DB name is mytestdb)
pscale connect mytestdb
Change the DATABASE_URL in the .env file as follows.
DATABASE_URL="mysql://127.0.0.1:3306/mytestdb"
Enter the following command to create the table model defined in schema.prisma in the DB.
npx prisma db push
Once the push is successfully completed, you can check that a new table has been created on the PlanetScale website.
Click on Tables on the screen above to check the created table schema.
Once you have completed the above steps, you can now read and write to the DB created in PlanetScale from NextJS.
ยฉ 2025 juniyunapapa@gmail.com.