sudo apt-get -y install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/6.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt-get update
# 安装 最终稳定版本
sudo apt-get install -y mongodb-org
# 安装 指定版本
sudo apt-get install -y mongodb-org=6.0.2 mongodb-org-database=6.0.2 mongodb-org-server=6.0.2 mongodb-mongosh=6.0.2 mongodb-org-mongos=6.0.2 mongodb-org-tools=6.0.2
# 以上两条安装命令 按照场景执行其中之一
# 可选择的尽管您可以指定MongoDB的任何可用版本,
# 但apt-get会在更新版本可用时升级包。为了防止意外升级,
# 您可以将软件包固定在当前安装的版本:
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-mongosh hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
# 如不需 固定版本 请忽略 这条配置
# 启动 mongod
sudo systemctl start mongod
# 如果您看到如下报错信息:
# Failed to start mongod.service: Unit mongod.service not found.
# 先执行如下命令:
sudo systemctl daemon-reload
# 在执行如下命令:
sudo systemctl start mongod
# 停止 mongod
sudo systemctl stop mongod
# 设置开机自动启动mongod
sudo systemctl enable mongod
# 禁用开机自动启动mongod
sudo systemctl disable mongod
# 控制台连接
mongosh
但此时远程客户端无法访问,例如使用navicat访问会如有如下报错
可以通过修改配置文件的方式解决:
sudo vim /etc/mongo.conf
# 将 bindIp: 127.0.0.1 修改为 bindIp: 0.0.0.0
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
保存后重启 mongod
sudo systemctl restart mongod
navicat 测试连接成功