MongoDB 连接命令 mongo

mongo 连接

mongo 基本命令及参数一览:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
D:\software\professional\mongoDB\bin>mongo --help
MongoDB shell version v3.4.5
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.168.0.5/foo foo database on 192.168.0.5 machine
192.168.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
--disableJavaScriptProtection allow 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
--networkMessageCompressors arg Comma-separated list of compressors to
use for network messages
--jsHeapLimitMB arg set the js scope's heap size limit

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

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified

示例

1
2
3
4
mongo  -u 用户名 -p 密码  服务器地址:端口号

//指定用户名和密码连接到指定的MongoDB数据库
mongo 192.168.1.200:27017/test -u user -p password

注意:

3.4版本认证连接需要指定认证的数据库,即 “–authenticationDatabase”参数。

使用mongo命令进行连接,指定服务器地址及端口号,用户名-u,密码-p。

若没有设置用户认证,则不需要用户名及密码两个参数。

1
mongo --authenticationDatabase admin -u 用户名 -p 密码  服务器地址:端口号

注:若未指定–authenticationDatabase 参数,此版本的mongodb会报错:

1
2
3
4
5
_MongoDB server version: 3.4.17  
2018-12-03T12:40:36.976+0800 E QUERY [thread1] Error: Authentication failed. : DB.prototype._authOrThrow@src/mongo/shell/db.js:1459:20
@(auth):6:1
@(auth):1:2
exception: login failed

第二种连接方式如下

mongo 192.168.0.1:27017/库名 -u 用户名 -p 密码

mongo “10.122.236.14:27019/bucketadm” -u “bucketadm-user-read” -p “m8readuserbucketadm7lwiRVu1Sfs65drg”


MongoDB 连接命令 mongo
https://flepeng.github.io/041-MongoDB-MongoDB-连接命令-mongo/
作者
Lepeng
发布于
2021年6月6日
许可协议