Connect your database (MongoDB)
To connect your database, you need to set the url
field of the datasource
block in your Prisma schema to your database connection URL:
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
In this case, the url
is set via an environment variable which is defined in .env
(the example uses a MongoDB Atlas URL):
DATABASE_URL="mongodb+srv://test:test@cluster0.ns1yp.mongodb.net/myFirstDatabase"
You now need to adjust the connection URL to point to your own database.
The format of the connection URL for your database depends on the database you use. For MongoDB, it looks as follows (the parts spelled all-uppercased are placeholders for your specific connection details):
mongodb://USERNAME:PASSWORD@HOST:PORT/DATABASE
Here's a short explanation of each component:
USERNAME
: The name of your database userPASSWORD
: The password for your database userHOST
: The host where amongod
(ormongos
) instance is runningPORT
: The port where your database server is running (typically27017
for MongoDB)DATABASE
: The name of the database. Note that if you're using MongoDB Atlas, you need to manually append the database name to the connection URL because the environment link from MongoDB Atlas doesn't contain it.
Troubleshooting
Error in connector: SCRAM failure: Authentication failed.
If you see the Error in connector: SCRAM failure: Authentication failed.
error message, you can specify the source database for the authentication by adding ?authSource=admin
to the end of the connection string.
Raw query failed. Error code 8000 (AtlasError): empty database name not allowed.
If you see the Raw query failed. Code: unknown. Message: Kind: Command failed: Error code 8000 (AtlasError): empty database name not allowed.
error message, be sure to append the database name to the database URL. You can find more info in this GitHub issue.