To create a connection string in MongoDB Atlas for Prisma, follow these steps:
-
Log In or Sign Up for MongoDB Atlas:
If you don't already have an account, go to the MongoDB Atlas website and sign up for a MongoDB Atlas account.
-
Create a New Project:
After signing in, create a new project in MongoDB Atlas. Give your project a name.
-
Build a New Cluster:
Inside your project, create a new cluster by clicking the "Build a Cluster" button. You'll need to choose a cloud provider (e.g., AWS, Azure, Google Cloud) and a region.
-
Configure Your Cluster:
Once your cluster is created, configure it according to your needs.
-
Create a MongoDB User:
In the "Database Access" section of your cluster settings, create a MongoDB user with the necessary privileges. You'll need this user's credentials to connect to the database from your Prisma application.
-
Get the Connection String:
In the "Clusters" view, click the "Connect" button for your cluster. This will open a dialog where you can choose how you want to connect. Select "Connect your application."
-
Configure Connection String:
In the "Connect to Cluster" dialog, you'll see a connection string that you can use in your Prisma application. It will look something like this:
mongodb+srv://<username>:<password>@clustername.mongodb.net/<dbname>
Replace
<username>
,<password>
,<clustername>
, and<dbname>
with your actual MongoDB Atlas credentials and database name. -
Use the Connection String in Prisma:
In your Prisma schema file, configure the
url
property in thedatasource
block to use the MongoDB Atlas connection string you obtained in the previous step:datasource db { provider = "mongodb" url = env("MONGO_TEST_URL") // Use your MongoDB Atlas connection string here }
You can use environment variables to securely store the connection string, or you can replace it directly.
-
Run godspeed prisma prepare command:
This command generates the prisma client for your application and connects the prisma to your database.
-
Start Building Your Application:
With the connection string and Prisma models in place, you can now start building your application using Prisma to interact with your MongoDB Atlas database.