最近因为项目原因,要搭建分布式爬虫,加上实验室更换了电脑,重装mongo,遇到一系列莫名奇妙的之前没遇到的错误,mark下。
之前安装的方式一直用的都是:
sudo apt-get install mongodb
安装没问题,而且启动命令也默认添加了环境变量配置文件中,但是启动mongo启动提示出错:
liangshuai@galaxy:~$ mongo
MongoDB shell version: 2.6.10
connecting to: test
2018-12-28T21:28:48.053+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2018-12-28T21:28:48.053+0800 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
阅读了大量博客,提到的大概有以下几种解决方法:
1.因为异常退出导致生成mongo.lock文件,删除即可
2.端口被占用,更改端口即可
基于以上的尝试都没有解决问题,我的解决办法:
1.卸载mongodb
sudo apt-get purge mongodb
2.添加mongo秘钥
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
3.为mongodb创建列表文件
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
4.更新软件包
sudo apt-get update
5.安装最新版本mongo
sudo apt-get install -y mongodb-org
输入mongo运行,成功!然后按照以下步骤进行配置用户和登录密码(记住以后每次查看数据库信息均需要先用账号和密码登录数据库)
liangshuai@galaxy:~$ mongo
MongoDB shell version v3.4.18
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.18
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-12-28T21:55:39.484+0800 I STORAGE [initandlisten]
2018-12-28T21:55:39.484+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-12-28T21:55:39.484+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
6.使用管理员方式添加登录账号和密码
> use admin
switched to db admin
> db.createUser({user:"allen",pwd:"125225",roles:["root"]})
Successfully added user: { "user" : "allen", "roles" : [ "root" ] }
7.登录验证,成功
> db.auth("allen","125225")
1
NOTE:
遇到这几种报错的朋友不妨卸载后按此方法重新再安装下。