在Ubuntu上配置MongoDB存储涉及几个关键步骤,包括安装、配置、启动和验证。以下是详细的步骤:
sudo apt update
sudo apt install mongodb
sudo dpkg -i mongodb-org_4.4.2_amd64.deb
sudo nano /etc/mongod.conf
bind_ip
:设置为 0.0.0.0
以允许从任意IP地址连接。port
:设置为 MongoDB 服务器监听的端口号(默认为27017)。dbpath
:指定MongoDB数据存储路径,例如 /var/lib/mongodb
。logpath
:指定MongoDB日志文件路径,例如 /var/log/mongodb/mongod.log
。security.authorization
:启用身份验证(如果需要)。例如:
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
保存并关闭文件。
重启MongoDB服务以使配置生效:
sudo systemctl restart mongod
sudo systemctl start mongod
sudo systemctl stop mongod
sudo systemctl restart mongod
sudo systemctl status mongod
mongo
show dbs
use mydb
show collections
通过以上步骤,您可以在Ubuntu上成功安装、配置、启动和验证MongoDB。请根据您的实际需求调整配置,例如设置身份验证、指定数据存储路径等。