温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mongodb如何安装配置

发布时间:2026-11-07 19:41:50 来源:亿速云 阅读:95 作者:小樊 栏目:数据库

下面以常见环境为例,系统介绍 MongoDB 的安装与基础配置(以 MongoDB 6.x / 7.x 为主,社区版 Community Server)。


一、Linux(以 Ubuntu 为例)

1. 安装 MongoDB

# 导入公钥
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

2. 启动 MongoDB

sudo systemctl start mongod
sudo systemctl enable mongod

检查状态:

sudo systemctl status mongod

3. 基础配置(/etc/mongod.conf)

常见配置项:

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

二、CentOS / Rocky / AlmaLinux

sudo yum install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod

三、Windows 安装

  1. 下载安装包
    https://www.mongodb.com/try/download/community

  2. 安装时选择:

    • Complete
    • 勾选 Install MongoDB as Service
  3. 默认数据目录:

    C:\data\db
    
  4. 启动:

    net start MongoDB
    

四、macOS(Homebrew)

brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb/brew/mongodb-community

五、连接 MongoDB

mongo
# 或新版本
mongosh

查看数据库:

show dbs

六、开启用户认证(重要)

1. 创建管理员用户

use admin
db.createUser({
  user: "root",
  pwd: "YourPassword",
  roles: ["root"]
})

2. 修改配置文件

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
  • 检查防火墙
  • 云服务器需开放安全组

如果你需要:

  • Docker 安装 MongoDB
  • 集群 / 副本集配置
  • 生产环境优化
  • 特定操作系统版本

告诉我你的使用场景即可。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手