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 端口未被占用。

相关推荐
桑榆4161 天前
双系统(Windows + Ubuntu)安装流程
linux·windows·ubuntu
ltl1 天前
实时 OS 巡礼:VxWorks、QNX、Zephyr 与 PREEMPT_RT
linux
我是谁??1 天前
Ubuntu22.04更换清华源
linux·运维·服务器
Shell运维手记1 天前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github
测试运维日常笔记1 天前
Linux系统安装配置TigerVNC服务器完整指南
linux·运维·服务器
雾时之林1 天前
Linux----cron定时服务
linux·运维·服务器
sunly_1 天前
TypeScript:3、类型声明与类型推断
javascript·ubuntu·typescript
strength_zhou20131 天前
Python检查MongoDB索引列中的字段是否存在
python·mongodb
wengqidaifeng1 天前
1.从“会敲命令”到理解 Linux:15 个基础指令、文件树与命令执行的本质
linux·运维·服务器·ubuntu
奔跑的架构师1 天前
[A-49]ARMv9/v8-PSCI接口规范与工作流程简介
android·linux·arm开发·arm