search

Home  >  Q&A  >  body text

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 days ago620

reply all(2)I'll reply

  • 迷茫

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

    Your user is in admin这个数据库中的,而默认连接的数据库是test,自然认证会失败,你应该在使用mongo连接时指定数据库为admin so that proper authentication can occur:

    mongo -u admin -p admin

    reply
    0
  • PHPz

    PHPz2017-05-02 09:22:59

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

    They are all for designated useadmin这个库中的用户来认证。有困难找文档,或者看一下--help. The version on my machine is 3.2, which may be slightly different from yours on 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
    

    reply
    0
  • Cancelreply