当前位置:网站首页>云服务器下载安装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.
截图如下:

边栏推荐
- This article takes you to understand the past and present of Mimir, Grafana's latest open source project
- 怀念故乡的月亮
- 【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
- Dart 命名参数语法
- Article summary: the basic model of VPN and business types
- Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
- JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
- 【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
- Character encoding and floating point calculation precision loss problem
- 雪糕和轮胎
猜你喜欢

让你的 Lottie 支持文字区域内自动换行

This article takes you to understand the past and present of Mimir, Grafana's latest open source project

【kali-信息收集】枚举——DNS枚举:DNSenum、fierce

Error using ts-node

Raspberry pie arm version of GCC installed configuration and environment variables

The bigger and bigger the project is, I split it like this

leetcode:126. Word Solitaire II

Which interpolation is better for opencv to zoom in and out??

移动端页面秒开优化总结
![[kali-information collection] enumeration - DNS enumeration: DNSenum, fierce](/img/97/bbe7c2af0ff8bcb5222b9105d80c73.png)
[kali-information collection] enumeration - DNS enumeration: DNSenum, fierce
随机推荐
How to download the Keil package
win10 fixed local IP
EntityFramework saves to SQLServer decimal precision is lost
Browser download shortcut to desktop (PWA)
开源项目站点必备&交流区功能
Article summary: the basic model of VPN and business types
What is dynamic programming and what is the knapsack problem
让你的 Lottie 支持文字区域内自动换行
[uniCloud] Application and Improvement of Cloud Objects
test
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
最新 955 不加班的公司名单
How to promote new products online?
ARM cross compilation
<JDBC> 批量插入 的四种实现方式:你真的get到了吗?
剑指offer专项突击版第16天
After specifying set 'execution.savepoint.path', restart flinksql and report this error
Replacing the Raspberry Pi Kernel
Interview Blitz 69: Is TCP Reliable?Why?
怀念故乡的面条