mongoDB 3.0レプリカセットのadmin
データベースにユーザーを作成するスクリプトを作成しようとしています。Powershellを使用してmongoDBユーザーを作成するには?
さまざまなバージョンのc-sharp MongoDBドライバを使って多くのことを試してみた結果、次のコードを組み立てることができました。これは、レプリカセットに正常にログを記録し、admin
データベースにアクセスし、 system
コレクションの完全な名前:この時点で
try {
Set-ExecutionPolicy Unrestricted
Add-Type -Path 'E:\drivers\MongoDB-CSharpDriver-2.0.2\MongoDB.Driver.dll'
Add-Type -Path 'E:\drivers\MongoDB-CSharpDriver-2.0.2\MongoDB.Driver.Core.dll'
Add-Type -Path 'E:\drivers\MongoDB-CSharpDriver-2.0.2\MongoDB.Bson.dll'
$connectionString = "mongodb://username:[email protected]:27017,secondary:27017,arbiter:27017/admin?replicaSet=name&authSource=admin"
$dbName = "admin"
$collectionName = "System"
function Get-MongoDBDatabase ($connectionString, $db) {
$mongoClient = New-Object MongoDB.Driver.MongoClient($connectionString)
$mongoServer = $mongoClient.GetServer()
$mongoServer.GetDatabase($db)
}
[MongoDB.Driver.MongoDatabase] $mongoDatabase = Get-MongoDBDatabase $connectionString $dbName
[MongoDB.Driver.MongoCollection] $mongoCollection = $mongoDatabase.GetCollection($collectionName)
Write-Output "Test accessing collection: $($mongoCollection.FullName)"
}
catch {
Write-Output "Error: $($_.Exception.Message)"
}
私はadmin
データベースに新しいユーザーを作成し、このユーザーにいくつかの役割を割り当てるために移動する方向に見当もつかない。上記
db.createUser(
{
user: "testUser",
pwd: "abc123",
roles: [
{role:"read", db:"admin"},
{role:"readWrite", db:"databaseOne"},
{role:"readWrite", db:"databaseTwo"},
{role:"readWrite", db:"databaseThree"},
{role:"readWrite", db:"databaseFour"},
{role:"read", db:"config"}]
}
)
私はadmin
データベース上で実行できるようにしたいコマンド/クエリですが、私はMongoDBのC-シャープドライバーV.2.0を使用してPowerShellのにこれを変換する方法を理解するためにあなたの助けが必要.2
ドライバのバージョンは非常に古いので、最新のものに更新する必要があります。 –
もし私が@PeteGarafanoをしたら、私はジェネリックスに入りますし、何らかの理由で上記のコードが何を達成することができる作業用コードも見つけられませんでした。 –
@PeteGarafano私はあなたの提案に従うことに決めました - 下の私の解決策を見てください –