mac 安裝 mongodb
安裝
- 官網(wǎng)下載
mongodb 官網(wǎng):https://www.mongodb.com/
點(diǎn)擊 右上角 Download 下載對(duì)應(yīng)系統(tǒng)的安裝包;我下載的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包
-
安裝
cd ~/Applications
sudo mkdir mongodb
然后把剛下載并解壓的 mongodb-osx-x86_64-3.4.7 中的所有文件復(fù)制到 /Applications/mongodb中export PATH=~/Applications/mongodb/bin:$PATH
進(jìn)入電腦文件跟目錄
cd /
創(chuàng)建mongodb數(shù)據(jù)默認(rèn)存放文件夾
sudo mkdir -p /data/db
sudo chown -R [本機(jī)用戶名] /data
運(yùn)行
回到 /Applications/mongodb 文件夾 cd bin 進(jìn)入 bin文件夾
./mongod
我剛安裝完之后這樣是直接啟動(dòng)成功了,但以后再運(yùn)行時(shí)有報(bào)錯(cuò),啟動(dòng)不起來(lái)了
[initandlisten] shutting down with code:100
這時(shí)必須要加管理員權(quán)限 sudo 才能正常啟動(dòng)
sudo ./mongod
啟動(dòng)成功
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
可以看到打印的信息
默認(rèn)端口 27017
瀏覽器看一看下
http://localhost:27017/
It looks like you are trying to access MongoDB over HTTP on the native driver port.
用一下看看
Mongo Shell
再打開一個(gè)終端窗口
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
命令可以顯示所有數(shù)據(jù)的列表
> show dbs
admin 0.000GB
local 0.000GB
新建數(shù)據(jù)庫(kù)
use DATABASE_NAME
如
test_db
再輸入
db
> use test_db
switched to db test_db
> db
test_db
但你再次輸入 show dbs 時(shí)發(fā)現(xiàn)并沒(méi)有新建的數(shù)據(jù)庫(kù)
我們需要向新建數(shù)據(jù)控中存入些數(shù)據(jù)才能顯示
db.COLLECTION_NAME.insert({key1:value1,key2:value2})
COLLECTION_NAME 就是集合名(關(guān)系型數(shù)據(jù)庫(kù)的表名)
其實(shí)這一步叫做插入文檔
> db.mytable.insert({"name":"張三"})
WriteResult({ "nInserted" : 1 })
>
查詢
> db.mytable.find()
{ "_id" : ObjectId("598d244c97268b19c856fdbc"), "name" : "張三" }
>
其他更精深的操作大家可以看下官方文檔 或其他教程
這里有個(gè)非常詳細(xì)的教程
http://www.runoob.com/mongodb/mongodb-tutorial.html
接下來(lái)我忍不了要找個(gè)可視化工具操作
可視化工具
我找了好幾個(gè)軟件,github上的也有
最終選定了這幾個(gè)
- 官方的 MongoDB Compass
這算是最好用的
https://www.mongodb.com/download-center?ct=false#compass
- Studio 3T 這個(gè)也不錯(cuò) 不過(guò)好像14天免費(fèi)>收費(fèi)
https://studio3t.com/
- Robo 3T
https://robomongo.org/download
這個(gè)其實(shí)和上面第二個(gè)是一家