question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to parse Connection String in multi hostname MongoDB

See original GitHub issue

Bug 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

  1. Follow the Prisma getting started guide to create a new project using Mongo, from here.
  2. Use a Mongo Atlas connection URL with a replica set
  3. 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:closed
  • Created 2 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
matthewmuellercommented, Mar 16, 2022

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!

3reactions
Jolg42commented, Feb 14, 2022

@matthewmueller It sounds to me that the DNS Seed list (with +srv):

  • is something Atlas does by default but
  • is optional
  • some hosts / custom hosting might not support it / not implement it

So the standard connection string format here always works, and we should support it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot Connection Issues — MongoDB Atlas
If you are using a mongodb+srv:// connection string and your driver or shell can't find the DNS host of the Atlas database deployment,...
Read more >
URI does not have hostname, domain name and tld - Stack ...
Solution-2: Click on auto-generate password and paste it into the connection string, it will work. screenshot.
Read more >
Multiple hostname:port elements in a URL - Swift Forums
The MongoDB connection string does not meet these requirements and you'll probably have to implement this parsing yourself, as anything that ...
Read more >
The Easiest Way to Connect to MongoDB - Studio 3T
Paste a URI or connection string (e.g. from MongoDB Atlas) · Import connections from Robo 3T (Robomongo), NoSQLBooster, and other clients · Import...
Read more >
invalid schema, expected connection string to start ... - You.com
The error invalid schema, expected mongodb means that you're using an outdated node driver version. The old driver cannot parse the new mongodb+srv...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found