[英]MongoError: there are no users authenticated, unable to connect to mongodb server
[英]MongoError: there are no users authenticated
我正在嘗試編寫一個腳本,使用mongodb
NodeJS 驅動程序 - 版本3.0.1添加一個管理員用戶和一個通用用戶到 MongoDB 數據庫
我可以為數據庫創建管理員用戶,但不能創建一般用戶。 我總是收到MongoError: there are no users authenticated
。 通過文檔,驗證用戶身份的唯一方法是通過 URL。 我已經從指定的路徑中完全刪除了數據庫,嘗試了多次,仍然卡住了。
這是我到目前為止所得到的
const MongoClient = require('mongodb').MongoClient;
const format = require('util').format;
const config = require("./config.json");
var adminUser = process.argv[2],
adminPassword = process.argv[3],
url = `mongodb://${config.database.location}:${config.database.port}`,
authURL = `mongodb://%s:%s@${config.database.location}:${config.database.port}/?authMechanism=SCRAM-SHA-1&authSource=admin`;
if (!adminUser || !adminPassword) {
throw new Error("Please enter administrator username and password!\nUsage:\tnode init.js <adminUserName> <adminPassword>\n\n");
}
MongoClient.connect(url, function (err, client) {
if (err) throw err;
console.log("Connected successfully to server");
const db = client.db(config.database.name);
var adminDb = db.admin();
adminDb.addUser(adminUser, adminPassword, {
roles: [{
role: "userAdminAnyDatabase",
db: "admin"
}]
}).then(function (err, result) {
MongoClient.connect(format(authURL, encodeURIComponent(adminUser), encodeURIComponent(adminPassword)), function (err, authClient) {
if (err) throw err;
console.log('Authenticated Successfully');
const db = client.db(config.database.name);
var adminDb = db.admin();
db.addUser(config.database.auth.username, config.database.auth.password, {
roles: [{
role: "readWrite",
db: config.database.name
}]
}).then(function () {
console.log("Setup completed!");
authClient.close();
client.close();
}).catch(function (err) {
throw err.stack;
});
});
});
});
這是我的mongod.cfg
, mongod
進程的配置文件:
systemLog:
destination: file
path: D:\MongoDB\logs\mongod.log
storage:
dbPath: D:\MongoDB\database
security:
authorization: "enabled"
最后是配置文件config.json
:
{
"database": {
"location": "localhost",
"name": "mongodb-test",
"port": 27017,
"auth": {
"username": "testuser",
"password": "welcome"
}
}
}
通過先關閉客戶端然后再次連接到 MongoDB 來解決它。 這次使用connect
返回的新client
。
上面代碼的相關部分是:
.......
............
adminDb.addUser(adminUser, adminPassword, {
roles: [{
role: "userAdminAnyDatabase",
db: "admin"
}]
}).then(function (result) {
if (result && result.user) {
console.log("Admin user created successfully");
client.close(); // close the previous connection!
}
MongoClient.connect(format(authURL, encodeURIComponent(adminUser), encodeURIComponent(adminPassword)), function (err, authClient) {
if (err) throw err;
console.log('Authenticated Successfully');
const db = authClient.db() // this is important!
....
........
在我的情況下,這個錯誤是由於錯誤的數據庫配置造成的。
我的數據庫設置了用戶名和密碼,但貓鼬試圖在沒有它們的情況下進行連接,因此出現了這些錯誤。
const databaseUrl = auth
? `mongodb://${username}:${password}@${host}:${port}/${name}` //if with auth
: `mongodb://${host}:${port}/${name}`; //without auth
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.