# 二、MongoDB 单机部署

参考:Install MongoDB Community Edition (opens new window)

# 1. MacOS

# 1.1 下载 MongoDB

  • 前往 MongoDB (opens new window) 下载 tar 包

    image-20210813101948139

  • 解压

    tar -zxvf mongodb-macos-x86_64-4.2.15.tgz
    
  • 进入 mongodb 目录

    cd mongodb-macos-x86_64-4.2.15
    
  • 配置环境变量

    sudo cp bin/*  /usr/local/bin/
    
    sudo ln -s  bin/* /usr/local/bin/
    
  • 创建 mongodb 所需的目录

    # 数据目录
    mkdir -p /usr/local/var/mongodb
    # 日志目录
    mkdir -p /usr/local/var/log/mongodb
    # 授权
    sudo chown 755 /usr/local/var/mongodb
    sudo chown 755 /usr/local/var/log/mongodb
    
  • 启动 mongod

    mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
    
  • 查看 mongod 是否启动成功

    ps aux | grep -v grep | grep mongod
    
  • 进入 mongo

    mongo
    

    输出:

    ➜  ~ mongo
    MongoDB shell version v4.2.15
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("c4854ee8-8399-4e11-b7cb-d601bb23b3ec") }
    MongoDB server version: 4.2.12
    Server has startup warnings:
    2021-08-12T11:49:07.389+0800 I  CONTROL  [initandlisten]
    2021-08-12T11:49:07.389+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2021-08-12T11:49:07.389+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2021-08-12T11:49:07.389+0800 I  CONTROL  [initandlisten]
    ---
    Enable MongoDB's free cloud-based monitoring service, which will then receive and display
    metrics about your deployment (disk utilization, CPU, operation statistics, etc).
    
    The monitoring data will be available on a MongoDB website with a unique URL accessible to you
    and anyone you share the URL with. MongoDB may use this information to make product
    improvements and to suggest MongoDB products and deployment options to you.
    
    To enable free monitoring, run the following command: db.enableFreeMonitoring()
    To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
    ---
    
    > 
    
  • 进行 show dbs 查看默认库

    > show dbs
    admin   0.000GB
    config  0.000GB
    local   0.000GB
    

# 1.2 下载 MongoDB Compass

# 2. Linux

CentOS 7

  • 安装相关依赖

    yum install libcurl openssl
    
  • 下载 mongodb tar 包

    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.15.tgz
    
  • 解压

    tar -zxvf mongodb-linux-x86_64-rhel70-4.2.15.tgz
    
  • 进入 mongodb 目录

    cd mongodb-linux-x86_64-rhel70-4.2.15
    
  • 配置环境变量

    sudo cp mongodb-linux-x86_64-rhel70-4.2.15/bin/* /usr/local/bin/
    
    sudo ln -s  mongodb-linux-x86_64-rhel70-4.2.15/bin/* /usr/local/bin/
    
  • 创建 mongodb 所需的目录

    # 数据目录
    mkdir -p /usr/local/var/mongodb
    # 日志目录
    mkdir -p /usr/local/var/log/mongodb
    
  • 启动 mongod

    mongod --config /usr/local/etc/mongod.conf
    
  • 查看 mongod 是否启动成功

    ps aux | grep -v grep | grep mongod
    
  • 进入 mongo

    mongo
    

# 3. Docker

# 下载 MongoDB:

  • 下载 MongoDB 的官方 Docker 镜像

    docker pull mongo:4
    
  • 查询下载的镜像

    docker images
    
  • 启动 MongoDB

    docker run --name mymongo -v $HOME/mymongo/data:/data/db -p 27017:27017 -d mongo:4 --port 27017
    
    • docker run mongo:4: 以 mongo:4 为镜像启动一个 docker 容器
    • --name mymongo: 指定容器名称为 mymongo
    • -v /mymongo/data:/data/db: 挂载容器数据目录,将本地目录 /mongodb/data 挂载到容器中的 /data/db 目录
    • -p: 指定端口映射
    • -d: 后台启动容器
    • --port: 指定 MongoDB 启动端口
  • 查看容器状态

    docker ps
    
  • 查看日志

    docker logs mymongo
    
  • 启动 mongo shell

    docker exec -it mymongo mongo
    

    docker exec -it mymongo: 以交互式启动 mymongo 容器

    mongo: 该命名会在容器中运行,启动 mongo shell

# 下载 MongoDB Express

  • 下载 mongo-express 镜像

    docker pull mongo-express
    
  • 运行 mongo-express 容器

    docker run --link mymongo:mongo -p 8081:8081 mongo-express
    
上次更新: 12/9/2021, 11:14:52 AM