1.下載軟件:
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.3.tgz
2.解壓安裝:
tar -zxvf mongodb-linux-x86_64-rhel62-3.0.3.tgz
mv mongodb-linux-x86_64-rhel62-3.0.3 /usr/local/
cd /usr/local/
ln -s mongodb-linux-x86_64-rhel62-3.0.3 mongodb
mkdir -p data/db mkdir data/log
3.啓動:
echo 'export PATH=/usr/local/mongodb/bin:$PATH' >/etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh
命令行啓動:
./mongod --dbpath=../data/db --logpath=../data/log/mongod.log --fork
mongod爲服務端程序 --dbpath指定數據的存儲目錄 --logpath指定日誌的存儲目錄 --fork指定以守護進程的方式啓動(注意,以守護進程方式啓動的話必須指定日誌存儲路徑)
配置文件啓動:
[root@localhost bin]# cat /etc/mongod.conf
logpath=/usr/local/mongodb/data/log/mongod.log
logappend=true
fork=true
dbpath=/usr/local/mongodb/data/db
port=27017
rest=true
#默認的mongodb會監控27017端口不甚安全,能夠用--port參數進行設定其監控的端口。mongodb默認的會在服務的端口號加上1000的端口上啓動一個web服務器,要使用web服務器的有關內容,須要啓用--rest參數;
#http://192.168.16.131:28017
[root@localhost bin]#./mongod -f /etc/mongod.conf
4.mongodb的架構介紹:
4.1數據邏輯結構
MongoDB的數據邏輯結構由:數據庫(database)、集合(collections)、文檔(document)三部分組成。
一個MongoDB支持多個數據庫,每一個數據庫中包含多個集合(至關於關係數據庫中的表),每一個集合中包含多個文檔(至關於關係型數據庫中表的一行)。
4.2數據存儲結構
MongoDB中,每一個數據庫包含一個.ns和一個或多個數據文件,其中,數據文件會隨着數據量的增多而變多,例如Test數據庫的數據文件就由Test.0、Test.一、Test.2等等組成。MongoDB採用預分配空間機制,每一個預分配空間的文件都採用0進行填充,因爲集合中的數據增長,數據文件每新分配一次,它的大小會是上一個文件大小的2倍,數據庫裏每一個集合和索引都對應一個命名空間,這些命名空間的元數據都存儲在.ns文件裏。
4.3BSON
BSON是一種相似於json的二進制的存儲格式,Binary JSON,支持內建的文檔對象和數組對象,而且包含JSON所沒有的一些數據類型。MongoDB採用BSON這種結構來存儲數據和進行網絡數據交換,把這個格式轉化成Document的概念,因爲BSON是模式自由的,因此document也是模式自由的。
5.mongodb的簡單操做:
[root@localhost ~]# mongo
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> show users
> show profile
db.system.profile is empty
Use db.setProfilingLevel(2) will enable profiling
Use db.system.profile.find() to show raw profile entries
#列出當前有哪些數據庫
> show dbs;
local 0.078GB
> db.test_1.save({1:"AAA"});
WriteResult({ "nInserted" : 1 })
> db.test_1.save({2:"BBB"});
WriteResult({ "nInserted" : 1 })
> db.test_1.find();
{ "_id" : ObjectId("556d171a75f85e97eeec2f5b"), "1" : "AAA" }
{ "_id" : ObjectId("556d172375f85e97eeec2f5c"), "2" : "BBB" }
> show dbs;
local 0.078GB
test 0.078GB
#查看當前的數據庫
> db
test
#列出當前數據庫中有哪些集合
> show collections
system.indexes
test_1
6.關閉mongodb:
[root@localhost ~]# mongo
> use admin
switched to db admin
> db.shutdownServer();
2015-06-01T19:35:45.155-0700 I NETWORK DBClientCursor::init call() failed
server should be down...
2015-06-01T19:35:45.159-0700 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2015-06-01T19:35:45.159-0700 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-06-01T19:35:45.160-0700 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failedlinux