Unable to parse Connection String in multi hostname MongoDB
See original GitHub issueBug description
Cannot connect successfully to mongodb when using a URL with a replica set specified.
- Removing the repeated host:port portion(s) for the replica set and instead just setting a single node works fine.
- The full URL with the replica set works fine when used with
mongo
on the command line.
Further details: A new project using Prisma 3.9.1 (https://registry.yarnpkg.com/prisma/-/prisma-3.9.1.tgz#7510a8bf06018a5313b9427b1127ce4750b1ce5c)
My mongodb connection URL, as printed from process.env.DATABASE_URL
within my running program is as per the following (with some bits redacted, obviously).
This works perfectly if I connect using mongo
on the command line.
When running in node and using const prisma = new PrismaClient();
I get the following error:
(node:50866) UnhandledPromiseRejectionWarning: Error: The provided database string is invalid. Unable to parse URL. in database URL. Please refer to the documentation in [https://www.prisma.io/docs/reference/database-reference/connection-urls]() for constructing a correct connection string. In some cases, certain characters must be escaped. Please check the string for any illegal characters.
at [/path/to/my/app/project/node_modules/]()@prisma[/client/runtime/index.js:36300:21]()
When running npx prisma db pull --force
using the CLI I get a different error:
project (main*) » npx prisma db pull --force Prisma schema loaded from prisma/schema.prisma
Environment variables loaded from .env
Datasource "db"
Introspecting based on datasource defined in prisma/schema.prisma …
Error: invalid port number in database URL
My database URL, as printed to the console via process.env.DATABASE_URL
immediately prior to initing the PrismaClient
is as follows:
mongodb://user:password@my-shard-00.atlas.mongodb.net:27017,my-shard-01.atlas.mongodb.net:27017,my-shard-02.atlas.mongodb.net:27017/database?ssl=true&replicaSet=atlas-xxxxx-shard-0&authSource=admin&retryWrites=true
How to reproduce
- Follow the Prisma getting started guide to create a new project using Mongo, from here.
- Use a Mongo Atlas connection URL with a replica set
- Cannot connect either via the prisma CLI tool or using the Prisma Client from within node.
Expected behavior
To be able to connect to a replica set.
Prisma information
From schema.prisma:
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
Environment & setup
- OS: MacOS 12.1
- Database: Mongodb Atlas
- Node.js version: v14.19.0
Prisma Version
prisma : 3.9.1
@prisma/client : 3.9.1
Current platform : darwin
Query Engine (Node-API) : libquery-engine bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : bcc2ff906db47790ee902e7bbc76d7ffb1893009
Studio : 0.457.0
Preview Features : mongoDb
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (9 by maintainers)
Top GitHub Comments
Thanks for the comments. I now agree we should support the multi hostname URL format.
My main concern was even if we supported it, your MongoDB version would be too old. We support MongoDB 4.2+ though so once we support this URL format, Prisma should work for you!
@matthewmueller It sounds to me that the DNS Seed list (with
+srv
):So the standard connection string format here always works, and we should support it.