当前位置:网站首页>MongoDB-使用mongo/mongosh命令行连接数据库
MongoDB-使用mongo/mongosh命令行连接数据库
2022-06-29 06:41:00 【小博测试成长之路】
之前的文章中有介绍:MongoDB-在windows电脑本地安装一个mongodb的数据库
但是,直接介绍的都是通过GUI工具去连接数据库,并没介绍用命令行的方式去连接,今天补充一下:
1
使用mongo命令
找到安装目录下的bin目录下,有一个mongo.exe,双击一下就可以:

如果忘记安装到哪个位置了怎么办呢?
可以在服务列表找到MongoDB对应的windows服务,然后双击服务,就可以看到安装的目录:

每次都要到安装目录下去启动,是不是很麻烦呢?有没有更简便的方法呢?
答案当时是有啦。将安装目录下的bin目录配置到环境变量中:

注意:以上这种方法只能连接本地的mongo服务,如果想连接其他服务器上部署的mongo数据库,可以采用下面的命令:
mongo -u 用户名 -p 密码 --port 端口号 --host ip 数据库名
mongo ip:port/数据库 -u 用户名 -p 密码
mongo "mongodb://ip:port"相当于我们平常输入mongo的时候,默认连接的是本地的mongo且端口是27017的。当然,使用命令行连接的方式肯定还有其他很多可选参数,大家有需要的可以自己去官网查查资料研究研究。
2
使用mongosh命令
安装后,默认在bin目录下是没有mongosh的命令的:

这个时候可以去官网下一个MongoDB Shell,下载地址:
https://www.mongodb.com/try/download/shell
下载完之后,建议安装到bin目录下 ,这样环境变量就不用再重新去额外配置了,安装完之后,我们再看一下bin目录下的内容:

安装好之后,连接本地默认的数据库的话,也可以使用mongosh的命令直接连接:

要连接其他服务器的数据库时,可以使用下面的命令:
mongosh "mongodb://ip:port"
mongosh ip:port/数据库 -u 用户名 -p 密码
mongosh -u 用户名 -p 密码 --port 端口号 --host ip 数据库名反正经过测试,用以上3种命令格式去连接,不管是使用mongo还是mongosh都可以连接成功。
如果写的文章对你有帮助,可以点个赞支持一下哟~
戳“阅读原文”一起来充电吧!
边栏推荐
- C#导入csv到mysql数据库中
- What you should know about databases
- Swin Transformer理论讲解
- 反思 - 完美主义
- Common MySQL errors and solutions summarized painstakingly (II)
- Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200
- 指针引用数组元素
- Select distinct on statement in kingbasees
- Problem solving -- > online OJ (13)
- 低配MySQL数据库几十秒插入百万数据
猜你喜欢
随机推荐
Summary of array knowledge points
101. symmetric binary tree (recursive and iterative methods)
ROS2中的行为树 BehaviorTree
ROS当中的仿真时间以及Bag包操作
Vulnhub's dc9 target
基于Sophus的Ceres优化
VMware vcenter/ESXI系列漏洞总结
SQL 注入绕过(六)
Appium 环境搭建
Reasons why the ext.dic file configured in ES does not take effect
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
关于开发web场景下如何解决手机访问web跨域问题
Schnuka: what is visual positioning system? How visual positioning system works
Cv:: mat and Base64 conversion (including picture compression and decompression)
蓝桥杯——13届第二批试题解析
【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结
Selected Siemens PLC project example source code [300 sets in total]
[量化投资系统]Django从数据库中实现筛选及分页
About the problem that the kingbasees temporary file is too large









