Closed nmerla9 closed 3 years ago
I do not have any input, sorry. Your exact commands, except the connection string, work in my environment.
Another way of getting database names is this Connect-Mdbc <conn-string> *
. Does it work for you?
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.
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
+ CategoryInfo : NotSpecified: (:) [Connect-Mdbc], MongoNotPrimaryException
+ FullyQualifiedErrorId : MongoDB.Driver.MongoNotPrimaryException,Mdbc.Commands.ConnectCommand
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.
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.
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.
If there is a MongoDB command for this then you can invoke it by Invoke-MdbcCommand
.
Or look for connection string options like mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test
where "test" is your replica set. ref
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.
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.
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.
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.
Does secondaryPreferred
makes any difference?
I am out of ideas.
Does
secondaryPreferred
makes any difference? I am out of ideas.
no, its not working either. I'll do some more research on this.
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.
Any progress? I will close the issue after a while if we have no progress.
I haven't had a chance to test the direct connection option in $settings. Will let you know if it works after testing.
same error returned with direct connection property.
Invoke-MdbcCommand : Server returned not master error
I am closing because it's not going anywhere :( Feel free to reopen with any new input, hints, etc...
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.