【发布时间】:2018-06-07 03:51:54
【问题描述】:
我正在 Digitalocean 上安装服务器,但遇到 MongoDB 问题。我在服务器上安装了 Mongo 并允许远程访问。
我创建了两个用户。一个是我的超级用户,另一个是网站用户的普通用户。 我可以通过 Robomongo 3T(mongo 客户端)为两个用户远程连接到 mongo。
但是,当我从浏览器使用该应用程序时,一个简单的登录请求会出现身份验证错误。这是错误:
MongoError: not authorized on DATABASE_NAME to execute command { find: "users", filter: { email: "YYY", password: "XXX" }, limit: 1, singleBatch: true, batchSize: 1 }
这里是真实的服务器端代码:
在 server.js 连接数据库:
MongoClient.connect(config.mongo_url, (err, client) => {
if(err) throw err
console.log('db connected')
app.locals.db = client
// start the server
app.listen(process.env.PORT || 3000, ()=>console.log('listening on port 3000!'))
});
尝试连接到服务器上的本地 mongo:
database.collection(config.mongo_col_user)
.findOne({
email: username,
password: hash
}, (err, user)=>{
if(err) return done(err)
if(!user) return done(null, false, {msg: 'not found'})
if(user.password)
return done(null, user)
})
我在服务器端使用 pm2,我使用 ecosystem.config.js,它的代码如下:
module.exports = {
apps : [
{
name: "NAME",
script: "SCRIPT_NAME",
watch: true,
env: {
"mongo_url": 'mongodb://USER_NAME:PASSWORD@localhost:27017?authMechanism=SCRAM-SHA-1&authSource=DATABASE_NAME',
"mongo_db_name": 'DATABASE_NAME',
"mongo_col_query": "COLLECTION_NAME",
}
}
]
}
你能帮忙解决这个问题吗?
【问题讨论】:
-
我相信不是。实际上,我做了答案建议的所有事情。我的问题是,尽管我可以从 shell 和 Robomongo 进行连接,但它在尝试从 REST API 连接时却给了我一个错误。
标签: node.js mongodb ubuntu-16.04