MongoDB 环境安装

  • 在Windows上安装MongoDB

    要在Windows上安装MongoDB,请首先从https://www.mongodb.com/try/download/community下载最新版本的MongoDB 。
    win install1
    现在安装下载的文件,默认情况下,它将安装在文件夹D:\mongodb\中。MongoDB需要一个数据文件夹来存储其文件。MongoDB数据目录的默认位置是D:\mongodb\data\,安装的时候会帮你自动创建。
    第一步点击下一步
    win install1
    第二步作为网络服务运行
    win install1
    第三步取消不必要的默认安装
    win install1
    第四步开始安装
    win install1
    第五步安装过程
    win install1
    第六步安装完成
    win install1
    可能由于版本不同,安装过程的界面也不同,但大致安装过程都大同小异。
    安装完成,可能您发现启动不了服务,可以参详这里的解决方法:https://www.bilibili.com/read/cv5236865/
  • 在CentOS上安装MongoDB

    第一步下载,解压缩,移动到安装目录
    
    $ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.0.tgz  # 下载
    $ tar -zxvf  mongodb-linux-x86_64-rhel70-4.4.0.tgz                             # 解压缩
    $ cd mongodb-linux-x86_64-rhel70-4.4.0                                         # 进入目录
    $ mkdir /usr/local/mongodb                                                     # 新建安装目录
    $ cp -R * /usr/local/mongodb                                                   # 复制当前目录下所有文件到安装目录
    $ PATH=/usr/local/mongodb/bin && export PATH                                   # 添加到环境变量 并导出
    
    linux install1
    第二步新建配置文件,日志文件,和数据库目录
    
    $ mkdir /usr/local/mongodb/data  # 新建数据库目录
    $ mkdir /usr/local/mongodb/logs  # 新建日志文件目录
    $ touch  /usr/local/mongodb/logs/MongoDB.log  # 新建日志文件
    $ touch /usr/local/mongodb/mongod.cfg   #新建配置文件  
    
    配置文件输入如下内容保存:
    
    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # Where and how to store data.
    storage:
      dbPath: /usr/local/mongodb/data
      journal:
        enabled: true
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path:  /usr/local/mongodb/logs/MongoDB.log
    
    # network interfaces
    net:
      port: 27017
      bindIp: 127.0.0.1
    
    检查mongod安装
    
    [root@www mongodb-linux-x86_64-rhel70-4.4.0]# mongo --version   
    MongoDB shell version v4.4.0
    Build Info: {
        "version": "4.4.0",
        "gitVersion": "563487e100c4215e2dce98d0af2a6a5a2d67c5cf",
        "openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
        "modules": [],
        "allocator": "tcmalloc",
        "environment": {
            "distmod": "rhel70",
            "distarch": "x86_64",
            "target_arch": "x86_64"
        }
    }
    
    第三步启动mongod服务,进入mongo客户端
    
    [root@www mongodb-linux-x86_64-rhel70-4.4.0]# mongod -f /usr/local/mongodb/mongod.cfg --fork # 启动服务
    about to fork child process, waiting until server is ready for connections.
    forked process: 2527
    child process started successfully, parent exiting
    [root@www mongodb-linux-x86_64-rhel70-4.4.0]# lsof -i:27017  #检查端口监听
    COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    mongod  2527 root   11u  IPv4  22548      0t0  TCP localhost:27017 (LISTEN)
    [root@www mongodb-linux-x86_64-rhel70-4.4.0]# mongo  # 进入mongo客户端
    MongoDB shell version v4.4.0
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("39eda7d8-b6e6-4e46-9d2f-0b64f777a1c8") }
    MongoDB server version: 4.4.0
    Welcome to the MongoDB shell.
    For interactive help, type "help".
    For more comprehensive documentation, see
      https://docs.mongodb.com/
    Questions? Try the MongoDB Developer Community Forums
      https://community.mongodb.com
    ---
    The server generated these startup warnings when booting: 
            2020-08-26T09:51:32.901+08:00: ***** SERVER RESTARTED *****
            2020-08-26T09:51:33.580+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
            2020-08-26T09:51:33.580+08:00: You are running this process as the root user, which is not recommended
            2020-08-26T09:51:33.580+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
            2020-08-26T09:51:33.580+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
            2020-08-26T09:51:33.580+08:00: Soft rlimits too low
            2020-08-26T09:51:33.580+08:00:         currentValue: 1024
            2020-08-26T09:51:33.580+08:00:         recommendedMinimum: 64000
    ---
    ---
            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()
    ---
    > 
    
    至此MongoDB在CentOS安装完毕。