1. 程式人生 > >第6周 處理非結構化資料的利器Mongodb

第6周 處理非結構化資料的利器Mongodb

mongodb:面向文件的nosql

mongodb官網:http://www.mongodb.org/ 10gen官網:http://www.mongodb.com/ 面向文件的儲存引擎,可以方便的支援非結構化資料 全面的索引支援,可以在任意屬性上建立索引 資料庫本身內建的複製與高可用 資料庫本身支援的自動分片叢集 豐富的基於文件的查詢功能 原子化的資料操作 非結構化資料 不能確定表的列結構的資料 例子:調查表 集合 集合就是一組文件 文件類似於關係資料庫裡的行 集合類似於關係資料庫裡的表 集合是無模式的,即集合中的文件可以五花八門,無需固定結構 資料庫是由多個集合組成

mongodb的安裝:

# tar zxvf mongodb-linux-i686-2.0.9.tgz      
         --解壓 # mv mongodb-linux-i686-2.0.9 mongodb              --重新命名 # cd mongodb # mkdir data                                                       --data目錄存放資料 # mkdir log                                                         --log目錄存放日誌 # cd bin/                                                         --然後定位到mongodb/bin目錄中
##啟動服務 [[email protected] bin]# ./mongod --dbpath=/nosql/mongodb/mongodb/data/ --logpath=/nosql/mongodb/mongodb/log/mongodb.log all output going to: /nosql/mongodb/mongodb/log/mongodb.log [[email protected] ~]# ps -ef | grep mongo root 3936 3047 0 18:45 pts/0 00:00:00 ./mongod --dbpath=/nosql/mongodb/mongodb/data/ --logpath=/nosql/mongodb/mongodb/log/mongodb.log root 3975 3946 0 18:46 pts/1 00:00:00 grep mongo [
[email protected]
~]# pstree -p | grep mongo         |-sshd(2713)-+-sshd(3045)---bash(3047)---mongod(3936)-+-{mongod}(3937)         | | |-{mongod}(3938)         | | |-{mongod}(3939)         | | |-{mongod}(3940)         | | |-{mongod}(3941)         | | |-{mongod}(3942)         | | `-{mongod}(3943)

通過配置檔案來啟動Mongodb

[[email protected] bin]# cat mongodb.conf  logpath=/nosql/mongodb/mongodb/log/mongodb.log logappend=true                                                              #日誌檔案自動累加,而不是覆蓋 dbpath=/nosql/mongodb/mongodb/data/ pidfilepath=/nosql/mongodb/mongodb/log/mongodb.pid fork=true                                                                     #加了fork,就會丟後臺執行 [[email protected] bin]# ./mongod -f /nosql/mongodb/mongodb/bin/mongodb.conf [[email protected] bin]# forked process: 4004 all output going to: /nosql/mongodb/mongodb/log/mongodb.log [[email protected] bin]# pstree -p | grep mongo         |-mongod(4004)-+-{mongod}(4005)         | |-{mongod}(4006)         | |-{mongod}(4007)         | |-{mongod}(4008)         | |-{mongod}(4009)         | |-{mongod}(4010)         | `-{mongod}(4011)

測試javascript:

[[email protected] bin]# ./mongo MongoDB shell version: 2.0.9 connecting to: test > 40/5 8 > x=200 200 > db.foo.save({a:1}); > db.foo.find(); { "_id" : ObjectId("52b815edda39a9573226cb88"), "a" : 1 }   ##_id用於唯一標示文件,類似oracle裡的rowid > exit                                                      ##objectid是id的預設產生辦法 bye [[email protected] bin]#

常用命令 :

如果想檢視當前連線在哪個資料庫下面,可以直接輸入db 檢視使用者列表db.system.users.find(); 檢視所有使用者show users; 檢視所有資料庫show dbs; 檢視所有集合show collections; 刪除當前的資料庫db.dropDatabase(); 刪除collection db.集合名.drop(); 想知道mongodb執行哪些命令,可以直接輸入help; 想知道當前資料庫支援哪些方法db.help(); 想知道當前集合支援哪些方法db.集合名.help();

MongoDb web 使用者介面

在比MongoDB服務的埠多1000的埠上,你可以訪問到MondoDB的web使用者介面。

如:如果你的MongoDB執行埠使用預設的27017,你可以在埠號為28017訪問web使用者介面。

http://192.168.253.128:28017/

mongod -help

[[email protected] bin]# ./mongod -help Allowed options: General options:   -h [ --help ] show this usage information   --version show version information   -f [ --config ] arg configuration file specifying additional options   -v [ --verbose ] be more verbose (include multiple times for more                               verbosity e.g. -vvvvv)   --quiet quieter output   --port arg specify port number - 27017 by default   --bind_ip arg comma separated list of ip addresses to listen on                               - all local ips by default   --maxConns arg max number of simultaneous connections - 20000 by                               default   --logpath arg log file to send write to instead of stdout - has                               to be a file, not directory   --logappend append to logpath instead of over-writing   --pidfilepath arg full path to pidfile (if not set, no pidfile is                               created)   --keyFile arg private key for cluster authentication   --setParameter arg Set a configurable parameter   --nounixsocket disable listening on unix sockets   --unixSocketPrefix arg alternative directory for UNIX domain sockets                               (defaults to /tmp)   --fork fork server process   --syslog log to system's syslog facility instead of file                               or stdout   --auth run with security   --cpu periodically show cpu and iowait utilization   --dbpath arg directory for datafiles - defaults to /data/db/   --diaglog arg 0=off 1=W 2=R 3=both 7=W+some reads   --directoryperdb each database will be stored in a separate                               directory   --ipv6 enable IPv6 support (disabled by default)   --journal enable journaling   --journalCommitInterval arg how often to group/batch commit (ms)   --journalOptions arg journal diagnostic options   --jsonp allow JSONP access via http (has security                               implications)   --noauth run without security   --nohttpinterface disable http interface   --nojournal disable journaling (journaling is on by default                               for 64 bit)   --noprealloc disable data file preallocation - will often hurt                               performance   --noscripting disable scripting engine   --notablescan do not allow table scans   --nssize arg (=16) .ns file size (in MB) for new databases   --profile arg 0=off 1=slow, 2=all   --quota limits each database to a certain number of files                               (8 default)   --quotaFiles arg number of files allowed per db, requires --quota   --repair run repair on all dbs   --repairpath arg root directory for repair files - defaults to                               dbpath   --rest turn on simple rest api   --shutdown kill a running server (for init scripts)   --slowms arg (=100) value of slow for profile and console log   --smallfiles use a smaller default file size   --syncdelay arg (=60) seconds between disk syncs (0=never, but not                               recommended)   --sysinfo print some diagnostic system information   --upgrade upgrade db if needed Replication options:   --oplogSize arg size to use (in MB) for replication op log. default is                         5% of disk space (i.e. large is good) Master/slave options (old; use replica sets instead):   --master master mode   --slave slave mode   --source arg when slave: specify master as <server:port>   --only arg when slave: specify a single database to replicate   --slavedelay arg specify delay (in seconds) to be used when applying                         master ops to slave   --autoresync automatically resync if slave data is stale Replica set options:   --replSet arg arg is <setname>[/<optionalseedhostlist>]   --replIndexPrefetch arg specify index prefetching behavior (if secondary)                           [none|_id_only|all] Sharding options:   --configsvr declare this is a config db of a cluster; default port                         27019; default dir /data/configdb   --shardsvr declare this is a shard db of a cluster; default port                         27018

mongo -help

[[email protected] bin]# ./mongo -help MongoDB shell version: 2.4.8 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   -u [ --username ] arg username for authentication   -p [ --password ] arg password for authentication   --authenticationDatabase arg user source (defaults to dbname)   --authenticationMechanism arg (=MONGODB-CR)                                         authentication mechanism   -h [ --help ] show this usage information   --version show version information   --verbose increase verbosity   --ipv6 enable IPv6 support (disabled by                                         default) file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified