nightroman / Mdbc

MongoDB Cmdlets for PowerShell
Apache License 2.0
141 stars 16 forks source link

Unable to read from secondary server #64

Closed nmerla9 closed 3 years ago

nmerla9 commented 3 years ago

Hi,

I'm trying to run "listDatabases" command in secondary server with a direct connection but getting the below error.

Invoke-MdbcCommand : Server returned not master error. At line:1 char:1

The same command used to work in earlier versions. Here is my code

$dbconn = "mongodb://username:pwd@servername:27017/admin?connect=direct" Connect-Mdbc $dbconn $db= $Client.GetDatabase('admin') Invoke-MdbcCommand @{listDatabases = 1} -Database $db

Appreciate any inputs on how to run the slaveOk command on secondary or another way to run listDatabases command in secondary server with direct connection.

nightroman commented 3 years ago

I do not have any input, sorry. Your exact commands, except the connection string, work in my environment.

nightroman commented 3 years ago

Another way of getting database names is this Connect-Mdbc <conn-string> *. Does it work for you?

nmerla9 commented 3 years ago

I do not have any input, sorry. Your exact commands, except the connection string, work in my environment.

Thanks for the response. Can you share the conn.string that this worked on a secondary server in a replset? May be I'm missing an option in connection.

nmerla9 commented 3 years ago

Another way of getting database names is this Connect-Mdbc <conn-string> *. Does it work for you?

PS C:\WINDOWS\system32> Connect-Mdbc $dbconn * Connect-Mdbc : Server returned not master error. At line:1 char:1

nightroman commented 3 years ago

Can you share the conn.string that this worked on a secondary server in a replset? May be I'm missing an option in connection.

I am using the default connection string and my local database. This is not your setup apparently.

Can you get what you need using the mongo shell connection and command? I am not saying "use mongo shell instead". I am saying this way may help to understand your problem better.

nightroman commented 3 years ago

P.S. Just in case. I am not an expert in MongoDB in any way. Mdbc is just a wrapper of C# driver making it all PowerShell friendly. This helps me a lot in using MongoDB mostly as the local data storage with just PowerShell. This is it...

The problem might be not about Mdbc at all. That is why I suggest to explore it using the shell. And then (maybe) ask questions at MongoDB forums.

nmerla9 commented 3 years ago

I needed to run "rs.slaveOk()" command on the secondary using mongo shell before running any commands such as listDatabases.

If there is a way to run the "rs.slaveOk()" from Mdbc will solve my problem.

nightroman commented 3 years ago

If there is a MongoDB command for this then you can invoke it by Invoke-MdbcCommand.

nightroman commented 3 years ago

Or look for connection string options like mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test where "test" is your replica set. ref

nightroman commented 3 years ago

I have an idea. It has something to do with read preference (https://docs.mongodb.com/manual/core/read-preference/#replica-set-read-preference-modes). Let me see if this may be configured in Mdbc.

nmerla9 commented 3 years ago

If there is a MongoDB command for this then you can invoke it by Invoke-MdbcCommand.

Unfortunately I couldn't find an equivalent DB command for the slaveOk.

nightroman commented 3 years ago

Try this

Import-Module Mdbc

#! use you conn-string
$settings = [MongoDB.Driver.MongoClientSettings]::FromConnectionString("mongodb://localhost:27017")

$settings.ReadPreference = [MongoDB.Driver.ReadPreference]::Secondary # or SecondaryPreferred?

$Client = [MongoDB.Driver.MongoClient]::new($settings)
$Database = $Client.GetDatabase('admin')

Invoke-MdbcCommand @{listDatabases = 1} -Database $Database

If it works then I'll think how to add this to Mdbc, more friendly.

nmerla9 commented 3 years ago

Try this

Import-Module Mdbc

#! use you conn-string
$settings = [MongoDB.Driver.MongoClientSettings]::FromConnectionString("mongodb://localhost:27017")

$settings.ReadPreference = [MongoDB.Driver.ReadPreference]::Secondary # or SecondaryPreferred?

$Client = [MongoDB.Driver.MongoClient]::new($settings)
$Database = $Client.GetDatabase('admin')

Invoke-MdbcCommand @{listDatabases = 1} -Database $Database

If it works then I'll think how to add this to Mdbc, more friendly.

Same error though the server settings shows read preference secondary.

nightroman commented 3 years ago

Does secondaryPreferred makes any difference? I am out of ideas.

nmerla9 commented 3 years ago

Does secondaryPreferred makes any difference? I am out of ideas.

no, its not working either. I'll do some more research on this.

nightroman commented 3 years ago

I think you should also try $settings.DirectConnection = $true, if direct connection is important in your case. Because [MongoDB.Driver.MongoClientSettings]::FromConnectionString("mongodb://localhost:27017/admin?connect=direct") does not set it as one may expect.

nightroman commented 3 years ago

Any progress? I will close the issue after a while if we have no progress.

nmerla9 commented 3 years ago

I haven't had a chance to test the direct connection option in $settings. Will let you know if it works after testing.

nmerla9 commented 3 years ago

same error returned with direct connection property.

Invoke-MdbcCommand : Server returned not master error

nightroman commented 3 years ago

I am closing because it's not going anywhere :( Feel free to reopen with any new input, hints, etc...