簡體   English   中英

MongoError:沒有經過身份驗證的用戶,無法連接到mongodb服務器

[英]MongoError: there are no users authenticated, unable to connect to mongodb server

我的server.js文件

  MongoClient.connect('mongodb://user:password@127.0.0.1:27017/QUIZ_GAME',(err,client)=>{
if(err){
  return console.log('unable to connect to the MongoDb server');
}
 const db =client.db('QUIZ_GAME');
 console.log('connected to the MongoDb server');

在我的膩子終端中,我這樣做是為了啟用身份驗證,並且我成功地做到了

 use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
 }
);
exit;

 use QUIZ_GAME
 db.createUser(
{
  user: "tester",
  pwd: "password",
  roles: [{ role: 'readWrite', db:'QUIZ_GAME'}]

添加數據承載節點

rs.initiate( {
   _id : "anyId" ,
   members: [ { _id : 0, host : "IP:PORT" } ]
})

以后如果您要添加任何其他成員,請添加rs.add("ip:port") ,然后嘗試執行此操作

要連接到服務器,只需使用

db: 'mongodb://username:password@10.2.1.00/db_name'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM