当前位置:网站首页>云服务器下载安装mongo数据库并远程连接详细图文版本(全)
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
2022-08-01 03:45:00 【码农研究僧】
前言
关于mongo的知识点补充可看这篇文章:MongoDB框架零基础入门
在这篇文章中也讲到了windows安装mongo,而且关于windows的无法启动可看这篇文章:MongoDB出现Error parsing command line: unrecognised option ‘–fork‘ 的解决方法
此篇文章为 在服务器中安装mongo
1. 下载解压
下载对应的mongo版本号,通过mongo官网复制对应的链接进行curl:mongo官网

操作系统的平台博主为ubuntu20.04,具体查看版本号,通过如下命令cat /etc/issue 或者 lsb_release -a
之后下载对应的mongo操作系统版本链接:
操作完成之后对应copy link 复制链接
- 下载以及解压:
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.0.tgz(ubuntu使用curl,如果是centos使用wegt) - 解压:
tar -zxvf mongodb-linux-x86_64-ubuntu2004-6.0.0.tgz

- 复制到usr路径并且创建data文件夹(当数据有所变化,命令中通过–dbpath参数来指定db目录的路径)
[email protected]:~/mongo# mv mongodb-linux-x86_64-ubuntu2004-6.0.0/ /usr/mongodb
[email protected]:~/mongo# mkdir -p /usr/mongodb/data
截图如下:

在创建一个log日志来存放log的配置文件:touch /usr/mongodb/mongod.log
为了让全局终端输入mongo都有效,需要配置环境变量
通过vim /etc/profile进入编辑模式
export MONGO_HOME=/usr/mongodb
export PATH=$PATH:$MONGO_HOME/bin
让配置生效:source /etc/profile
通过echo $PATH 或者 printenv PATH或者echo $MONGO_HOME,查看配置是否有生效,有这路径

2. 启动
2.1 命令启动
之后启动mongo可以通过mongod运行对应的参数,而不需要在mongodb下的bin目录执行
- 后台运行模式(建议):
mongod --dbpath=/usr/mongodb/data --logpath=/usr/mongodb/mongod.log --logappend --port=27017 --fork - 当前运行模式(执行后不可关闭终端):
mongod --dbpath=/usr/mongodb/data --logpath=/usr/mongodb/mongod.log --logappend --port=27017

在当前的运行模式下,如果crtl+c退出终端之后,mongo就会自动退出,具体查看进程号通过ps -ef | grep mongo

如果关闭mongo的启动,则通过kill -9 进程号即可
具体启动之后如果没有这个mongo信息,也就是没有启动成功,则需要删除/usr/mongodb/data/mongod.lock文件
[email protected]:/usr# mongod --dbpath=/usr/mongodb/data --logpath=/usr/mongodb/mongod.log --logappend --port=27017 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 6836
child process started successfully, parent exiting
主要原因是因为上次的非正常关闭,导致mongodb在启动后创建了这个lock文件,影响下一次的启动,只需要将其删除即可(一般正常启动也会有,如果无法启动删除该文件在启动即可)

通过rm - rf mongod.lock删除,在将其启动即可
2.2 配置启动
通过touch 创建一个配置文件进行存放:touch /usr/mongodb/mongod.conf
# 日志
logpath=/usr/mongodb/data/mongod.log
# 数据库
dbpath=/usr/mongodb/data
# 端口映射
port=27017
# 日志是否追加
logappend=true
# 后台运行
fork=true
# 最大连接数
maxConns=100
# 是否启用用户访问
# auth=true
# 是否远程链接
bind_ip = 0.0.0.0
截图如下:

之后通过如下进行启动:mongod --config /usr/mongodb/mongod.conf
[email protected]:/usr/mongodb# mongod --config /usr/mongodb/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 6966
child process started successfully, parent exiting
[email protected]:/usr/mongodb# ps -ef | grep mongo
root 6966 1 9 18:03 ? 00:00:00 mongod --config /usr/mongodb/mongod.conf
root 7007 6603 0 18:04 pts/3 00:00:00 grep --color=auto mongo
[email protected]:/usr/mongodb# kill -9 6966
[email protected]:/usr/mongodb# ps -ef | grep mongo
root 7011 6603 0 18:04 pts/3 00:00:00 grep --color=auto mongo
截图如下:

注意的两点:
- 如果关闭mongo的启动,则通过
kill -9 进程号即可 - 如果无法执行该文件,则通过
chmod 777 mongod.conf增加权限
3. 连接
3.1 本地连接
启动之后本地测试是否可以正常连接,只需要配置相应的代码
或者通过curl 127.0.0.1:27017即可验证
出现如下所示:It looks like you are trying to access MongoDB over HTTP on the native driver port.
即代表可以连接成功:

3.2 远程连接
博主使用的是华为云服务器
云服务器的启动需要配置入站规则(只要是云服务器就要配置入站规则)
所以在云服务器的安全组中增加一个27017的入站规则即可

之后通过网页输入:服务器ip:27017即可看到这个It looks like you are trying to access MongoDB over HTTP on the native driver port.
截图如下:

边栏推荐
- [Search topic] After reading the inevitable BFS solution to the shortest path problem
- Dynamic Programming 01 Backpack
- HCIP(15)
- This map drawing tool is amazing, I recommend it~~
- 智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
- 【SemiDrive源码分析】系列文章链接汇总(全)
- ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks
- Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
- Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
- button remove black frame
猜你喜欢
![Valentine's Day Romantic 3D Photo Wall [with source code]](/img/a9/2c26f4f048f3c0a9a65551bc734233.png)
Valentine's Day Romantic 3D Photo Wall [with source code]

MySQL4

Hackers can how bad to what degree?

The 16th day of the special assault version of the sword offer

How to download the Keil package

IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints

button remove black frame

JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类

每周小结(*67):为什么不敢发表观点

The fledgling Xiao Li's 114th blog project notes: Wisdom cloud intelligent flower watering device combat (3) - basic Demo implementation
随机推荐
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
Flutter Tutorial 01 Configure the environment and run the demo program (tutorial includes source code)
pdb药物综合数据库
Guys, MySQL cdc source recycles replication slave and r in incremental process
Message queue MySQL table for storing message data
【Make YOLO Great Again】YOLOv1-v7全系列大解析(Neck篇)
How to promote new products online?
项目越写越大,我是这样做拆分的
"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
【愚公系列】2022年07月 Go教学课程 024-函数
大佬们,MySQL cdc source在增量过程中回收 replication slave 和 r
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
leetcode:126. Word Solitaire II
移动端页面秒开优化总结
Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink
2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m
[Getting Started Tutorial] Rollup Module Packager Integration
IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints
Raspberry pie arm version of GCC installed configuration and environment variables
带你体验一次类型编程实践