搜索

首页  >  问答  >  正文

mongodb连接出错

明明db.auth返回1,可是mongo -u admin -p就是失败,为啥呢?

andyliwr@andyliwrPC:~/文档/xiaodiFTP/nodejs/xiaodiTodoList$ mongo
MongoDB shell version: 2.6.10
connecting to: test
> use admin
switched to db admin
> db.auth("admin", "123456")
1
> db.auth("admin", "1234535")
Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 }
0
> exit
bye
andyliwr@andyliwrPC:~/文档/xiaodiFTP/nodejs/xiaodiTodoList$ mongo -u admin -p
MongoDB shell version: 2.6.10
Enter password: 
connecting to: test
2016-07-31T22:41:33.520+0800 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1287
exception: login failed
阿神阿神2757 天前617

全部回复(2)我来回复

  • 迷茫

    迷茫2017-05-02 09:22:59

    你的用户是在admin这个数据库中的,而默认连接的数据库是test,自然认证会失败,你应该在使用mongo连接时指定数据库为admin,这样才能进行正确的认证:

    mongo -u admin -p admin

    回复
    0
  • PHPz

    PHPz2017-05-02 09:22:59

    mongo localhost/admin -u admin -p
    // or
    mongo -u admin -p --authenticationDatabase admin
    

    都是为了指定使用admin这个库中的用户来认证。有困难找文档,或者看一下--help。我机器上的是3.2,可能会和你的2.6有些出入。

    $ mongo --help
    MongoDB shell version: 3.2.6
    usage: mongo [options] [db address] [file names (ending in .js)]
    db address can be:
      foo                   foo database on local machine
      192.169.0.5/foo       foo database on 192.168.0.5 machine
      192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999
    Options:
      --shell                             run the shell after executing files
      --nodb                              don't connect to mongod on startup - no
                                          'db address' arg expected
      --norc                              will not run the ".mongorc.js" file on
                                          start up
      --quiet                             be less chatty
      --port arg                          port to connect to
      --host arg                          server to connect to
      --eval arg                          evaluate javascript
      -h [ --help ]                       show this usage information
      --version                           show version information
      --verbose                           increase verbosity
      --ipv6                              enable IPv6 support (disabled by default)
      --disableJavaScriptJIT              disable the Javascript Just In Time
                                          compiler
      --enableJavaScriptProtection        disable automatic JavaScript function
                                          marshalling
      --ssl                               use SSL for all connections
      --sslCAFile arg                     Certificate Authority file for SSL
      --sslPEMKeyFile arg                 PEM certificate/key file for SSL
      --sslPEMKeyPassword arg             password for key in PEM file for SSL
      --sslCRLFile arg                    Certificate Revocation List file for SSL
      --sslAllowInvalidHostnames          allow connections to servers with
                                          non-matching hostnames
      --sslAllowInvalidCertificates       allow connections to servers with invalid
                                          certificates
      --sslFIPSMode                       activate FIPS 140-2 mode at startup
    
    Authentication Options:
      -u [ --username ] arg               username for authentication
      -p [ --password ] arg               password for authentication
      --authenticationDatabase arg        user source (defaults to dbname)
      --authenticationMechanism arg       authentication mechanism
      --gssapiServiceName arg (=mongodb)  Service name to use when authenticating
                                          using GSSAPI/Kerberos
      --gssapiHostName arg                Remote host name to use for purpose of
                                          GSSAPI/Kerberos authentication
    

    回复
    0
  • 取消回复