windows命令行连接远程服务器MongoDB

基本命令及参数一览:

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

创建连接:

首先打开命令行cmd,跳转到MongoDB安装的位置,这里为  cd /d D:\software\professional\mongoDB\bin 

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

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

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

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

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

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

 

进一步的操作:

查看数据库: show dbs

指向某个数据库:use 数据库名

查看该数据下的集合:show collections

查找数据库中某个集合的数据:db.集合名.find()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、安装和启动 MongoDB远程机器上安装和启动 MongoDB,具体安装和启动过程可以参考 MongoDB 的官方文档。 2、开启 SSH 服务 在远程机器上开启 SSH 服务,并配置好 SSH 的端口,用户名和密码等信息。 3、从本地机器连接远程机器 在本地机器上使用 ssh 命令连接远程机器的 SSH 服务,例如: ``` ssh username@remote_ip_address -p ssh_port ``` 其中,`username` 为远程机器的用户名,`remote_ip_address` 为远程机器的 IP 地址,`ssh_port` 为远程机器的 SSH 端口号。 4、从远程机器连接MongoDB远程机器上使用命令行工具或 MongoDB 的客户端程序连接MongoDB,例如: ``` mongo --host mongodb_ip_address --port mongodb_port --username mongodb_username --password mongodb_password --authenticationDatabase mongodb_authentication_database ``` 其中,`mongodb_ip_address` 是 MongoDB 服务器的 IP 地址,`mongodb_port` 是 MongoDB 服务器监听的端口号,`mongodb_username` 和 `mongodb_password` 是 MongoDB连接用户名和密码,`mongodb_authentication_database` 是 MongoDB 的认证数据库名称。 5、连接 MongoDB 可视化工具 在本地机器上使用 MongoDB 的可视化工具连接远程机器的 MongoDB,例如使用 Robo 3T 或者 MongoDB Compass 等工具。在连接时,需要设置 MongoDB 服务器的 IP 地址,端口号,用户名和密码等信息。 以上五步就是通过 SSH 远程连接 MongoDB 的基本步骤,按照这些步骤操作,就可以在本地机器上连接远程机器上的 MongoDB 服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值