下面以常见环境为例,系统介绍 MongoDB 的安装与基础配置(以 MongoDB 6.x / 7.x 为主,社区版 Community Server)。
# 导入公钥
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
# 添加源
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# 安装
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
检查状态:
sudo systemctl status mongod
常见配置项:
storage:
dbPath: /var/lib/mongodb
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
修改后重启:
sudo systemctl restart mongod
sudo yum install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
下载安装包
https://www.mongodb.com/try/download/community
安装时选择:
默认数据目录:
C:\data\db
启动:
net start MongoDB
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb/brew/mongodb-community
mongo
# 或新版本
mongosh
查看数据库:
show dbs
use admin
db.createUser({
user: "root",
pwd: "YourPassword",
roles: ["root"]
})
security:
authorization: enabled
重启服务后使用:
mongosh -u root -p YourPassword --authenticationDatabase admin
| 目录 | 说明 |
|---|---|
| /var/lib/mongodb | 数据文件 |
| /var/log/mongodb | 日志 |
| /etc/mongod.conf | 配置文件 |
1. 端口被占用
netstat -tunlp | grep 27017
2. 无法远程连接
bindIp如果你需要:
告诉我你的使用场景即可。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。