下载二进制包

  1. 进入官网下载页面 https://www.mongodb.com/try/download/community
  2. 选择系统类型并复制下载链接(二进制压缩包)
  3. 进入ssh进行下载 cd /root && wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2204-7.0.3.tgz

解压并复制到指定目录

cd /root
tar -zxvf mongodb-linux-x86_64-ubuntu2204-7.0.3.tgz
mv mongodb-linux-x86_64-ubuntu2204-7.0.3 /usr/local/mongodb

修改配置文件

mkdir /usr/local/mongodb/log
mkdir /usr/local/mongodb/data

vim /usr/local/mongodb/bin/mongod.conf

processManagement:
   fork: false
net:
   bindIp: localhost
   port: 27017
storage:
   dbPath: /usr/local/mongodb/data
systemLog:
   destination: file
   path: "/usr/local/mongodb/log/mongod.log"
   logAppend: true

配置systemd

vim /etc/systemd/system/mongod.service

[Unit]
Description=MongoDB
After=network.target

[Service]
User=mongo
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongod.conf
Restart=always

[Install]
WantedBy=multi-user.target

下载mongosh

  1. 进入官网下载页面 https://www.mongodb.com/try/download/shell
  2. 选择系统类型并复制下载链接
  3. 进入系统安装

测试链接

mongosh

创建超级用户

db.createUser(
  {
    user: "myRootUser",
    pwd: "myRootPassword",
    roles: [ { role: "root", db: "admin" } ]
  }
)

验证创建用户

mongo -u myRootUser -p --authenticationDatabase admin