MongoError:没有经过身份验证的用户 [英] MongoError: there are no users authenticated
问题描述
我正在尝试编写一个脚本,使用 mongodb
NodeJS 驱动程序 - 版本 3.0.1
我可以为数据库创建管理员用户,但不能创建一般用户.我总是收到MongoError:没有用户通过身份验证
.通过文档,验证用户身份的唯一方法是通过 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("请输入管理员用户名和密码!\n用法:\tnode init.js \n\n");}MongoClient.connect(url, function (err, client) {如果(错误)抛出错误;console.log("连接服务器成功");const db = client.db(config.database.name);var adminDb = db.admin();adminDb.addUser(adminUser, adminPassword, {角色:[{角色:userAdminAnyDatabase",数据库:管理员"}]}).then(函数(错误,结果){MongoClient.connect(format(authURL, encodeURIComponent(adminUser), encodeURIComponent(adminPassword)), function (err, authClient) {如果(错误)抛出错误;console.log('认证成功');const db = client.db(config.database.name);var adminDb = db.admin();db.addUser(config.database.auth.username, config.database.auth.password, {角色:[{角色:读写",db: config.database.name}]}).then(函数(){console.log("安装完成!");authClient.close();客户端关闭();}).catch(函数(错误){抛出 err.stack;});});});});
这是我的mongod.cfg
,mongod
进程的配置文件:
systemLog:目的地:文件路径:D:\MongoDB\logs\mongod.log贮存:dbPath: D:\MongoDB\database安全:授权:启用"
最后是配置文件,config.json
:
<代码>{数据库":{位置":本地主机","name": "mongodb-test",端口":27017,认证":{用户名":测试用户",密码":欢迎"}}}
通过先关闭客户端然后再次连接到 MongoDB 来解决它.这次使用 connect
返回的新 client
.
上面代码的相关部分是:
<代码>......………………adminDb.addUser(adminUser, adminPassword, {角色:[{角色:userAdminAnyDatabase",数据库:管理员"}]}).then(函数(结果){如果(结果&& result.user){console.log("管理员用户创建成功");客户端关闭();//关闭之前的连接!}MongoClient.connect(format(authURL, encodeURIComponent(adminUser), encodeURIComponent(adminPassword)), function (err, authClient) {如果(错误)抛出错误;console.log('认证成功');const db = authClient.db()//这很重要!....…………
I'm trying to write a script to add an admin user and a generic user to the MongoDB database using mongodb
NodeJS driver - version 3.0.1
I'm able to create the admin user, but not general user for a database. I'm always getting MongoError: there are no users authenticated
. Going through the documentation, the only way to authenticate a user is via URL. I've deleted the database completely from the specified path, tried multiple times, still, I'm stuck.
Here is what I've got so far,
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;
});
});
});
});
Here is my mongod.cfg
, the configuration file for mongod
process:
systemLog:
destination: file
path: D:\MongoDB\logs\mongod.log
storage:
dbPath: D:\MongoDB\database
security:
authorization: "enabled"
And finally the configuration file, config.json
:
{
"database": {
"location": "localhost",
"name": "mongodb-test",
"port": 27017,
"auth": {
"username": "testuser",
"password": "welcome"
}
}
}
Resolved it by closing the client first and then connecting to MongoDB again. This time use the new client
returned by connect
.
Relevant section from above code is:
.......
............
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!
....
........
这篇关于MongoError:没有经过身份验证的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!