mac 安装 mongodb

mac 安装 mongodb

安装

  1. 官网下载
    mongodb 官网:https://www.mongodb.com/
    点击 右上角 Download 下载对应系统的安装包;我下载的OSX OSX 10.7+64-bit w/SSL x64 压缩包

https://www.mongodb.com/download-center?ct=false#community

https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-3.4.7.tgz

下载后解压 tgz包

  1. 安装
    cd ~/Applications
    sudo mkdir mongodb
    然后把刚下载并解压的 mongodb-osx-x86_64-3.4.7 中的所有文件复制到 /Applications/mongodb中

    export PATH=~/Applications/mongodb/bin:$PATH

    进入电脑文件跟目录 cd /
    创建mongodb数据默认存放文件夹
    sudo mkdir -p /data/db
    sudo chown -R [本机用户名] /data

运行

回到 /Applications/mongodb 文件夹 cd bin 进入 bin文件夹
./mongod
我刚安装完之后这样是直接启动成功了,但以后再运行时有报错,启动不起来了
[initandlisten] shutting down with code:100
这时必须要加管理员权限 sudo 才能正常启动
sudo ./mongod
启动成功

2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten] MongoDB starting : pid=3949 port=27017 dbpath=/data/db 64-bit host=bogon
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten] db version v3.4.7
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten] git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 0.9.8zh 14 Jan 2016
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten] allocator: system
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten] modules: none
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten] build environment:
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten]     distarch: x86_64
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2017-08-11T11:00:00.611+0800 I CONTROL  [initandlisten] options: {}
2017-08-11T11:00:00.612+0800 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-08-11T11:00:00.613+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3584M,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-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] 
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] 
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] 
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2017-08-11T11:00:01.483+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-08-11T11:00:01.485+0800 I NETWORK  [thread1] waiting for connections on port 27017

可以看到打印的信息
默认端口 27017
浏览器看一看下
http://localhost:27017/

It looks like you are trying to access MongoDB over HTTP on the native driver port.

用一下看看

Mongo Shell
再打开一个终端窗口
cd ~/Applications/mongodb/bin/

./mongo
成功

MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Server has startup warnings: 
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] 
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] 
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] 
2017-08-11T11:00:01.431+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> 

附上官方文档连接
doc:
https://docs.mongodb.com/manual/mongo/

输入
show dbs
命令可以显示所有数据的列表

> show dbs
admin    0.000GB
local    0.000GB

新建数据库
use DATABASE_NAME

test_db
再输入
db

> use test_db
switched to db test_db
> db
test_db

但你再次输入 show dbs 时发现并没有新建的数据库
我们需要向新建数据控中存入些数据才能显示

db.COLLECTION_NAME.insert({key1:value1,key2:value2})

COLLECTION_NAME 就是集合名(关系型数据库的表名)
其实这一步叫做插入文档

> db.mytable.insert({"name":"张三"})
WriteResult({ "nInserted" : 1 })
> 

查询

> db.mytable.find()
{ "_id" : ObjectId("598d244c97268b19c856fdbc"), "name" : "张三" }
> 

其他更精深的操作大家可以看下官方文档 或其他教程
这里有个非常详细的教程
http://www.runoob.com/mongodb/mongodb-tutorial.html
接下来我忍不了要找个可视化工具操作

可视化工具

我找了好几个软件,github上的也有
最终选定了这几个

  1. 官方的 MongoDB Compass
    这算是最好用的
    https://www.mongodb.com/download-center?ct=false#compass

《mac 安装 mongodb》 image.png

  1. Studio 3T 这个也不错 不过好像14天免费>收费
    https://studio3t.com/

《mac 安装 mongodb》 image.png

  1. Robo 3T
    https://robomongo.org/download
    这个其实和上面第二个是一家

《mac 安装 mongodb》 image.png

    原文作者:正为疯狂
    原文地址: https://www.jianshu.com/p/813f1682e106
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞