w3ctech

thinkjs2怎么连mongodb?

这是我db.js的配置

export default {
    type: 'mongo',
    host: '10.1.201.210',
    port: '27017',
    name: 'bridgetest',
    user: 'hybird',
    pwd: '666666',
    prefix: '',
    encoding: 'utf8',
    nums_per_page: 10,
    log_sql: true,
    log_connect: true,
    cache: {
        on: true,
        type: '',
        timeout: 3600
    },
    adapter: {
        mysql: {},
        mongo: {
            options: {}
        }
    }
};

这是我mongodb管理员表的记录

> db.system.users.find()

{ "_id" : "admin.hybird", "user" : "hybird", "db" : "admin", "credentials" : { "
MONGODB-CR" : "e9084c300050482c1c3dd527e20d5cfe" }, "roles" : [ { "role" : "root
", "db" : "admin" } ] }
{ "_id" : "admin.hyadmin", "user" : "hyadmin", "db" : "admin", "credentials" : {
 "MONGODB-CR" : "a48af653727cd7a647f8a0f808e9fed3" }, "roles" : [ { "role" : "ro
ot", "db" : "admin" } ] }

我的mongo版本是2.6.11,启动mongo服务的时候带上--auth开启权限,如上配置访问页面会报以下错误。

[Error] MongoError: auth failed
    at Function.MongoError.create (D:\Projects\thinkjs\project_path\node_modules\mongodb\node_modules\mongodb-core
\lib\error.js:31:11)
    at D:\Projects\thinkjs\project_path\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:77
0:66
    at bound (domain.js:254:14)
    at runBound (domain.js:267:12)
    at Callbacks.emit (D:\Projects\thinkjs\project_path\node_modules\mongodb\node_modules\mongodb-core\lib\topolog
ies\server.js:95:3)
    at null.messageHandler (D:\Projects\thinkjs\project_path\node_modules\mongodb\node_modules\mongodb-core\lib\to
pologies\server.js:243:23)
    at Socket.<anonymous> (D:\Projects\thinkjs\project_path\node_modules\mongodb\node_modules\mongodb-core\lib\con
nection\connection.js:262:22)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)
    at TCP.onread (net.js:538:20)

验证失败? 之前不去管权限这块的话,是没问题的,现在加上了权限死活访问不了啊!

求大神解惑!

w3ctech微信

扫码关注w3ctech微信公众号

共收到2条回复

  • adapter: {
            mysql: {},
            mongo: {
                options: {
                    authSource: 'admin'
                }
            }
        }
    

    adapter配置中要加上authSource认证来源?可能是指向admin表。

    回复此楼
  • 文档上有说吧?

    回复此楼