mongoDB的安装

2021/12/7 2:24:15

本文主要是介绍mongoDB的安装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

win11下安装mongodb5.0

百度搜索安装mongodb,发现搜索到的页面一直都无法下载,18年以前的都不对应,后来发现是网站改了,改成这个:
mongodb官网下载
我也不确定什么时候又会改了,先下载着用吧,我下载的是5.0.4的windows版本的压缩包,下载结束后直接解压缩在我定好的文件夹下:
在解压缩后我把所有文件都放在我的MongoDB文件夹下并新创了用来存放数据的Data文件夹和存放日志的Log文件夹,如下:
在这里插入图片描述
一键安装,选好自己的自定义目录
在这里插入图片描述
确定设备安装类型
在这里插入图片描述

在这里插入图片描述
取消勾选install mongodb compass,听说勾选了会安装得很慢,我也没试过。
在这里插入图片描述
然后在安装目录下的Data目录中新建两个目录:db和log,并在log目录下新建一个保存配置的文件,内容如下:

dbpath=D:\MongoDB\Data\db
logpath=D:\MongoDB\Data\log\mongod.log
logappend=true
auth=false

接下来就是安装服务的时候了,我们进入bin目录,在此处打开我们的命令行终端,输入以下命令:

PS D:\MongoDB\bin> ./mongod --config "D:\MongoDB\mongod.config" --install --serviceName "MongoDB"
PS D:\MongoDB\bin>

然后我们就可以在服务中启动我们的mongodb server服务了,可以直接在开始菜单中搜索服务就可以了。然后找到我们的[MongoDB server],从里面我们可以看到,我们的mongodb服务已经启动了。
在这里插入图片描述
最后检验一下
打开http://localhost:27017/看看是否出现如下界面就可以了:
在这里插入图片描述
然后我们就到此结束。。。。。。
呸呸呸,还得配置环境变量呢,把mongod的路径配置到环境变量path中就算可以了(部分电脑需要重启一下,比如我的),检验标志就是:

PS C:\Users\samu\Desktop> mongo
MongoDB shell version v5.0.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ca2c7ce1-04d0-46f5-88d9-85be27fd515c") }
MongoDB server version: 5.0.4
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting:
        2021-12-05T18:00:41.339+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>

输入mongo命令看看是否进入数据库

linux的ubuntu安装

我这里用的是win11的子系统,即win10下的WSL,所以会出现以下情况

jack@DESKTOP-SJO8SMG:/mnt/c/Users/samu$ systemctl status mongodb
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

sys init的替代命令
来,开始吧。

sudo apt update && sudo apt upgrade -y
sudo apt install mongodb

先更新一下安装方式,然后安装mongodb,这样得到的基本就是适合你系统的最新版本。安装好了以后,一般就已经启动了mongodb服务了,但我们需要查看一下服务是否启动了。

jack@DESKTOP-SJO8SMG:/mnt/c/Users/samu$ service mongodb status
 * Checking status of database mongodb                               [fail]

可以看到的是我的mongodb是安装好了的,但启动失败,所以我卸载一次看看是否是之前操作失误的残余导致

service mongodb stop
 sudo apt purge mongodb
 sudo apt autoremove

上面正常执行完毕就可以完全卸载mongodb了,但重新安装以后启动数据库和检查服务状态都是启动失败,看来不是这个情况呢。

jack@DESKTOP-SJO8SMG:/mnt/c/Users/samu$ mongo
MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
2021-12-05T18:30:17.292+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2021-12-05T18:30:17.292+0800 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed
jack@DESKTOP-SJO8SMG:/mnt/c/Users/samu$ service mongodb status
 * Checking status of database mongodb                               [fail]

让我们尝试一下第二种办法
在这里插入图片描述
依然是官网,选好我们的版本,然后下方有个Copy link,点击以后在我们的系统输入wget命令后接复制的链接就可以获取压缩包了,不过要记住别搞错自己的系统版本,可以先查看一下是否对应

jack@DESKTOP-SJO8SMG:/mnt/c/Users/samu$ cat /etc/issue
Ubuntu 20.04.3 LTS \n \l

我的版本如上,所以要的也是ubuntu 20.04的

wget https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/5.0/multiverse/binary-amd64/mongodb-org-server_5.0.4_amd64.deb
sudo dpkg -i mongodb-org-server_5.0.4_amd64.deb

上面的一些命令有时候会出现权限不够的情况,这时候就可以加前置命令sudo,不过这里在各种切换目录时后知后觉,发现子系统和windows系统用的是统一的存储,只不过抽象成另一种系统,这下让我对操作更谨慎了(c盘存储不足啊。。。。。。)
不过出人意料的是,它冒出以下结果:

Selecting previously unselected package mongodb-org-server.
(Reading database ... 38484 files and directories currently installed.)
Preparing to unpack mongodb-org-server_5.0.4_amd64.deb.1 ...
Unpacking mongodb-org-server (5.0.4) ...
Setting up mongodb-org-server (5.0.4) ...
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
Processing triggers for man-db (2.9.1-1) ...

着实让我摸不着头脑,因为上面的System has not been booted with systemd as init system (PID 1). Can’t operate.是表明我用的是systemctl提升命令权限才会有的,然后后面两句就应该是指示mongodb服务启动失败,让我们查看一下

jack@DESKTOP-SJO8SMG:/mnt/c/Users/samu$ service mongodb status
 * Checking status of database mongodb                               [fail]

嗯,是失败的,那应该是配置文件的问题,那我们去修改一下配置
在/etc/mongod.conf中添加以下配置

fork=true

然后又一次gg,这次错误是Error parsing YAML config file: yaml-cpp: error at line 21, column 1: end of map not found,查阅资料显示mongodb 3.0之后配置文件采用YAML格式,这种格式非常简单,使用:表示,开头使用“空格”作为缩进。需要注意的是,“:”之后有value的话,需要紧跟一个空格,如果key只是表示层级,则无需在“:”后增加空格(比如:systemLog:后面既不需要空格)。按照层级,每行4个空格缩进,第二级则8个空格,依次轮推,顶层则不需要空格缩进。如果格式不正确,将会出现上面的错误,所以就是我们要进行层级的修改
标记一下,这里还没成功

linux的centos7系统安装mongoDB

所用环境为腾讯服务器,centos7版本,安装很简单,应该是yum直接安装即可,但要先更新一下yum,需要的话,可以看看我写的这个博客,也可另寻适合的办法。
centos7的yum更新
让我们查看一下是否有适合的mongodb的相关资源

[root@VM-0-17-centos yum.repos.d]# yum info mongo-10gen
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Error: No matching Packages to list

哦豁,没有,尴了个尬,那我们就自行补充吧,让我们配置一个MongoDB的yum源吧。

[root@VM-0-17-centos ~]# cd /etc/yum.repos.d
[root@VM-0-17-centos yum.repos.d]# ls
Centos-7.repo  repo_bak
[root@VM-0-17-centos yum.repos.d]# vim mongodb-org-5.0.repo#添加mongodb-org-5.0的文件
[root@VM-0-17-centos yum.repos.d]# cat mongodb-org-5.0.repo
[mngodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/5.0/x86_64/
gpgcheck=0
enabled=1

然后更新一下
yum update
接下来安装

[root@VM-0-17-centos yum.repos.d]# yum -y install mongodb-org
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package mongodb-org-5.0.4-1.el7.x86_64 already installed and latest version
Nothing to do

嗯。。。。。。所以是yum update已经帮我搞定了是吗?让我们看一下

[root@VM-0-17-centos yum.repos.d]# whereis mongod
mongod: /usr/bin/mongod /etc/mongod.conf /usr/share/man/man1/mongod.1.gz

好像是,这些博客文章…
好,我们再查看一下配置文件

[root@VM-0-17-centos ~]# cat /etc/mongod.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

好的,fork不用修改,bindIp不用修改,不用考虑缩进的问题了,噢,Thandk God!!!好,启动服务!好,挂了(。。。。。。)

[root@VM-0-17-centos ~]# systemctl status mongod.service
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2021-12-06 12:02:31 CST; 44s ago
     Docs: https://docs.mongodb.org/manual
  Process: 28162 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
  Process: 28160 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 28158 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 28157 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 1444 (code=exited, status=0/SUCCESS)

Dec 06 12:02:31 VM-0-17-centos systemd[1]: Starting MongoDB Database Server...
Dec 06 12:02:31 VM-0-17-centos mongod[28162]: about to fork child process, w....
Dec 06 12:02:31 VM-0-17-centos mongod[28162]: forked process: 28165
Dec 06 12:02:31 VM-0-17-centos systemd[1]: mongod.service: control process e...4
Dec 06 12:02:31 VM-0-17-centos systemd[1]: Failed to start MongoDB Database ....
Dec 06 12:02:31 VM-0-17-centos systemd[1]: Unit mongod.service entered faile....
Dec 06 12:02:31 VM-0-17-centos systemd[1]: mongod.service failed.

大大的failed戳瞎了我的眼。。。。。。查来查去都是权限有问题,真是难受,先做个标记吧,回头再改。



这篇关于mongoDB的安装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程