Ubuntu安装Mongodb

一、安装 MongoDB Community Edition​

1. 更新系统并安装依赖
bash 复制代码
sudo apt update
sudo apt upgrade -y
sudo apt install -y gnupg curl
2. 导入 MongoDB 公钥
bash 复制代码
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
3. 添加 MongoDB 仓库
bash 复制代码
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] \
https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
4. 安装 MongoDB
bash 复制代码
sudo apt update
sudo apt install -y mongodb-org

​二、启动与配置 MongoDB​

1. 启动服务并设置开机自启
bash 复制代码
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod  # 检查状态应为active (running)
2. 修改配置文件(可选)

编辑 /etc/mongod.conf 以允许远程连接或调整性能:

bash 复制代码
net:
  bindIp: 0.0.0.0  # 允许所有IP访问
  port: 27017
storage:
  journal:
    enabled: true  # 启用日志

重启服务生效:

bash 复制代码
sudo systemctl restart mongod

​三、安全配置​

1. 创建管理员用户
bash 复制代码
mongosh
use admin
db.createUser({
  user: "nandu",
  pwd: "nandu123",
  roles: ["root"]
})
exit
2. 启用认证

修改 /etc/mongod.conf

bash 复制代码
security:
  authorization: enabled

重启服务:

bash 复制代码
sudo systemctl restart mongod

​四、验证安装​

1. 连接测试
bash 复制代码
mongosh -u nandu -p nandu123 --authenticationDatabase admin

执行基础命令:

javascript 复制代码
db.version()  // 查看版本
show dbs      // 显示数据库
2. 远程连接(如配置了bindIp)

使用工具如 mongoshNavicat 连接 服务器IP:27017,输入认证信息


​常见问题解决​

  1. ​依赖错误​ ​:若安装失败,尝试先安装 libssl1.14:

    bash 复制代码
    echo "deb http://security.ubuntu.com/ubuntu focal-security main" | sudo tee /etc/apt/sources.list.d/focal-security.list
    sudo apt update
    sudo apt install libssl1.1
  2. ​端口冲突​ ​:确保 27017 端口未被占用。

相关推荐
Yana.nice14 小时前
Linux 只保留 30 天内日志(find命令删除日志文件)
linux·运维·chrome
DFT计算杂谈18 小时前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
Zhang~Ling19 小时前
从 fopen 到 struct file:从零开始拆解 Linux 文件 I/O
linux·运维·服务器
DeeplyMind19 小时前
Linux 深入 per-VMA lock:Linux 缺页路径如何摆脱 mmap_lock
linux·per-vma lock
爱写代码的森19 小时前
蒙三方库 | harmony-utils之FileUtil文件重命名与属性查询详解
linux·运维·服务器·华为·harmonyos·鸿蒙·huawei
XMAIPC_Robot20 小时前
软硬协同实时控制|RK3588业务调度+FPGA硬件时序,ethercat实现半导体设备微秒级响应(125us)
linux·arm开发·人工智能·fpga开发
重生的黑客21 小时前
Linux 进程优先级、切换与调度:从孤儿进程到 O(1) 调度模型
linux·运维·服务器·进程优先级·nice
骑上单车去旅行1 天前
MD5校验对比脚本
linux·服务器·windows
平生幻1 天前
Linux 常用命令
linux
ShirleyWang0121 天前
让headlamp控制台能访问
linux·服务器·python·k8s·k3s