MongoDB是什么
MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由C++語(yǔ)言編寫。旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
他的特點(diǎn):高性能、易部署、易使用,存儲(chǔ)數(shù)據(jù)非常方便。
本文主要給大家介紹了在mac下安裝和配置mongodb的步驟,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō),來(lái)一起看看詳細(xì)的介紹:
備注:本文相關(guān)源碼可在文末下載!
安裝 mongodb
install 之前,iTerm2 下用 brew 查看已安裝軟件、搜索 mongodb:
brew list
brew search mongodb
安裝 mongodb :
此處需要稍等一段時(shí)間,成功后會(huì)輸出以下即說(shuō)明安裝成功:
$ brew install mongodb
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.4.0.sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-3.4.0.sierra.bottle.1.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
brew services start mongodb
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
==> Summary
🍺 /usr/local/Cellar/mongodb/3.4.0: 17 files, 261.4M
啟動(dòng) mongodb
新建一個(gè) iTerm2 窗口,執(zhí)行 mongod
嘗試啟動(dòng) mongodb 但會(huì)失敗 exiting:
$ mongod
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] MongoDB starting : pid=1765 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] db version v3.4.0
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c1
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2j 26 Sep 2016
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] allocator: system
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] modules: none
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] build environment:
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] distarch: x86_64
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-06-12T15:51:49.810+0800 I CONTROL [initandlisten] options: {}
2017-06-12T15:51:49.811+0800 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-06-12T15:51:49.811+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-06-12T15:51:49.811+0800 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-06-12T15:51:49.811+0800 I CONTROL [initandlisten] now exiting
2017-06-12T15:51:49.811+0800 I CONTROL [initandlisten] shutting down with code:100
啟動(dòng) mongodb 之前,要先新建一個(gè)mongodb默認(rèn)的數(shù)據(jù)寫入目錄:
$ mkdir -p /data/db
mkdir: /data/db: Permission denied (沒(méi)有權(quán)限拒絕訪問(wèn))
// sudo 并輸入密碼,重新新建目錄
$ sudo mkdir -p /data/db
Password:
給剛才新建的數(shù)據(jù)庫(kù)目錄賦予權(quán)限:
$ sudo chown -R guojc /data
此時(shí),執(zhí)行 mongod
啟動(dòng) mongodb 服務(wù):
$ mongod
2017-06-12T16:00:48.036+0800 I CONTROL [initandlisten] MongoDB starting : pid=1837 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] db version v3.4.0
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c1
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2j 26 Sep 2016
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] allocator: system
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] modules: none
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] build environment:
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] distarch: x86_64
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-06-12T16:00:48.037+0800 I CONTROL [initandlisten] options: {}
2017-06-12T16:00:48.037+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(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-06-12T16:00:48.616+0800 I CONTROL [initandlisten]
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten]
2017-06-12T16:00:48.665+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-06-12T16:00:48.741+0800 I INDEX [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-06-12T16:00:48.741+0800 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-06-12T16:00:48.755+0800 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs
2017-06-12T16:00:48.756+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
2017-06-12T16:00:48.756+0800 I NETWORK [thread1] waiting for connections on port 27017
mongodb 啟動(dòng)成功,正等待著被連接。
新建 iTerm2 窗口,執(zhí)行 mongo
,進(jìn)入 mongodb 命令行模式:
$ mongo
MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten]
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-06-12T16:00:48.616+0800 I CONTROL [initandlisten]
>
繼續(xù)在上面的終端輸入 show dbs
,會(huì)列出系統(tǒng)自帶的2個(gè)數(shù)據(jù)庫(kù):
> show dbs
admin 0.000GB
local 0.000GB
help
小結(jié)一下,往后要重新啟動(dòng) mongodb 服務(wù)、進(jìn)入 mongodb 命令行的操作:
在一個(gè)iTerm2窗口執(zhí)行:mongod //MongoDB starting........waiting for connections
另一個(gè)iTerm2窗口執(zhí)行:mongo //MongoDB shell
插入數(shù)據(jù)
首先,在電腦根目錄下安裝 node 的數(shù)據(jù)驅(qū)動(dòng)庫(kù):
# guojc @ MacBook-Pro-2 in ~ [16:29:33] C:127
$ cnpm install mongodb
新建目錄并編輯text.js文件: s3_mongodb/test.js:
var MongoClient = require('mongodb').MongoClient;
// 連接數(shù)據(jù)庫(kù)
var url_test = 'mongodb://localhost:27017/test'; //數(shù)據(jù)庫(kù)test本不存在,連接時(shí)會(huì)自動(dòng)創(chuàng)建
var insertData = function(db){
// 往test數(shù)據(jù)庫(kù)里新建一個(gè)site集合,并插入一條數(shù)據(jù)
db.collection('site').insertOne({name: 'guojc', age: 99, hobby: 'movie'}, function(err, result){
console.log('inserted successly');
console.log(result);
db.close();
console.log('close');
});
}
MongoClient.connect(url_test, function(err, db) {
console.log('Connected successly to server.');
insertData(db);
});
新建 iTerm2 窗口,運(yùn)行 test.js:
成功后,來(lái)到之前執(zhí)行過(guò) mongo
的 MongoDB shell 窗口,執(zhí)行:
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
help
繼續(xù)執(zhí)行:選中數(shù)據(jù)庫(kù)test、顯示test中的表,查詢?cè)摫淼乃袛?shù)據(jù):
> use test
switched to db test
> show tables
site
> db.site.find()
{ "_id" : ObjectId("593e594ab4727b09349c33a6"), "name" : "guojc", "age" : 99, "hobby" : "movie" }
至此,mongodb的安裝配置與基本啟動(dòng)就介紹完了。
相關(guān)源碼下載:
github地址:https://github.com/gjincai/vue-node-proj/blob/master/s3_Mongodb/test.js
本地下載地址:http://xiazai.jb51.net/201707/yuanma/mac-mongodb(jb51.net).rar
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
您可能感興趣的文章:- MongoDB的安裝及配置文件選項(xiàng)全解
- Mac中mongoDB的安裝與卸載步驟詳解
- 在Mac OS上安裝使用MongoDB的教程
- mac下使用brew 安裝mongodb的方法教程
- MongoDB的安裝方法圖文教程
- 在mac系統(tǒng)下安裝與配置mongoDB數(shù)據(jù)庫(kù)