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.

`Error: The provided database string is invalid. Unable to parse URL. in database URL.`

See original GitHub issue

Bug description

I’m trying to follow the getting started guide for MongoDB. Running the most basic example with very minimal credentials throws the following error:

  prisma:tryLoadEnv Environment variables loaded from /Users/username/myRepo/.env +0ms
  prisma:tryLoadEnv Environment variables loaded from /Users/username/myRepo/.env +1ms
  prisma:client dirname /Users/username/myRepo/node_modules/.prisma/client +0ms
  prisma:client relativePath ../../../prisma +0ms
  prisma:client cwd /Users/username/myRepo/prisma +0ms
  prisma:client clientVersion: 3.14.0 +1ms
  prisma:client clientEngineType: library +0ms
  prisma:client:libraryEngine internalSetup +0ms
  prisma:client:libraryEngine Searching for Query Engine Library in /Users/username/myRepo/node_modules/.prisma/client +1ms
  prisma:client:libraryEngine loadEngine using /Users/username/myRepo/node_modules/.prisma/client/libquery_engine-darwin-arm64.dylib.node +0ms
  prisma:client:libraryEngine library starting +3ms
(node:57335) 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 /Users/username/myRepo/node_modules/@prisma/client/runtime/index.js:41273:21
(Use `node --trace-warnings ...` to show where the warning was created)
(node:57335) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:57335) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I’m I came across https://github.com/prisma/prisma/issues/11883 but since username and password are so simple encoding should not be an issue.

How to reproduce

  1. Setup MongoDB using docker-compose.yaml
# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - ./database:/data/db
    ports:
      - 27017:27017

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
  1. Follow the getting started guide: https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/mongodb-node-mongodb
  2. In step https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/mongodb/querying-the-database-node-mongodb#write-your-first-query-with-prisma-client I’m running into the above error.

Expected behavior

No error. According to the guide the script should print an empty array

Prisma information

.env:

DATABASE_URL="mongodb+srv://root:example@localhost:27017/myDatabase"

schema.prisma as per example:

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}
model Post {
  id       String    @id @default(auto()) @map("_id") @db.ObjectId
  slug     String    @unique
  title    String
  body     String
  comments Comment[]
  author   User      @relation(fields: [authorId], references: [id])
  authorId String    @db.ObjectId
}

// Comments contain a comment string and connect back to the post.
// postId must have @db.ObjectId to match up with Post's id type
model Comment {
  id      String @id @default(auto()) @map("_id") @db.ObjectId
  post    Post   @relation(fields: [postId], references: [id])
  postId  String @db.ObjectId
  comment String
}

model User {
  id    String  @id @default(auto()) @map("_id") @db.ObjectId
  email String  @unique
  name  String?
  posts Post[]
}

Environment & setup

  • OS: Mac OS
  • Database: MongoDB running in Docker/Docker-Compose
  • Node.js version: v14.18.2

Prisma Version

 3.14.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
aqrlncommented, Sep 22, 2022

@jvolker

When mongodb+srv:// protocol is used, a port cannot be specified in the connection string. The domain in the connection string is only used to obtain the seed list from the SRV record — and the value of that record contains the hostnames and the corresponding ports of the servers in the replica set.

See these links for more info:

In your case, the correct fix is dropping +srv from your connection string since you are not using DNS seed list configuration.

We will improve the error message to include the error returned by the MongoDB driver.

1reaction
aqrlncommented, Sep 22, 2022

@Jolg42

Note: notice how the link is missing Please refer to the documentation in https://. 🙈

I can’t reproduce this, for me the URL to the docs is displayed properly. I also noticed some of the other lines in your output are trimmed in a similar way, e.g., ../@prisma/client/runtime/index.j) instead of .../@prisma/client/runtime/index.js:<line>:<col>). Could it be something that your terminal is doing?

Read more comments on GitHub >

github_iconTop Results From Across the Web

`The provided database string is invalid. Unable to parse URL ...
Something in this connection string seems to be weird, but the error message also does not make sense: Connection string: ...
Read more >
prisma can't connect to postgresql - Stack Overflow
Make sure that in your DATABASE_URL in the .env file, the special characters in your username and password are replaced with percent-encodings.
Read more >
Fail to connect to database PGSQL - SonarQube
Hey there. This is your sonar.jdbc.url. jdbc:postgresql://localhost:/Sonarqube?currentSchema=Sonar. This is the error ...
Read more >
Connection String URI Format — MongoDB Manual
The authentication database to use if the connection string includes username:password@ authentication credentials but the authSource option is unspecified.
Read more >
Connection URLs (Reference) - Prisma
The connection URL is provided via the url field of a datasource block in your Prisma schema.
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