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 connect to local MongoDB (configured with replicaset)

See original GitHub issue

Bug description

I just ran into the following error while trying to setup a local MongoDB with replication (from this comment)

Prisma schema loaded from prisma/schema.prisma
Environment variables loaded from .env
Datasource "db"

Introspecting based on datasource defined in prisma/schema.prisma …

Error: Error in connector: Error querying the database: Server selection timeout: None of the available servers suitable for criteria Predicate. Topology: { Type: Unknown, Servers: [ { Address: localhost:27017, Type: RsGhost, Average RTT: 346.567µs, Last Update Time: DateTime(2022-02-16T20:00:59.552Z), Max Wire Version: 9, Min Wire Version: 0 }, ] }

Oddly, it works fine with TablePlus, but not with Prisma.


Steps to reproduce (OSX):

How to reproduce

  1. brew install mongodb-community
  2. vi /usr/local/etc/mongod.conf
  3. Add replication config in comment above
  4. brew services start mongodb-community

Then, Create a new Prisma project, with the following:

.env

DATABASE_URL="mongodb://localhost:27017/mongo"

prisma/schema.prisma

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["mongoDb"]
}

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

Lastly, run npx prisma db pull. After some time you should see:

Prisma schema loaded from prisma/schema.prisma
Environment variables loaded from .env
Datasource "db"

Introspecting based on datasource defined in prisma/schema.prisma …

Error: Error in connector: Error querying the database: Server selection timeout: None of the available servers suitable for criteria Predicate. Topology: { Type: Unknown, Servers: [ { Address: localhost:27017, Type: RsGhost, Average RTT: 346.567µs, Last Update Time: DateTime(2022-02-16T20:00:59.552Z), Max Wire Version: 9, Min Wire Version: 0 }, ] }

Then if you try TablePlus with the same connection string, it should work as expected.

Expected behavior

It should connect in the same way it connects to TablePlus and pull existing collections.

Environment & setup

  • OS: OSX
  • Database: MongoDB
  • Node.js version: v14.18.1

Prisma Version

Environment variables loaded from .env
prisma                  : 3.10.0-dev.50
@prisma/client          : Not found
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 1e2a986858a8bc0ad185ac8baf19fb72b382b7ec (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 1e2a986858a8bc0ad185ac8baf19fb72b382b7ec (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core 1e2a986858a8bc0ad185ac8baf19fb72b382b7ec (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt 1e2a986858a8bc0ad185ac8baf19fb72b382b7ec (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : 1e2a986858a8bc0ad185ac8baf19fb72b382b7ec
Studio                  : 0.458.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
matthewmuellercommented, Feb 18, 2022

Yah, I know it works with the local Docker setup, but that requires our users to have Docker installed and understand how to use Docker Compose.

I’d like us to also support:

  1. You get mongod on your machine somehow
  2. You edit mongod.conf to configure the replica set (bummer!)
  3. You start the mongod server
  4. You connect with prisma

It might be as simple as adding a missing a URL parameter. Nonetheless, TablePlus can handle the super basic connection string.

3reactions
Jolg42commented, Feb 18, 2022

I could reproduce but I don’t think it’s a bug actually, but misconfiguration of the replica set, which needs to be initialized.

I could “connect” with TablePlus but found an empty view and MongoDB support there is in Beta (so buggy behavior?). Using Robo 3T which I think is a better tool (solid and proven) for that, the connection would not work.

Also, the logs at /usr/local/var/log/mongodb/mongo.log show many entries with

{"t":{"$date":"2022-02-18T15:03:32.828+01:00"},"s":"I",  "c":"-",        "id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to refresh key cache","attr":{"error":"NotYetInitialized: Cannot use non-local read concern until replica set is finished initializing.","nextWakeupMillis":8800}}

I could make it work by initiating the replica set with the additional commands:

  • mongo opens a mongo shell
  • rs.initiate({_id: "rs0", members: [{_id: 0, host: "127.0.0.1:27017"}] })
  • optionally check on the status with rs.status()

After that npx prisma db pull will show

Prisma schema loaded from prisma/schema.prisma
Datasource "db"

Introspecting based on datasource defined in prisma/schema.prisma …
Error: 
P4001 The introspected database was empty: 

prisma db pull could not create any models in your schema.prisma file and you will not be able to generate Prisma Client with the prisma generate command.

To fix this, you have two options:

- manually create a table in your database.
- make sure the database connection URL inside the datasource block in schema.prisma points to a database that is not empty (it must contain at least one table).

Then you can run prisma db pull again. 
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot connect to replica set - Replication - MongoDB
I have recently built a mongodb replica-set with two replication nodes and one arbiter. all three nodes have a similar configuration: ...
Read more >
Cannot connect to replica set via mongo compass - MongoDB
I think you have an issue with your replica set configuration. I suspect that your replica set configuration uses host names rather than...
Read more >
Dotnet driver - can't connect to replica set - MongoDB
Hi all, wondering if anybody can help diagnose an issue connecting to a replica set using the .NET driver. Environment
Read more >
Can't connect remote replica set by Public IP - MongoDB
I've setup MongoDB replica set on a single machine with 3 different port 27017, 27018, and 27019. MongoDB connection works fine When I'm ......
Read more >
Can not connect to mongodb locally: Server selection timed ...
A client using a replication set connection must be able to connect to members of the replica set using the configured hostnames/IPs and...
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