Mac MongoDB 安装

字数 1255阅读 5251

Mac MongoDB安装

一、安装

MongoDB的安装方法有很多,本文以官方文档中的两种方法为准。

1. 手动安装MongoDB Edition

​ 也可以使用终端下载:

curl -O https://fastdl.mongodb.org/osx-ssl/mongodb-osx-ssl-x86_64-3.4.10.tgz

  • 解压到安装目录

    默认目录为/usr/local,解压完成后修改文件夹名称为(mongodb)删掉后面的版本号,这一步也可以使用终端来完成:

    # 1. 解压文件
    tar -zxvf mongodb-osx-ssl-x86_64-3.4.10.tgz
    
    # 2. 新建mongodb文件夹
    mkdir -p mongodb
    
    # 3. 将mongodb拷贝到新文件夹
    cp -R -n mongodb-osx-ssl-x86_64-3.4.10/ mongodb
    

  • 将安装目录添加到系统环境变量中

    在终端中输入:export PATH=/usr/local/mongodv/bin:$PATH
    如果你的mongodb不在此路径中安装,那么将上面path后面的路径替换为mongodb的安装路径。
    注意:这样添加环境变量的方式只在当前打开的终端中有用,要想永久配置该变量请使用下面方法:

    • 打开当前用户环境变量配置文件:在终端中输入sudo vi ~/.bash_profile
    • 在文档中加入你的安装路径export PATH=$PATH:/usr/local/mongodv/bin,保存即可。(英文输入状态下按字母i,进行编辑;按esc后输入:wq保存离开
    $  sudo vi ~/.bash_profile
    export PATH=$PATH:/usr/local/mongodv/bin
    ~
    ~
    ~
    
    • 终端中使用source .bash_profile更新配置,使配置文件生效(此步骤可以用重启终端代替)

    • 使用echo $PATH查看当前系统环境变量

      $ echo $PATH
      /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/mongodb/bin
      

2. 使用Homebrew安装MongoDB Binaries

  • 首先更新你的brew

    brew update

  • 安装MongoDB

    使用终端:

    brew install mongodb
    
    # TLS / SSL支持
    brew install mongodb --with-openssl
    
    # 最新开发版本
    brew install mongodb --devel
    

二、运行MongoDB

  • 在运行MongoDB之前需要先启动mongod进程

    • 创建数据库存储目录/data/dbsudo mkdir -p /data/db,目录路径可以自定义

    • 启动mongod

      终端:

      mongod
      
      # 如果上一步自定义了存储路径则需要使用 --dbpath ,例如
      mongod --dbpath /Volumes/documents/mango/db
      
      # 如果没有创建全局路径 PATH,需要进入以下目录
      cd /usr/local/mongodb/bin && ./mongod
      

      一切正常会显示:

      Last login: Thu Nov 23 10:23:48 on ttys000
      promote:~ Keriy$ mongod --dbpath /Volumes/documents/mango/db
      2017-11-23T11:00:24.635+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1038 port=27017 dbpath=/Volumes/documents/mango/db 64-bit host=promote.cache-dns.local
      2017-11-23T11:00:24.635+0800 I CONTROL  [initandlisten] db version v3.4.10
      2017-11-23T11:00:24.635+0800 I CONTROL  [initandlisten] git version: 078f28920cb24de0dd479b5ea6c66c644f6326e9
      2017-11-23T11:00:24.635+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 0.9.8zh 14 Jan 2016
      2017-11-23T11:00:24.635+0800 I CONTROL  [initandlisten] allocator: system
      2017-11-23T11:00:24.635+0800 I CONTROL  [initandlisten] modules: none
      2017-11-23T11:00:24.635+0800 I CONTROL  [initandlisten] build environment:
      2017-11-23T11:00:24.635+0800 I CONTROL  [initandlisten]     distarch: x86_64
      2017-11-23T11:00:24.636+0800 I CONTROL  [initandlisten]     target_arch: x86_64
      2017-11-23T11:00:24.636+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/Volumes/documents/mango/db" } }
      2017-11-23T11:00:24.639+0800 I -        [initandlisten] Detected data files in /Volumes/documents/mango/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
      2017-11-23T11:00:24.640+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1536M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
      2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] 
      2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
      2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
      2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] 
      2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] 
      2017-11-23T11:00:25.256+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/Volumes/documents/mango/db/diagnostic.data'
      2017-11-23T11:00:25.257+0800 I NETWORK  [thread1] waiting for connections on port 27017
      

  • 运行MongoDB

    终端:

    mongo
    
    # 如果没有创建全局路径 PATH,需要进入以下目录
    cd /usr/local/mongodb/bin && ./mongo
    

    一切正常会显示:

    $ mongo
    MongoDB shell version v3.4.10
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 3.4.10
    Server has startup warnings: 
    2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] 
    2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] 
    2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] 
    2017-11-23T11:00:25.235+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
    > 
    
  • 停止进程

    终端中:ctrl + c

三、可视化管理工具

robomongo现在改名叫 Robo 3T 。

四、参考文档

官方文档

推荐阅读更多精彩内容