Blümchenpeter
Blümchenpeter

Reputation: 1

Is there a parameter for the authentification mechanism for MongoDb Connection when using Powershell?

I want to do a Mongodump Backup of a database using a Powershell Skript, which i got from this page: text

I am getting an error which relates to a wrong authentification mechanism (SCRAM-SHA-1 instead of SCRAM-SHA-256) but i couldt not find an parameter to set the authentification mechanism.

My adaptation of the Powershell Skript from above (Username and Password are hidden):

<# Set the MongoDB access variables #>
$databaseName = "HistoryTest"
$username = "..."
$password = "..."
$mechanism="SCRAM-SHA-256"
$mongoDbHost = "localhost:27017"


<# Set the folders location and name #>
$backupPath = "C:\Mongo_Backup"
$currentDate = get-date -format yyyyMMddHHmm
$directoryName = "$databaseName-$currentDate"
$directoryPath = Join-Path $backupPath $directoryName

#endregion

#region Backup Process
$watch = New-Object System.Diagnostics.StopWatch
$watch.Start()
Write-Host "Backing up the Database: '$databaseName' to local directory: $backupPath."

# Use this command when the database require authorization
 mongodump -h "$mongoDbHost" `
   -d "$databaseName" `
   -u "$username" `
   -p "$password" `
   -o "$directoryPath" 


$archiveFileDestinationPath = "$backupPath\$directoryName.gz";
mongodump --gzip -h "$mongoDbHost" -d "$databaseName" --archive="$archiveFileDestinationPath"


Write-Host "Creating the backup for $databaseName..."

$watch.Stop();
Write-Host "MongoDB backup completed in "$watch.Elapsed.ToString()

#endregion

Complete Error Message: mongodump : 2023-01-31T12:11:36.381+0100 Failed: can't create session: could not connect to server: connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.

I already searched online for that spezific topic, but i coult not find anything usefull. Only examples for the mongo Shell but nothing related to Powershell. I also tried stuff like, adding a variable named $mechanism and setting it to "SCRAM-SHA-256" and included it in the mondodump call but it didnt worked.

Upvotes: 0

Views: 915

Answers (2)

Janis Ravelis
Janis Ravelis

Reputation: 1

I had the same issue, also same error when setting authentication mechanism as "SCRAM-SHA-256".

Turned out that I was accidentally using different variable for --authenticationDatabase option. Changed following option and it works now even without --authenticationMechanism option: --authenticationDatabase admin

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174825

You want to use the --authenticationMechanism option to pass the mechanism:

mongodump -h "$mongoDbHost" `
   -d "$databaseName" `
   -u "$username" `
   -p "$password" `
   -o "$directoryPath" `
   --authenticationMechanism $mechanism

Upvotes: 1

Related Questions