Home > other >  Is there a parameter for the authentification mechanism for MongoDb Connection when using Powershell
Is there a parameter for the authentification mechanism for MongoDb Connection when using Powershell

Time:02-01

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.

CodePudding user response:

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

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

CodePudding user response:

Okay to make some things clear:

I did not handle to connect to the MongoDb using the code from above. But i was successfully able to connect using the uri instead of the individual parameters.

My new code looks like this:

$uri = "mongodb://USER:PASSWORD@localhost:27017/?authMechanism=SCRAM-SHA-256"

#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."

mongodump --uri "$uri"  -o "$directoryPath"

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

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

Now the backup does actually work, but I still get the following error message which I am not sure what it is about:

  • In C:\Users\Administrator\Desktop\Mongo_Backup_local.ps1:35 Zeichen:1
  • mongodump --uri "$uri" -o "$directoryPath"
  • #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (2023-0201T14:4...stem.users.bson:String) [], RemoteException
  • FullyQualifiedErrorId : NativeCommandError

(I added a # infront of row 3 because otherwise it would not be displayed correct, also i needed to use a list for the error message because Stackoverflow went wild with the formating)

As I mentioned I dont know what this error means, or if I can just ignore it.

Another crazy thing is that as soon as I edit the uri to be directed direcetly onto a spezific database, i get an autentification error again. For example:

$uri = "mongodb://USER:PASSWORD@localhost:27017/History?authMechanism=SCRAM-SHA-256" 

gets me following error: mongodump : 2023-02-01T15:05:25.409 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-256": (AuthenticationFailed) Authentication failed.

  • Related