Ubuntu20系统上离线安装MongoDB

Ubuntu20系统上离线安装MongoDB

准备工作:下载安装包及依赖​

下载MongoDB二进制包​

在联网环境中访问MongoDB官网,选择以下配置:

下载地址:https://www.mongodb.com/try/download/community

  • Version:需与目标系统兼容(如Ubuntu 20.04)
  • Platform:Linux x86_64
  • Package:TGZ格式(例如mongodb-linux-x86_64-ubuntu2004-6.0.12.tgz)

​传输安装包到目标服务器

安装与配置​

解压安装包​

登录目标服务器后执行:

bash 复制代码
cd /tmp
tar -zxvf mongodb-linux-*.tgz -C /opt  # 解压到/opt目录
mv /opt/mongodb-linux-* /opt/mongodb   

配置环境变量​

将MongoDB二进制路径加入系统环境变量:

bash 复制代码
echo 'export PATH=/opt/mongodb/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

​创建数据与日志目录

bash 复制代码
sudo mkdir -p /data/db              # 数据存储目录
sudo mkdir -p /var/log/mongodb      # 日志目录
sudo chown -R `whoami` /data/db /var/log/mongodb  # 赋权当前用户

启动MongoDB服务

创建配置文件/etc/mongod.conf:

yaml 复制代码
systemLog:
  destination: file
  path: /var/log/mongodb/mongod.log
  logAppend: true
storage:
  dbPath: /data/db
  journal:
    enabled: true
net:
  bindIp: 0.0.0.0
  port: 27017
processManagement:
  fork: true
security: # 启用身份验证
  authorization: enabled

启动服务

bash 复制代码
mongod -f /etc/mongod.conf

重启服务

bash 复制代码
pkill mongod
mongod -f /etc/mongod.conf

查看进程

bash 复制代码
sudo lsof-i:27017

创建用户

​创建管理员用户

bash 复制代码
mongo
use admin
db.createUser({
  user: "admin",
  pwd: "password",     
  roles: ["root"],
  mechanisms: ["SCRAM-SHA-256"], 
  passwordDigestor: "server" 
})

创建数据库用户

bash 复制代码
const USER="user";
const PASSWORD= "password"; //Randomly generated password

db.createUser(
   {
     user: USER,
     pwd: PASSWORD,
     
     roles: [{"role":"dbOwner","db":"zking"}],
    /* All built-in Roles 
     Database User Roles: read|readWrite
     Database Admin Roles: dbAdmin|dbOwner|userAdmin
     Cluster Admin Roles: clusterAdmin|clusterManager|clusterMonitor|hostManager
     Backup and Restoration Roles: backup|restore
     All-Database Roles: readAnyDatabase|readWriteAnyDatabase|userAdminAnyDatabase|dbAdminAnyDatabase
     Superuser Roles: root 
    */
    
     // authenticationRestrictions: [ {
     //       clientSource: ["192.0.2.0"],
     //       serverAddress: ["198.51.100.0"]
     // } ],
     mechanisms: ["SCRAM-SHA-256"], 
     passwordDigestor: "server",
   }
)

const URI= mb.getUserConnectionURI({user:USER, password: PASSWORD, authDb: "zking"});
mb.writeTextToClipboardAndNotify(URI);

print("URI for "+USER,":", URI)

备份和还原

更新MongoDB工具链

bash 复制代码
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2004-x86_64-100.7.4.deb
sudo dpkg -i mongodb-database-tools-*.deb

还原数据库

确保数据路径包含有效数据文件(.bson和 .json),且当前用户有读取权限:

bash 复制代码
sudo chmod -R 755 /data/project/zking

还原

bash 复制代码
#写入的用户密码不要使用特殊字符
mongorestore -u admin -p 'password' --authenticationDatabase admin  --db zking /data/zking

扩展场景命令​

  1. 仅恢复特定集合(如 users)
bash 复制代码
mongorestore -u admin -p 'password' --authenticationDatabase admin \
  --db zking --nsInclude "zking.users" \
  /data/zking
  1. 从压缩备份恢复(若备份时使用了 --gzip)
bash 复制代码
mongorestore -u admin -p 'password' --authenticationDatabase admin \
  --db zking --gzip \
  /data/zking
  1. 远程恢复且不覆盖现有数据
bash 复制代码
mongorestore --host 192.168.1.2 -u admin -p 'password' \
  --authenticationDatabase admin --db zking \
  /data/zking
相关推荐
小马哥编程1 小时前
【软考架构】第6章 数据库基本概念
数据库·oracle·架构
自学也学好编程1 小时前
【数据库】PostgreSQL详解:企业级关系型数据库
数据库·postgresql
小猪写代码1 小时前
解释一下,Linux,shell,Vmware,Ubuntu,以及Linux命令和shell命令的区别
linux·ubuntu
源代码杀手2 小时前
大模型微调训练资源占用查询:Windows 10 查看 NVIDIA 显卡GPU状态教程(替代 Ubuntu 下 watch nvidia-smi)
linux·windows·ubuntu
.Eyes2 小时前
OceanBase 分区裁剪(Partition Pruning)原理解读
数据库·oceanbase
MrZhangBaby3 小时前
SQL-leetcode— 2356. 每位教师所教授的科目种类的数量
数据库
一水鉴天4 小时前
整体设计 之定稿 “凝聚式中心点”原型 --整除:智能合约和DBMS的在表层挂接 能/所 依据的深层套接 之2
数据库·人工智能·智能合约
翔云1234564 小时前
Python 中 SQLAlchemy 和 MySQLdb 的关系
数据库·python·mysql
Java 码农4 小时前
nodejs mongodb基础
数据库·mongodb·node.js