Mongodb安装---->Ubuntu 20.04 LTS

本文记录了在Linux上安装MongoDB 5.0的详细步骤,包括下载、解压、配置、启动服务及解决Address already in use错误的过程。在安装过程中,创建数据库目录、配置文件,并对日志文件和数据库目录设置权限。遇到的问题是端口已被占用,通过删除mongodb.lock文件和调整日志文件权限得以解决。
摘要由CSDN通过智能技术生成

新手安装Mongodb的一些记录

在开始安装时,选对教程是最重要的哟。首先我个人推荐官方教程,本文主要是结合[官方教程]和自己安装时所遇到一些问题的记录。每个人所遇到的问题可能有一些细微差异,同时本文只是个人的一些记录,如果很巧让你遇到和我相同的问题,能够帮助你也会给本次记录赋予一些意义!!!

MongoDB是什么?

为了不曲解(新手的觉悟),我就直接将官方的介绍贴上来啦^. ^

MongoDB is a document database with the scalability... 
and flexibility that you want with the querying and indexing that you need

安装

在Linux上安装一种服务很多种方式(你们也可以采用其他方式),我自己采用的是下载.tgz文件,然后根据自己的喜好,将其解压在某个目录下,可选择性和灵活性都比较好。

  1. 版本:MongoDB 5.0 Enterprise Edition

  2. 下载链接->点我,按照如下步骤,选择自己想要的版本。我下载开了vpn几秒就解决了,给你们一个地址吧,提取码:7gqq,不过我只上传了我的版本哈~在这里插入图片描述

  3. 下载好了,如图所示
    在这里插入图片描述

  4. 解压

tar -zxvf mongodb-linux-x86_64-enterprise-ubuntu2004-5.0.2.tgz
  1. 移动到自己想要的路径,首先建立一个目录
cd /usr/local
sudo mkdir mongodb
  1. 进入到我们下载好的路径下,移动到我们新建的目录下面
cd ~/下载/mongodb-linux-x86_64-enterprise-ubuntu2004-5.0.2/
sudo mv bin/ /usr/local/mongodb/
  1. 还需要做一点准备工作,以免后面出现意想不到的问题
sudo apt-get install libcurl4 libgssapi-krb5-2 libldap-2.4-2 libwrap0 libsasl2-2 libsasl2-modules libsasl2-modules-gssapi-mit snmp openssl liblzma5
  1. 这样我们的基本的环境就算可以了。接下来就是一些配置,由于是我们直接下载的tgz包,因而没有默认的配置文件,我们首先在/etc 建立我们的配置文件,命名为mongodb.conf就可。
cd /etc
sudo touch mongodb.conf
  1. 我们就按照官方给的基本配置就好,对于初学者使用足够,后面有需要可以修改就ok~
processManagement:
   fork: true
net:
   bindIp: localhost
   port: 27017
storage:
   dbPath: /var/lib/mongo #根据这个配置,我们需要建立相应的文件路径 数据库目录
systemLog:
   destination: file
   path: "/var/log/mongodb/mongod.log" #同上 日志目录
   logAppend: true
storage:
   journal:
      enabled: true
  1. 建立数据库目录和日志文件
cd /var/lib
sudo mkdir mongo
cd /var/log/
sudo mkdir mongodb
cd mongodb/
sudo touch mongod.log 

安装的步骤基本就OK啦,下面开始启动服务。

使用

遵循官方介绍,我们首先不使用配置文件,直接用命令行来指定参数:

mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

很简单哈,这里指定了三个参数,数据库路径,日志文件和子进程模式运行。
当然我们既然配置了文件怎么能不用,对吧,直接上代码:

mongod -f /etc/mongodb.conf

上面的两种方式任意一种都可以。

遇到的问题

  1. 子进程没等服务启动就当down了,我们看看日志
cd /var/log/mongodb/
sudo vim mongodb.log

问题详情:

{"t":{"$date":"2021-09-05T11:18:49.026+08:00"},"s":"E",  "c":"CONTROL",  "id":20568,   "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}}
{"t":{"$date":"2021-09-05T11:18:49.026+08:00"},"s":"I",  "c":"REPL",     "id":4784900, "ctx":"initandlisten","msg":"Stepping down the ReplicationCoordinator for shutdown","attr":{"waitTimeMillis":15000}}
{"t":{"$date":"2021-09-05T11:18:49.026+08:00"},"s":"I",  "c":"COMMAND",  "id":4784901, "ctx":"initandlisten","msg":"Shutting down the MirrorMaestro"}
{"t":{"$date":"2021-09-05T11:18:49.026+08:00"},"s":"I",  "c":"SHARDING", "id":4784902, "ctx":"initandlisten","msg":"Shutting down the WaitForMajorityService"}
{"t":{"$date":"2021-09-05T11:18:49.026+08:00"},"s":"I",  "c":"NETWORK",  "id":4784905, "ctx":"initandlisten","msg":"Shutting down the global connection pool"}
{"t":{"$date":"2021-09-05T11:18:49.026+08:00"},"s":"I",  "c":"NETWORK",  "id":4784918, "ctx":"initandlisten","msg":"Shutting down the ReplicaSetMonitor"}
{"t":{"$date":"2021-09-05T11:18:49.026+08:00"},"s":"I",  "c":"SHARDING", "id":4784921, "ctx":"initandlisten","msg":"Shutting down the MigrationUtilExecutor"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"ASIO",     "id":22582,   "ctx":"MigrationUtil-TaskExecutor","msg":"Killing all outstanding egress activity."}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"COMMAND",  "id":4784923, "ctx":"initandlisten","msg":"Shutting down the ServiceEntryPoint"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"CONTROL",  "id":4784925, "ctx":"initandlisten","msg":"Shutting down free monitoring"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"CONTROL",  "id":4784927, "ctx":"initandlisten","msg":"Shutting down the HealthLog"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"CONTROL",  "id":4784928, "ctx":"initandlisten","msg":"Shutting down the TTL monitor"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"CONTROL",  "id":4784929, "ctx":"initandlisten","msg":"Acquiring the global lock for shutdown"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"-",        "id":4784931, "ctx":"initandlisten","msg":"Dropping the scope cache for shutdown"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"FTDC",     "id":4784926, "ctx":"initandlisten","msg":"Shutting down full-time data capture"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"CONTROL",  "id":20565,   "ctx":"initandlisten","msg":"Now exiting"}
{"t":{"$date":"2021-09-05T11:18:49.027+08:00"},"s":"I",  "c":"CONTROL",  "id":23138,   "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":48}}

exitCode 48,这个问题一般有几种可能:

  1. 服务被意外关闭(不正确的方式关闭服务):
    我们到数据库目录下,发现有个mongodb.lock,lock是什么?锁,有了一把锁,如果之前没有被关闭,自然是无法启动,干掉它!
sudo rm -f mongodb.lock
  1. 日志文件没有权限,我们打开官方教程,按照他的介绍,是需要给日志文件和数据库设置权限的!!!很重要
sudo chown `whoami` /var/lib/mongo     # Or substitute another user
sudo chown `whoami` /var/log/mongodb   # Or substitute another user

这俩个搞定基本没问题咯
就可以在日志中看到这么一句话(令人兴奋):Waiting for connections!!!

{"t":{"$date":"2021-09-05T11:58:45.674+08:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
{"t":{"$date":"2021-09-05T11:58:45.675+08:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"127.0.0.1"}}
{"t":{"$date":"2021-09-05T11:58:45.675+08:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}

当然界面也会提醒你成功啦,主要是要学会通过日志得到答案(这也是解决问题的最好的方式)。本次介绍就结束啦,祝你和我一样完结撒花***~
当然我是新手写的文章,难免会有错误,希望你们见谅,发现我当积极改正哈~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值