【问题标题】:MongoDB not authorized error on Ubuntu 16.04Ubuntu 16.04 上的 MongoDB 未授权错误
【发布时间】: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",
          }
        }
    ]
}

你能帮忙解决这个问题吗?

【问题讨论】:

标签: node.js mongodb ubuntu-16.04


【解决方案1】:

我想通了。

当我对 pm2 环境文件生态系统.config.js 进行更改时,我总是使用pm2 restart all 重新启动 pm2。但是,当您更改文件时,您应该告诉 pm2 您已将其更改为pm2 reload ecosystem.config.js --update-env

另外,正确的语法是no

mongodb://USER_NAME:PASSWORD@localhost:27017?authMechanism=SCRAM-SHA-1&authSource=DATABASE_NAME'

但是

mongodb://USER_NAME:PASSWORD@localhost:27017/DATABASE_NAME'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2023-03-18
    相关资源
    最近更新 更多