在命令行输入“mongo.exe --help”命令,查看 mongo.exe 命令的帮助信息。如下:
MongoDB shell version v3.4.15-52-g874aa31cae
usage: mongo.exe [options] [db address] [file names (ending in .js)]
用法:mongo.exe [选项] [数据库地址] [文件名(.js结尾)]
db address can be:(数据库地址可以是)
foo foo database on local machine(foo数据库在本地机器上)
192.168.0.5/foo foo database on 192.168.0.5 machine(foo 数据库在 192.168.0.5机器上)
192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999(foo数据库在192.168.0.5机器的9999端口上)
Options:(选项)
--shell run the shell after executing files(在执行文件之后运行shell)
--nodb don't connect to mongod on startup - no 'db address' arg expected(不要在启动时连接到mongod,预期的参数:no '数据库地址')
--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)(启用IPv6支持(默认禁用))
--disableJavaScriptJIT disable the Javascript Just In Time compiler
--disableJavaScriptProtection allow automatic JavaScript function marshalling
--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(使用GSSAPI/Kerberos进行身份验证时使用的服务名称)
--gssapiHostName arg Remote host name to use for purpose of GSSAPI/Kerberos authentication(用于GSSAPI/Kerberos身份验证的远程主机名)
file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
下面将介绍 mongo.exe 不带任何参数快速连接到 MongoDB。如下:
D:mongodb-3.4.15in> mongo.exe MongoDB shell version v3.4.15-52-g874aa31cae connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.15-52-g874aa31cae >
上面成功连接到了mongodb。如果你的 mongodb 有权限验证,你可以直行下面操作:
# 切换到授权的数据库 > use test switched to db test # 验证权限 > db.auth("test", "123456"); 1
下面通过 --port 指定端口。如下:
D:servermongodb-3.4.15in> mongo --port 27017 MongoDB shell version v3.4.15-52-g874aa31cae connecting to: mongodb://127.0.0.1:27017/ MongoDB server version: 3.4.15-52-g874aa31cae >
下面将介绍使用 mongo.exe 通过 -port、-u、-p等参数连接到mongodb。如下:
D:mongodb-3.4.15in> mongo -port 27017 -u "test" -p "123456" --authenticationDatabase "test" MongoDB shell version v3.4.15-52-g874aa31cae connecting to: mongodb://127.0.0.1:27017/ MongoDB server version: 3.4.15-52-g874aa31cae > show tables test >
其中:
-port:服务端口
-u:用户名
-p:密码
--authenticationDatabase:授权的数据库
生产中常用 MongoDB URI 形式对数据库进行连接,如下:
mongodb://127.0.0.1:27017/dbName
添加用户名密码验证。如下:
mongodb://username:password@127.0.0.1:27017/dbName
MongoDB中使用 quit() 或 exit 命令退出当前mongodb连接。
(1)按 ctrl + c 快捷键可以退出mongodb连接。如下:
D:mongodb-3.4.15in> mongo MongoDB shell version v3.4.15-52-g874aa31cae connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.15-52-g874aa31cae > ^C bye
(2)使用quit()方法退出mongodb连接。如下:
D:mongodb-3.4.15in> mongo MongoDB shell version v3.4.15-52-g874aa31cae connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.15-52-g874aa31cae > quit()
(3)使用 exit 命令退出当前mongodb连接。如下:
D:mongodb-3.4.15in> mongo MongoDB shell version v3.4.15-52-g874aa31cae connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.15-52-g874aa31cae > exit bye