这就是我的工作。
1。选择版本:
2。安装和配置
我使用的是 Ubuntu 20.04,我想安装 mongodb Community ver 2.6.12
安装:
$ cd ~/Downloads
$ curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.12.tgz
$ tar -zxvf mongodb-linux-x86_64-2.6.12.tgz
$ mv mongodb-linux-x86_64-2.6.12 2.6.12
$ cd /opt && mkdir -p mongodb
$ cd ~/Downloads
$ cp -R 2.6.12 /opt/mongodb
$ cp /mongod.conf /opt/mongodb/2.6.12/
配置:
版本 3.0 不同,所以我用谷歌搜索了它。这就是我所拥有的。
systemLog:
destination: file
path: "/var/log/mongodb/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27018
setParameter:
enableLocalhostAuthBypass: false
在这个文件中,您需要将 systemLog.path、net.port 和 net.bindIp 更改为您自己的。一件重要的事情是我们不能使用相同的端口同时运行 2 个版本。所以这里我为旧版本设置了27018。
把它放在任何你想要的地方,我做到了:
$ cp ~/mongod.conf /opt/mongodb/2.6.12/
不要运行这个,因为它会覆盖当前的 mongodb 版本命令
$ export PATH=/opt/mongodb/2.6.12/bin:$PATH
创建数据和日志目录并设置权限。
$ mkdir -p /data/db
$ mkdir -p /var/log/mongodb
跑步:
现在你可以用命令启动 2.6 版本了。
$ /opt/mongodb/2.6.12/bin/mongod --fork --dbpath /data/db --config /opt/mongodb/2.6.12/mongod.conf
它太长了,不是吗?所以用别名使它更短。
$ sudo nano ~/.bash_profile
or
$ sudo nano ~/.bashrc
并将其添加到文件中
alias mongo26="/opt/mongodb/2.6.12/bin/mongo --port 27018"
alias mongoRestore26="/opt/mongodb/2.6.12/bin/mongorestore --port 27018"
alias mongoDump26="/opt/mongodb/2.6.12/bin/mongodump --port 27018"
alias startMongo26="/opt/mongodb/2.6.12/bin/mongod --fork --dbpath /data/db --config /opt/mongodb/2.6.12/mongod.conf"
alias stopMongo26="/opt/mongodb/2.6.12/bin/mongod --dbpath /data/db --shutdown"
alias nanoMongo="nano /opt/mongodb/2.6.12/mongod.conf"
alias tailMongo="tail -f /var/log/mongodb/mongod.log"
alias tailMongo26="tail -f /var/log/mongodb/mongodb.log"
应用这些别名
$ source ~/.bash_profile
or
$ source ~/.bashrc
现在我们可以简单地启动 mongodb 2.6 版了
$ startMongo26
about to fork child process, waiting until server is ready for connections.
forked process: 1049481
child process started successfully, parent exiting
运行 shell 版本 2.6
$ mongo26
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27018/test
>
运行shell当前版本
$ mongo
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3ae6094f-86dc-4741-aa72-1e48efc5273a") }
MongoDB server version: 4.4.3
>
3.疑难解答
- 无法从
startMongo26 开始,请查看/var/log/mongodb/mongod.log 的日志
about to fork child process, waiting until server is ready for connections.
forked process: 1049890
ERROR: child process failed, exited with error number 100
在/var/log/mongodb/mongod.log查看日志
2021-02-26T15:51:13.493-0700 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
这是因为您当前的用户没有运行和写入此目录的权限。您可以将权限设置为 /data/db 或更改目录。我改成了~/datamongo
$ /opt/mongodb/2.6.12/bin/mongod --fork --dbpath ~/datamongo --config /opt/mongodb/2.6.12/mongod.conf
- 对
/var/log/mongodb/mongodb.log 上的错误执行相同操作
stopMongo26 无法关闭
There doesn't seem to be a server running with dbpath: /data/db
这样做:
$ mongo26
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27018/test
> use admin
switched to db admin
> db.shutdownServer()
2021-02-26T15:45:19.738-0700 DBClientCursor::init call() failed
server should be down...
2021-02-26T15:45:19.742-0700 trying reconnect to 127.0.0.1:27018 (127.0.0.1) failed
2021-02-26T15:45:19.742-0700 warning: Failed to connect to 127.0.0.1:27018, reason: errno:111 Connection refused
2021-02-26T15:45:19.742-0700 reconnect 127.0.0.1:27018 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27018 (127.0.0.1), connection attempt failed
按照我的指示一步一步做,你会没事的。祝你好运,享受!!!