刘焕勇老师CMKG项目data_spider.py报错解决

本文记录了在Ubuntu20.04上安装MongoDB的过程,包括解决版本不匹配、服务启动失败、依赖冲突等问题。通过卸载并安装指定版本的pymongo,然后处理systemctl启动mongodb.service时遇到的各种错误,如System has not been booted with systemd in init system等,最终成功启动MongoDB服务。
摘要由CSDN通过智能技术生成

运行刘焕勇老师的CMKG的爬虫程序时:

报错1:

Collection' object is not callable. If you meant to call the 'insert' method on a 'Collection' object it is failing because no such method exists

原因:版本问题。解决:卸载然后装特定版本

pip uninstall pymongo
pip install pymongo==3.5.1

报错2:

localhost:27017: [Errno 111] Connection refused

原因:可能是没有启用mongodb服务

解决:Linux终端也就是terminal运行以下代码。win里叫法不同,叫命令行

systemctl start mongodb.service

解决systemctl start mongodb.service的报错:

所以之后很长一段路都真的是在解决systemctl start mongodb.service的报错

报错:

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

原因:systenctl没有安装或者无法正常使用,所以要下载它,输入以下代码

sudo apt install systemctl

然后报错:

E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.

E: 错误,pkgProblemResolver::Resolve 发生故障,这可能是有软件包被要求保持现状的缘故。 E: 无法更正依赖关系

(举例子)
下列软件包有未满足的依赖关系:
mentohust:i386 : 依赖: libpcap0.8:i386 但是它将不会被安装 或
libpcap0.9:i386 但无法安装它 或
libpcap1.0:i386 但无法安装它 或
libpcap1:i386 但无法安装它
wps-office : 依赖: libc6:i386 (>= 2.12) 但是它将不会被安装
依赖: libstdc++6:i386 (>= 4.5) 但是它将不会被安装
依赖: libfreetype6:i386 (>= 2.4) 但是它将不会被安装
依赖: libglu1-mesa:i386 但是它将不会被安装
依赖: libcups2:i386 但是它将不会被安装
依赖: libglib2.0-0:i386 但是它将不会被安装
依赖: libsm6:i386 但是它将不会被安装
依赖: libxrender1:i386 但是它将不会被安装
依赖: libfontconfig1:i386 但是它将不会被安装
推荐: ttf-mscorefonts-installer 但是它将不会被安装
————————————————
原文链接:https://blog.csdn.net/weixin_42156097/article/details/103805063

解决:

sudo aptitude install systemctl

若报错:没有"aptitude"这个命令:

sudo: aptitude: command not found

安装它!

sudo: aptitude: command not found

安装完成后,再次:

sudo aptitude install systemctl

然后系统会给出各个安装包解除依赖并安装的方案,系统询问:Accept this solution? [Y/n/q/?] ,输入Y。随后开始安装,问题解决!

再在终端运行

systemctl start mongodb.service

报错:

ERROR:systemctl:Unit mongodb.service could not be found.

解决:创建配置文件

sudo nano /etc/systemd/system/mongodb.service

报错:没有nano。下载nano

sudo aptitude install nano

然后创建配置文件,将以下内容粘贴复制至打开的文件

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

不同的 编辑器 保存文件的方法不同(49条消息) linux下nano修改并保存_发狂的蜗牛的博客-CSDN博客_nano 保存

nano:要保存所做的修改,按下Ctrl+O。想要退出,按下Ctrl+X。如果你退出前没有保存所做的修改,它会提示你是否要保存。如果不要,请按N,反之,则按Y。然后它会让你确认要保存的文件名,确认或修改后按Enter即可。

vi:首先需要按ESC键回到命令模式;然后输入:wq 保存文件并退出vi;最后回车即可。

再在终端运行mongodb/启动mongodb服务

systemctl start mongodb.service

报错:

ERROR:systemctl: Exec command does not exist: (ExecStart) /usr/bin/mongod
ERROR:systemctl: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ERROR:systemctl: Found 1 problems in /etc/systemd/system/mongodb.service
ERROR:systemctl: Now 1 executable paths were not found in the current environment.
ERROR:systemctl: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

为什么报错更多了,救命,试一下别的命令

sudo service mongod start

mongod: unrecognized service

sudo systemctl start mongodb

ERROR:systemctl: Exec command does not exist: (ExecStart) /usr/bin/mongod
ERROR:systemctl: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ERROR:systemctl: Found 1 problems in /etc/systemd/system/mongodb.service
ERROR:systemctl: Now 1 executable paths were not found in the current environment.
ERROR:systemctl: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

都不行,就是说上述命令无法正常启动服务

然后使用以下命令查看服务启动情况;

ps -aux | grep systemctl 

如果输出结果为:

root     24646  0.0  0.0   5196   728 pts/6    S+   01:14   0:00 grep keepalived

表示服务没运行起来。然而我的结果是没有结果,证明能运行systemctl吧

原因分析:运行以下命令查看服务状态;

systemctl status mongod.service

显示没有这个服务模块

于是我怀疑我是不是没有安装mongodb,所以我就去查看了LINUX版本并试图安装这个模块

查看LINUX版本

lsb_release -a

报错: 

 /usr/bin/sh: 16: lsb_release: not found  

试试别的,剩下俩都可以 

# cat /proc/version
Linux version 4.15.0-64-generic (buildd@lgw01-amd64-035) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)) #73~16.04.1-Ubuntu SMP Fri Sep 13 09:56:18 UTC 2019


# cat /etc/issue
Ubuntu 20.04.3 LTS \n \l

在Ubuntu 20.04 LTS上安装MongoDB

如何在Ubuntu 20.04 LTS上安装MongoDB - 技术教程 (yundongfang.com)

步骤1.首先,通过apt在终端中运行以下命令来确保所有系统软件包都是最新的。

sudo apt update
sudo apt upgrade
sudo apt gnupg
# sudo apt gnupg
E: Invalid operation gnupg

前两代码都没问题,但是第三个这个报错了,而且百度了一下之后

# sudo apt-get install gnupg
Reading package lists... Done
Building dependency tree
Reading state information... Done
gnupg is already the newest version (2.2.19-3ubuntu2.2).
gnupg set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

# sudo apt gnupg
E: Invalid operation gnupg

再试依然报错,猜想有没有可能只用一个命令确保软件包是最新的就行,那就试一下步骤2吧

步骤2.在Ubuntu 20.04上安装MongoDB

默认情况下,Ubuntu 20.04默认存储库中不提供最新版本的MongoDB。因此,您将需要在系统中添加官方的MongoDB存储库:

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add -
OK

OK?那就接着往下搞。使用以下命令添加MongoDB存储库

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse"| tee /etc/apt/sources.list.d/mongodb-org-4.2.list

 输出

deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse

看不懂deb啥是个啥意思,没报错就试一下往下搞吧

通过运行以下命令来更新系统并刷新现有存储库:

sudo apt update
sudo apt install mongodb-org

安装完成后,请启动MongoDB服务,并使用以下命令使其在重新启动时启动:

systemctl start mongodb.service

好的这次启动服务没有报错,那运行下边的代码

sudo systemctl start mongod      #立刻启动后面接的 unit
sudo systemctl enable mongod     #设置下次开机时,后面接的 unit 会被启动
sudo systemctl status mongod     #目前后面接的这个 unit 的状态,会列出有没有正在执行、开机时是否启动等信息

一路绿灯!最后一行查看状态的输出为:

mongod.service - MongoDB Database Server
    Loaded: loaded (/usr/lib/systemd/system/mongod.service, enabled)    #已开启
    Active: inactive (dead)    #防火墙已关闭

补充:service和systemctl是Linux 服务的命令管理的两种方式(49条消息) systemctl enable的作用_linux中systemctl详细理解及常用命令_weixin_39963341的博客-CSDN博客

步骤3.配置MongoDB。

MongoDB默认配置文件位于。默认情况下,每个用户都可以访问所有数据库并执行任何操作。/etc/mongod.conf

nano /etc/mongod.conf

打开文件后,添加以下行

security:
  authorization: enabled

使用键盘上的方向键上下移动,然后复制到对应位置。

保存并关闭文件后重新启动MongoDB服务以应用更改:

systemctl restart mongod

绿灯!那么到现在 在Ubuntu 20.04上 Mongodb就完成了

 再来试一下在终端运行mongodb/启动mongodb服务!

systemctl start mongodb.service

激动的心颤抖的手!可以啦!

现在让我再去运行一下我的代码,成功啦!如下图

总结:一共俩报错

1、Collection' object is not callable. If you meant to call the 'insert' method on a 'Collection' object it is failing because no such method exists

原因:版本问题。

解决:卸载然后装特定版本

2、localhost:27017: [Errno 111] Connection refused

原因:可能是没有启用mongodb服务

解决:启用服务,terminal里运行systemctl start mongodb.service。

后面的问题就变成了如何启用服务,然后发现问题在于我没有安装mongodb,所以进行安装使用,然后问题解决

补充:mongoDB操作Linux 平台安装 MongoDB | 菜鸟教程 (runoob.com)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值