初次學(xué)習(xí)mongodb

MongoDB安裝

學(xué)習(xí)node快接近尾聲了, 想使用express+ejs+mongoDB 完成一個(gè)小栗子, 算是我學(xué)習(xí)node的一個(gè)結(jié)業(yè)考試. 不過,之前從來沒有接觸過mongodb,對(duì)我這小小白來說, 安裝-啟動(dòng)mongodb都異常的艱難, 想想真是 ... 鐵窗呀 ~~ 鐵門 ~ 鐵鎖鏈 ~~~ 路過的你們請(qǐng)盡情嘲笑吧 , 我不怕!!!!

在mongodb官網(wǎng)的安裝指南 上沒有找到 ArchLinux版本的安裝包, 所以通過下載安裝包解壓使用是不太可能了, 只能自己下載源碼編譯安裝或者看看Arch 官方倉庫或AUR倉庫有沒有了, 不過豐富的軟件支持在Arch里從來不是問題,! Arch官方倉庫有, AUR倉庫也有. 如:

** pacman**

[palm@arch]: ~/myapps/node-web-learn>$ sudo pacman -Ss MongoDB
[sudo] password for palm: 
community/libmongoc 1.4.2-1
    A client library written in C for MongoDB
community/mongodb 3.2.10-2
    A high-performance, open source, schema-free document-oriented database
community/mongodb-tools 3.2.5-1
    The MongoDB tools provide import, export, and diagnostic capabilities.
community/php-mongodb 1.1.9-1
    MongoDB driver for PHP
community/python-pymongo 3.3.1-1
    Python module for using MongoDB
community/python2-pymongo 3.3.1-1
    Python module for using MongoDB

** AUR **

[palm@arch]: ~/myapps/node-web-learn>$ yaourt -Ss MongoDB
community/libmongoc 1.4.2-1
    A client library written in C for MongoDB
community/mongodb 3.2.10-2
    A high-performance, open source, schema-free document-oriented database
community/mongodb-tools 3.2.5-1
    The MongoDB tools provide import, export, and diagnostic capabilities.
community/php-mongodb 1.1.9-1
    MongoDB driver for PHP
community/python-pymongo 3.3.1-1
    Python module for using MongoDB
community/python2-pymongo 3.3.1-1
    Python module for using MongoDB
aur/adminer-git 4.2.5.r0.g53dfafd-1 (2) (0.01)
    a web based SQL management tool supporting MySQL, PostgreSQL, SQLite, MS SQL, Oracle, Firebird, SimpleDB, Elasticsearch and MongoDB. Formerly phpMinAdmin.
aur/cube-git 20140730-1 (0) (0.00)
    A system for analyzing time series data using MongoDB and Node
aur/fluent-plugin-mongo 0.7.7-1 (0) (0.00)
    MongoDB plugin for Fluent event collector
aur/graylog 2.1.2-2 (3) (0.00)
    Graylog is an open source syslog implementation that stores your logs in ElasticSearch and MongoDB
aur/libmongo-client 0.1.8-1 (0) (0.00)
    Alternative C driver for MongoDB (obsolete)
aur/mongo-cxx-driver r3.0.0-1 (Out of Date) (5) (1.24)
    The official MongoDB C++ driver library
aur/mongo-cxx-driver-legacy 1.1.2-1 (6) (0.00)
    Official MongoDB C++ driver (legacy).
aur/mongo-cxx-driver-legacy-0.0-26compat 2.6.12-1 (6) (0.00)
    Official MongoDB C++ driver (26compat).
aur/mongo_fdw 4.0.0-2 (1) (0.00)
    PostgreSQL foreign data wrapper for MongoDB
aur/mongobooster 3.0.3-1 (Out of Date) (3) (0.87)
    The Smartest MongoDB Admin GUI
aur/mongoclient 1.3.0-1 (1) (0.09)
    MongoDB administration client
aur/perl-bson 0.11-1 (1) (0.00)
    Pure Perl implementation of MongoDB's BSON serialization
aur/perl-mojolicious-plugin-mongodb 1.16-1 (0) (0.00)
    Use MongoDB in Mojolicious
aur/perl-mongodb 1.2.3-1 (3) (0.00)
    Official MongoDB Driver for Perl
aur/perl-mongodbx-autoderef 1.110560-1 (0) (0.00)
    Automagically dereference MongoDB DBRefs lazily
aur/php53-mongo 1.5.8-1 (1) (0.00)
    Officially supported PHP 5.3 driver for MongoDB
aur/python-flask-pymongo 0.4.0-1 (1) (0.00)
    Flask-PyMongo bridges Flask and PyMongo, so that you can use Flask’s normal mechanisms to configure and connect to MongoDB.
aur/python-monary-hg 0.3.0.r69.efc4072b9b7f-1 (1) (0.00)
    Perform high-performance column queries from MongoDB for Python 3. 10x speedup over pymongo alone.
aur/python-mongoengine 0.10.6-2 (0) (0.00)
    An object-document mapper for MongoDB.
aur/python2-dex 0.5.5-1 (0) (0.00)
    A MongoDB performance tuning tool that compares queries to the available indexes in the queried collection(s) and generates index suggestions based on simple heuristics.
aur/python2-flask-pymongo 0.4.0-1 (1) (0.00)
    Flask-PyMongo bridges Flask and PyMongo, so that you can use Flask’s normal mechanisms to configure and connect to MongoDB.
aur/python2-monary-hg 0.3.0.r69.efc4072b9b7f-1 (1) (0.00)
    Perform high-performance column queries from MongoDB for Python 2. 10x speedup over pymongo alone.
aur/python2-pymongo-2.9 2.9-1 (0) (0.00)
    Python driver for MongoDB
aur/robomongo 0.9.0-1 (70) (4.08)
    Shell-centric cross-platform open source MongoDB management tool
aur/robomongo-bin 0.9.0-1 (20) (1.54)
    Shell-centric cross-platform open source MongoDB management tool
aur/ros-indigo-moveit-ros-warehouse 0.6.5-2 (0) (0.00)
    ROS - Components of MoveIt connecting to MongoDB.
aur/ros-indigo-warehouse-ros 0.8.8-4 (0) (0.00)
    ROS - Persistent storage of ROS data using MongoDB.
aur/umongo 1.6.2-1 (40) (1.18)
    This package provides a GUI app that can browse and administer a MongoDB cluster
[palm@arch]: ~/myapps/node-web-learn>$ sudo pacman -Ss MongoDB
[sudo] password for palm: 
community/libmongoc 1.4.2-1
    A client library written in C for MongoDB
community/mongodb 3.2.10-2
    A high-performance, open source, schema-free document-oriented database
community/mongodb-tools 3.2.5-1
    The MongoDB tools provide import, export, and diagnostic capabilities.
community/php-mongodb 1.1.9-1
    MongoDB driver for PHP
community/python-pymongo 3.3.1-1
    Python module for using MongoDB
community/python2-pymongo 3.3.1-1
    Python module for using MongoDB

隨便挑選一個(gè)安裝就可以了.

使用MongoDB

軟件找到了以后, 然后找到mongodb node.js的支持指南 按照指南安裝好node.js的mongodb驅(qū)動(dòng)包,然后編寫一個(gè)小demo測試一下, Quick Start:

var MongoClient = require('mongodb').MongoClient,assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  db.close();
});

接著運(yùn)行 node app.js , 不是我想象的那般順利, 錯(cuò)誤如下:

[palm@arch]: ~/myapps/mongodb-test>$ node app.js 

/home/palm/myapps/mongodb-test/node_modules/mongodb/lib/mongo_client.js:225
          throw err
          ^
AssertionError: null == { MongoError: failed to connect to server [localhost:27017] on first connect
    at Pool.<anonymous> (/home/palm/myapps/mongodb-
    at /home/palm/myapps/mongodb-test/app.js:9:10
    at connectCallback (/home/palm/myapps/mongodb-test/node_modules/mongodb/lib/mongo_client.js:315:5)
    at /home/palm/myapps/mongodb-test/node_modules/mongodb/lib/mongo_client.js:222:11
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

網(wǎng)絡(luò)上一頓狂搜, 好像安裝好以后需要指定mongodb存放數(shù)據(jù)的目錄, 默認(rèn)在/data/db 于是:


[palm@arch]: ~/myapps/mongodb-test/data>$ mongod --dbpath=/home/palm/myapps/mongodb-test/data
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten] MongoDB starting : pid=13694 port=27017 dbpath=/home/palm/myapps/mongodb-test/data 64-bit host=arch
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten] db version v3.2.10
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten] modules: none
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten] build environment:
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten]     distarch: x86_64
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2016-11-21T10:45:55.066+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/home/palm/myapps/mongodb-test/data" } }
2016-11-21T10:45:55.086+0800 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /home/palm/myapps/mongodb-test/data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2016-11-21T10:45:55.086+0800 I CONTROL  [initandlisten] dbexit:  rc: 100

大概意思是, 已經(jīng)有mongodb服務(wù)在運(yùn)行中,現(xiàn)在沒有權(quán)限再創(chuàng)建一個(gè)mongodb實(shí)例. 然后我使用ps 確是查詢到了很多個(gè)mongodb進(jìn)程. 找到后一一都干死, 然后從Archlinux Wiki MongoDB篇又了解到在Archlinux中MongoDB安裝指南 需要使用systemctl 啟動(dòng)服務(wù):

systemctl start mongodb.service

下面也有一些常用問題解決方案,例如mongodb服務(wù)異常關(guān)閉導(dǎo)致被鎖,和mongodb使用指南, 很方便, 之前老聽人說Archlinux Wiki 是Linux發(fā)行版最完整最詳細(xì)的Wiki, 現(xiàn)在看來,越來越覺得確是如此, 平時(shí)archlinx 鬧脾氣也都是翻閱Wiki解決的.

這一通折騰之后, 使用--repair修復(fù)之前的database 設(shè)置, 如:

mongod --dbpath /home/palm/myapps/mongodb-test/data --repair

然后運(yùn)行mongo 進(jìn)入mongodb命令行(MongoDB shell)并初始化數(shù)據(jù)庫:

[palm@arch]: ~/myapps/mongodb-test>$ mongo
MongoDB shell version: 3.2.10
connecting to: test
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: 
2016-11-21T14:19:39.625+0800 I CONTROL  [initandlisten] 
2016-11-21T14:19:39.625+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-11-21T14:19:39.625+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-11-21T14:19:39.625+0800 I CONTROL  [initandlisten] 
>

這代表mongodb服務(wù)已經(jīng)成功啟動(dòng)了, 可以使用了, 到這里,我總算是半只腳邁進(jìn)了mongodb的大門, 接下來可以在項(xiàng)目實(shí)踐中一邊學(xué)習(xí)一邊使用mongodb了.

關(guān)于設(shè)置mongodb數(shù)據(jù)存儲(chǔ)目錄

在linux中,默認(rèn)情況下手動(dòng)安裝或解壓mongodb應(yīng)用包后,缺少數(shù)據(jù)存儲(chǔ)目錄:

默認(rèn)為/data/db

這個(gè)目錄最開始是不存在的,而且運(yùn)行參數(shù)設(shè)定命令也不會(huì)自動(dòng)幫我們新建. 所以需要提前將這個(gè)目錄新建完成. 當(dāng)然,這個(gè)目錄是可以修改的, 使用--dbpath 自定義mongodb數(shù)據(jù)存儲(chǔ)目錄. 使用--port arg 可以自定義服務(wù)端口, 還有主機(jī)地址 host , 更多參數(shù)幫助可以使用:

mongo --help

查看

[palm@arch]: ~/myapps/mongodb-test>$ mongo --help
MongoDB shell version: 3.2.10
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
  foo                   foo database on local machine
  192.169.0.5/foo       foo database on 192.168.0.5 machine
  192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999
Options:
  --shell                             run the shell after executing files
  --nodb                              don't connect to mongod on startup - no 
                                      'db address' arg expected
  --norc                              will not run the ".mongorc.js" file on 
                                      start up
  --quiet                             be less chatty
  --port arg                          port to connect to
  --host arg                          server to connect to
  --eval arg                          evaluate javascript
  -h [ --help ]                       show this usage information
  --version                           show version information
  --verbose                           increase verbosity
  --ipv6                              enable IPv6 support (disabled by default)
  --disableJavaScriptJIT              disable the Javascript Just In Time 
                                      compiler
  --enableJavaScriptProtection        disable automatic JavaScript function 
                                      marshalling
  --ssl                               use SSL for all connections
  --sslCAFile arg                     Certificate Authority file for SSL
  --sslPEMKeyFile arg                 PEM certificate/key file for SSL
  --sslPEMKeyPassword arg             password for key in PEM file for SSL
  --sslCRLFile arg                    Certificate Revocation List file for SSL
  --sslAllowInvalidHostnames          allow connections to servers with 
                                      non-matching hostnames
  --sslAllowInvalidCertificates       allow connections to servers with invalid
                                      certificates
  --sslFIPSMode                       activate FIPS 140-2 mode at startup

Authentication Options:
  -u [ --username ] arg               username for authentication
  -p [ --password ] arg               password for authentication
  --authenticationDatabase arg        user source (defaults to dbname)
  --authenticationMechanism arg       authentication mechanism
  --gssapiServiceName arg (=mongodb)  Service name to use when authenticating 
                                      using GSSAPI/Kerberos
  --gssapiHostName arg                Remote host name to use for purpose of 
                                      GSSAPI/Kerberos authentication

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified

如果你將mongodb數(shù)據(jù)存儲(chǔ)目錄設(shè)置在非/home/ 下,則需要注意權(quán)限, 需要讓自定義目錄擁有-r 可讀 / -w 可寫 / -x 可執(zhí)行 的權(quán)限.

參考鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容