Unable to connect to local MongoDB (configured with replicaset)
See original GitHub issueBug 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
brew install mongodb-community
vi /usr/local/etc/mongod.conf
- Add replication config in comment above
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:
- Created 2 years ago
- Comments:13 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
mongod
on your machine somehowIt might be as simple as adding a missing a URL parameter. Nonetheless, TablePlus can handle the super basic connection string.
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 withI could make it work by initiating the replica set with the additional commands:
mongo
opens a mongo shellrs.initiate({_id: "rs0", members: [{_id: 0, host: "127.0.0.1:27017"}] })
rs.status()
After that
npx prisma db pull
will show