denodrivers / mongo

🍃 MongoDB driver for Deno 🦕
https://deno.land/x/mongo
MIT License
509 stars 95 forks source link

deno_mongo uses replica information from primary instead of URL #41

Closed lewisakura closed 2 years ago

lewisakura commented 4 years ago

We have a replica set on MongoDB configured with hosts set in the /etc/hosts file which don't exist on our development machines. When using a URL with the actual domains that are public, deno_mongo uses the information from the primary server instead of the URL. This is not the behaviour we get with Node's MongoDB or with MongoDB Compass (which both use the servers in the connection URL).

thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: ServerSelectionError { message: "Server selection timeout: No available servers. Topology: { Type: ReplicaSetNoPrimary, Servers: [ { Address: premid:27017, Type: Unknown, Error: The supplied Host is unknown. (os error 11001) }, { Address: premid2:27017, Type: Unknown, Error: The supplied Host is  (os error 11001) }, { Address: premid3:27017, Type: Unknown, Error: The supplied Host is (os error 11001) }, ] }" } }', src\command\find.rs:41:26
Timeraa commented 4 years ago

Can confirm

alirealasad commented 4 years ago

yeah, that's true but this library is based on rust's MongoDB library which doesn't seem to have stringUrl connection. but still, if @manyuanrong implement this. that would be cool though.

manyuanrong commented 4 years ago

I hope to describe the problem as a more detailed feature request. Indicate what input is received and what result is expected

JorgeLuis commented 4 years ago

Hello, I went through the same thing a few hours ago, but I discovered that there is another way to connect "connectWithOptions". I leave you an example:

import { MongoClient } from "https://deno.land/x/mongo@v0.7.0/mod.ts";

const client = new MongoClient();
const opciones = {
    hosts: ['mongo1:27017','mongo2:27017','mongo3:27017','mongo4:27017' ],
    replSetName:'rs0'
}
//client.connectWithOptions(opciones);
client.connectWithOptions(opciones);
console.log('CLIENTE: ', client);
const db = client.database("deno");
const users = db.collection("dispositivos");

This code works for me in W10 but it doesn't work for me in Debian 9. And I do not know why...

lewisakura commented 4 years ago

@manyuanrong In the Node.js driver, the connection string is used to determine what servers the driver needs to connect to. What the Rust driver (and therefore the deno_mongo driver) does is that it's using the data from the primary server in the replica. Because this data is specifically for the servers, it shouldn't be used by the drivers when connecting. Instead, the connection string (which contains the IPs to the servers) should be used.

Timeraa commented 4 years ago

Until this issue is fixed, use this as a temporary solution (Windows only):

  1. Open C:\Windows\System32\drivers\etc\hosts in a text editor of your choice
  2. Add your hosts at the bottom of the file like this:
    IP HOST
    IP HOST2
    IP HOST3
  3. Save the file, run ipconfig /flushdns in a terminal
  4. Try again, if it doesn't work try restarting the terminal
  5. Enjoy!
lucarducci commented 4 years ago

I have the same problem also without replica. Is it possible to set the "moongose" option { useUnifiedTopology: true } in the connection?