mongodb连接出错

浏览:48日期:2023-06-24

问题描述

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

andyliwr@andyliwrPC:~/文档/xiaodiFTP/nodejs/xiaodiTodoList$ mongoMongoDB shell version: 2.6.10connecting to: test> use adminswitched to db admin> db.auth('admin', '123456')1> db.auth('admin', '1234535')Error: 18 { ok: 0.0, errmsg: 'auth failed', code: 18 }0> exitbyeandyliwr@andyliwrPC:~/文档/xiaodiFTP/nodejs/xiaodiTodoList$ mongo -u admin -pMongoDB shell version: 2.6.10Enter password: connecting to: test2016-07-31T22:41:33.520+0800 Error: 18 { ok: 0.0, errmsg: 'auth failed', code: 18 } at src/mongo/shell/db.js:1287exception: login failed

问题解答

回答1:

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

mongo -u admin -p admin回答2:

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

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

$ mongo --helpMongoDB shell version: 3.2.6usage: 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 9999Options: --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 --enableJavaScriptProtectiondisable 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 startupAuthentication Options: -u [ --username ] arg username for authentication -p [ --password ] arg password for authentication --authenticationDatabase arguser source (defaults to dbname) --authenticationMechanism arg authentication mechanism --gssapiServiceName arg (=mongodb) Service name to use when authenticating using GSSAPI/Kerberos --gssapiHostName argRemote host name to use for purpose of GSSAPI/Kerberos authentication

相关文章: