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

相关推荐
未济2 天前
linux 配置环境变量
linux
傲世仙尊2 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
_艾伦 耶格尔.2 天前
进程间通信
linux
Liuqy-052 天前
Linux IO编程——静态库、动态库
linux
彧azz2 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅2 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK2 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid
琥珀色糖2 天前
SimlpeHttp
linux·服务器
qeen872 天前
【Linux】操作系统之进程介绍(二)
linux·笔记·学习·进程
Zenova EdgeOS2 天前
Linux ss 工业边缘网络分析实战
linux·python·边缘计算·工业网关