我的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');
在我的putty终端中,我做了这个,启用auth并且我成功地做到了
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'}]
答案 0 :(得分:0)
添加数据承载节点
rs.initiate( {
_id : "anyId" ,
members: [ { _id : 0, host : "IP:PORT" } ]
})
以后如果您想添加任何其他成员,请添加rs.add("ip:port")
,然后尝试this
答案 1 :(得分:0)
要连接到服务器,只需使用
db: 'mongodb://username:password@10.2.1.00/db_name'