1 案例1:搭建MongoDB服務器
1.1 問題
- 知足如下要求:
- 在主機192.168.4.51上部署MongoDB服務
1.2 步驟
實現此案例須要按照以下步驟進行。php
步驟一:搭建MongoDB服務器html
1)在主機192.168.4.51上部署MongoDB服務java
MongoDB:mysql
介於關係數據庫和非關係數據庫之間的產品linux
一款基於分佈式文件存儲的數據庫,旨在爲WEB應用提供可擴展的高性能數據存儲解決方案正則表達式
將數據存儲爲一個文檔(相似於JSON對象),數據結構由鍵值(key=>value)對組成sql
支持豐富的查詢表達,能夠設置任何屬性的索引mongodb
支持副本集,分片shell
- [root@mongodb51 ~]# cd mongodb/
- [root@mongodb51 mongodb]# ls
- mongodb-linux-x86_64-rhel70-3.6.3.tgz
- [root@mongodb51 mongodb]# tar -xf mongodb-linux-x86_64-rhel70-3.6.3.tgz
- [root@mongodb51 mongodb]# mkdir /usr/local/mongodb
- [root@mongodb51 mongodb]# cd /usr/local/mongodb/
- [root@mongodb51 mongodb]# cp -r \
- /root/mongodb/mongodb-linux-x86_64-rhel70-3.6.3/bin/ .
- [root@mongodb51 mongodb]# ls
- bin
- [root@mongodb51 mongodb]# mkdir etc
- [root@mongodb51 mongodb]# mkdir log
- [root@mongodb51 mongodb]# mkdir -p data/db
- [root@mongodb51 mongodb]# vim etc/mongodb.conf
- dbpath=/usr/local/mongodb/data/db/
- logpath=/usr/local/mongodb/log/mongodb.log
- logappend=true
- fork=true
2)設置PATH變量數據庫
- [root@mongodb51 mongodb]# vim /etc/profile
- export PATH=/usr/local/mongodb/bin:$PATH
- [root@mongodb51 mongodb]# source /etc/profile
3)啓動服務
- [root@mongodb51 mongodb]# mongod -f /usr/local/mongodb/etc/mongodb.conf
- about to fork child process, waiting until server is ready for connections.
- forked process: 28001
- child process started successfully, parent exiting
4)驗證配置,默認端口爲27017
- [root@mongodb51 mongodb]# ls /usr/local/mongodb/data/db/
- [root@mongodb51 mongodb]# ps -C mongod
- PID TTY TIME CMD
- 28001 ? 00:00:01 mongod
- [root@mongodb51 mongodb]# netstat -antup | grep mongod
- tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 28001/mongod
5)鏈接服務
- [root@mongodb51 mongodb]# mongo
- MongoDB shell version v3.6.3
- connecting to: mongodb:
- MongoDB server version: 3.6.3
- ...
- ...
- > show dbs
- admin 0.000GB
- config 0.000GB
- local 0.000GB
- > db
- test
- > exit
- bye
6)中止服務
- [root@mongodb51 mongodb]# mongod --dbpath=/usr/local/mongodb/data/db/ --shutdown
- killing process with pid: 28001
7)因爲啓動和中止服務名字太長,能夠起一個別名
給中止服務起一個別名
- [root@mongodb51 mongodb]# alias cmdb='mongod --dbpath=/usr/local/mongodb/data/db/ --shutdown'
給啓動服務起一個別名
- [root@mongodb51 mongodb]# alias smdb='mongod -f /usr/local/mongodb/etc/mongodb.conf'
8)修改配置文件,使用ip和端口鏈接服務
- [root@mongodb51 mongodb]# vim /usr/local/mongodb/etc/mongodb.conf
- bind_ip=192.168.4.51
- port=27077
- [root@mongodb51 mongodb]# smdb
- about to fork child process, waiting until server is ready for connections.
- forked process: 28240
- child process started successfully, parent exiting
- [root@mongodb51 mongodb]# ps -C mongod
- PID TTY TIME CMD
- 28240 ? 00:00:01 mongod
- [root@mongodb51 mongodb]# netstat -antup | grep mongod
- tcp 0 0 192.168.4.51:27077 0.0.0.0:* LISTEN 28240/mongod
9)鏈接服務
- [root@mongodb51 mongodb]# mongo --host 192.168.4.51 --port 27077
2 案例2:MongoDB經常使用管理操做
2.1 問題
- 要求以下:
- 練習庫的建立、查看、切換、刪除
- 練習集合的建立、查看、刪除
- 練習文檔的查看、插入、刪除
2.2 步驟
實現此案例須要按照以下步驟進行。
步驟一:MongoDB經常使用管理操做
1)數據庫管理
命令格式:
show dbs 查看已有的庫
db 顯示當前所在的庫
use 庫名 切換庫,若庫不存在延時建立庫
show collections 或 show tables 查看庫下已有集合
db.dropDatabase() 刪除當前所在的庫
查看
- > show dbs
- admin 0.000GB
- config 0.000GB
- local 0.000GB
建立,切換
- > db
- test
- > use ddsdb
- switched to db ddsdb
- > db
- ddsdb
2)集合管理
命令格式:
show collections 或 show tables 查看集合
db.集合名.drop() 刪除集合
db.集合名.save({'',''}) 建立集合,集合不存在時,建立並添加文檔
- > db.t1.save({name:"yaya",age:60,addr:"hebei",email:"yaya@163.com"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({name:"yaya",age:70})
- WriteResult({ "nInserted" : 1 })
- > show tables
- t1
查看集合裏的全部內容
- > db.t1.save({name:"yaya",age:70})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- { "_id" : ObjectId("5badf1b420cdd1574b851f12"), "name" : "yaya", "age" : 60, "addr" : "hebei", "email" : "yaya@163.com" }
- { "_id" : ObjectId("5badf21820cdd1574b851f13"), "name" : "yaya", "age" : 70 }
- >
查看第一行的內容
- > db.t1.findOne()
- {
- "_id" : ObjectId("5badf1b420cdd1574b851f12"),
- "name" : "yaya",
- "age" : 60,
- "addr" : "hebei",
- "email" : "yaya@163.com"
- }
- >
3)文檔管理
命令格式:
db.集合名.find()
db.集合名.count()
db.集合名.insert({「name」:」jim」})
db.集合名.find(條件)
db.集合名.findOne() 返回查詢一條文檔
db.集合名.remove({}) 刪除全部文檔
db.集合名.remove({條件}) 刪除與條件匹配的全部文檔
- > db.t1.save({name:"xm",age:18,"addr":"hn","email":"xm.qq.com","like":"nicai"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.count()
- 3
- > db.t1.find({name:"xm"})
- { "_id" : ObjectId("5badf4bc20cdd1574b851f14"), "name" : "xm", "age" : 18, "addr" : "hn", "email" : "xm.qq.com", "like" : "nicai" }
刪除與條件匹配的全部文檔
- > db.t1.remove({name:"yaya"})
- WriteResult({ "nRemoved" : 2 })
刪除全部文檔
- > db.t1.remove({})
- WriteResult({ "nRemoved" : 1 })
4)插入記錄
- > db.col.insert(
- { title: 'MongoDB 教程',
- description: 'MongoDB 是一個 Nosql 數據庫',
- by: 'MongoDB中文網',
- url: 'http://www.mongodb.org.cn',
- tags: ['mongodb', 'database', 'NoSQL'],
- likes: 100
- }
- )
- WriteResult({ "nInserted" : 1 })
刪除記錄
- > db.col.remove({'title':'MongoDB 教程'})
- WriteResult({ "nRemoved" : 1 })
步驟二:基本數據類型
1)null:用於表示空值或者不存在的字段,{「x」:null}
- > db.t1.save({name:"bob",work:null})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- { "_id" : ObjectId("5badf71520cdd1574b851f16"), "name" : "bob", "work" : null }
- > db.t1.save({_id:9,name:"jerry",work:null})
- WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 9 })
- > db.t1.find()
- { "_id" : ObjectId("5badf71520cdd1574b851f16"), "name" : "bob", "work" : null }
- { "_id" : 9, "name" : "jerry", "work" : null }
2)布爾值:布爾類型有兩個值true和false,{「x」:true}
- > db.t1.save({name:"zhangsan",addr:"shanghai",marry:"true"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({name:"lisi",age:35,addr:"beijing",marry:"false"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- { "_id" : ObjectId("5badf71520cdd1574b851f16"), "name" : "bob", "work" : null }
- { "_id" : 9, "name" : "jerry", "work" : null }
- { "_id" : ObjectId("5badf7b020cdd1574b851f17"), "name" : "zhangsan", "addr" : "shanghai", "marry" : "true" }
- { "_id" : ObjectId("5badf7b720cdd1574b851f18"), "name" : "lisi", "age" : 35, "addr" : "beijing", "marry" : "false" }
- >
3)數值:shell默認使用64爲浮點型數值,{「x」:3.14}或{「x」:3},對於整型值,可使用NumberInt(4字節符號整數)或NumberLong(8字節符號整數),{「x」:NumberInt(「3」)}{「x」:NumberLong(「3」)}
- > db.t1.save({name:"alice",woker:"gcs",pay:28888.88})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({pay:28888.88})
- { "_id" : ObjectId("5badf80f20cdd1574b851f19"), "name" : "alice", "woker" : "gcs", "pay" : 28888.88 }
- > db.t1.save({name:"lilei",woker:"cxy",pay:28000})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({pay:28000})
- { "_id" : ObjectId("5badf81f20cdd1574b851f1a"), "name" : "lilei", "woker" : "cxy", "pay" : 28000 }
4)字符串:UTF-8字符串均可以表示爲字符串類型的數據,{「x」:「呵呵」}
- > db.t1.save({name:"hehe",woker:null,pay:null})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({name:"呵呵",woker:"沒有",pay:"沒有"})
- WriteResult({ "nInserted" : 1 })
5)日期:日期被存儲爲自新紀元依賴通過的毫秒數,不存儲時區,{「x」:new Date()}
- > db.t1.save({name:"bobo",wokertime:new Date()})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({name:"bobo"})
- { "_id" : ObjectId("5badf8ff6827555e3fd8680f"), "name" : "bobo", "wokertime" : ISODate("2018-09-28T09:48:47.437Z") }
- >
6)正則表達式:查詢時,使用正則表達式做爲限定條件,語法與JavaScript的正則表達式相同,{「x」:/[abc]/}
- > db.t1.save({procname:"php",code:/abc$/})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({procname:"php",code:/[a-z]/})
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({procname:"java",code:/[a-z]/})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- ...
- ...
- { "_id" : ObjectId("5badf93b6827555e3fd86810"), "procname" : "php", "code" : /abc$/ }
- { "_id" : ObjectId("5badf9426827555e3fd86811"), "procname" : "php", "code" : /[a-z]/ }
- { "_id" : ObjectId("5badf9496827555e3fd86812"), "procname" : "java", "code" : /[a-z]/ }
7)數組:數據列表或數據集能夠表示爲數組,{「x」: [「a「,「b」,」c」]},一個字段有多個值
- > db.t1.save({name:"jerry",email:["plj@163.com","lij@yahoo.net","lij@tedu"]})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({name:"jerry"})
- { "_id" : 9, "name" : "jerry", "work" : null }
- { "_id" : ObjectId("5badf9976827555e3fd86813"), "name" : "jerry", "email" : [ "plj@163.com", "lij@yahoo.net", "lij@tedu" ] }
- >
8)內嵌文檔:文檔能夠嵌套其餘文檔,被嵌套的文檔做爲值來處理,{「x」:{「y」:3 }}
- > db.t1.save({book: {zuozhe:"dmy",bname:"yuweizhidao",jiaqian:69,version:2.0 }})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- ...
- ...
- { "_id" : ObjectId("5badfa4a6827555e3fd86814"), "book" : { "zuozhe" : "dmy", "bname" : "yuweizhidao", "jiaqian" : 69, "version" : 2 } }
9) 對象id:對象id是一個12字節的字符串,是文檔的惟一標識,{「x」: objectId() }
- > db.t1.save({stunum:ObjectId(),name:"yaya",class:"nsd"})
- WriteResult({ "nInserted" : 1 })
- > db.t1.find({name:"yaya"})
- { "_id" : ObjectId("5badfa966827555e3fd86816"), "stunum" : ObjectId("5badfa966827555e3fd86815"), "name" : "yaya", "class" : "nsd" }
10)二進制數據:二進制數據是一個任意字節的字符串。它不能直接在shell中使用。若是要將非utf-字符保存到數據庫中,二進制數據是惟一的方式。
11)代碼:查詢和文檔中能夠包括任何JavaScript代碼,{「x」:function(){/*…*/}}
- > db.t1.save({lname:"html",codecript:function(){
- WriteResult({ "nInserted" : 1 })
- > db.t1.save({lname:"html",codecript:function(){
- WriteResult({ "nInserted" : 1 })
- > db.t1.find()
- ...
- ...
- { "_id" : ObjectId("5badfd626827555e3fd86817"), "lname" : "html", "codecript" : { "code" : "function (){/*...*/}" } }
- { "_id" : ObjectId("5badfd6a6827555e3fd86818"), "lname" : "html", "codecript" : { "code" : "function (){/*<html><h1>abc</h1></html>*/}" } }
- >
3 案例3:數據導入導出/備份/恢復
3.1 問題
3.2 步驟
實現此案例須要按照以下步驟進行。
步驟一:數據備份與恢復
1)數據備份
- [root@mongodb51 ~]# mongodump --host 192.168.4.51 --port 27077
- 2018-09-28T18:14:12.585+0800 writing admin.system.version to
- 2018-09-28T18:14:12.586+0800 done dumping admin.system.version (1 document)
- 2018-09-28T18:14:12.586+0800 writing ddsdb.t1 to
- 2018-09-28T18:14:12.586+0800 writing test.t1 to
- 2018-09-28T18:14:12.586+0800 writing ddsdb.col to
- 2018-09-28T18:14:12.587+0800 done dumping ddsdb.t1 (17 documents)
- 2018-09-28T18:14:12.588+0800 done dumping test.t1 (1 document)
- 2018-09-28T18:14:12.588+0800 done dumping ddsdb.col (0 documents)
- [root@mongodb51 ~]# ls
- dump
- [root@mongodb51 ~]# bsondump dump/ddsdb/t1.bson
- {"_id":{"$oid":"5badf71520cdd1574b851f16"},"name":"bob","work":null}
- {"_id":9.0,"name":"jerry","work":null}
- ...
- ...
- {"_id":{"$oid":"5badfd626827555e3fd86817"},"lname":"html","codecript":{"$code":"function (){/*...*/}"}}
- {"_id":{"$oid":"5badfd6a6827555e3fd86818"},"lname":"html","codecript":{"$code":"function (){/*\u003chtml\u003e\u003ch1\u003eabc\u003c/h1\u003e\u003c/html\u003e*/}"}}
- 2018-09-28T18:15:45.948+0800 17 objects found
備份時指定備份的庫和備份目錄
- [root@mongodb51 ~]# mongodump --host 192.168.4.51 --port 27077 -d ddsdb -o /root/bbsdb
- 2018-09-28T18:23:30.389+0800 writing ddsdb.t1 to
- 2018-09-28T18:23:30.389+0800 writing ddsdb.col to
- 2018-09-28T18:23:30.391+0800 done dumping ddsdb.t1 (17 documents)
- 2018-09-28T18:23:30.391+0800 done dumping ddsdb.col (0 documents)
只備份ddsdb庫裏的集合t1
- [root@mongodb51 ~]# mongodump --host 192.168.4.51 --port 27077 -d ddsdb -c t1 -o /root/bbsdb.t
- 2018-09-28T18:19:00.210+0800 writing ddsdb.t1 to
- 2018-09-28T18:19:00.211+0800 done dumping ddsdb.t1 (17 documents)
2)恢復數據
- [root@mongodb51 ~]# mongo --host 192.168.4.51 --port 27077
- > show tables;
- col
- t1
- > db.t1.remove({})
- WriteResult({ "nRemoved" : 17 })
- >exit
- [root@mongodb51 ~]# mongorestore --host 192.168.4.51 --port 27077 -d ddsdb /root/bbsdb.t/ddsdb/
- 2018-09-28T18:26:16.889+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
- 2018-09-28T18:26:16.890+0800 building a list of collections to restore from /root/bbsdb.t/ddsdb dir
- 2018-09-28T18:26:16.891+0800 reading metadata for ddsdb.t1 from /root/bbsdb.t/ddsdb/t1.metadata.json
- 2018-09-28T18:26:16.891+0800 restoring ddsdb.t1 from /root/bbsdb.t/ddsdb/t1.bson
- 2018-09-28T18:26:16.893+0800 no indexes to restore
- 2018-09-28T18:26:16.893+0800 finished restoring ddsdb.t1 (17 documents)
- 2018-09-28T18:26:16.893+0800 done
步驟二:數據的導入導出
1)導出
用csv的格式導出
- [root@mongodb51 ~]# mongoexport --host 192.168.4.51 --port 27077 -d ddsdb -c t1 -f name --type=csv -o /root/lig1.csv
- 2018-09-28T18:29:24.653+0800 connected to: 192.168.4.51:27077
- 2018-09-28T18:29:24.654+0800 exported 17 records
- [root@mongodb51 ~]# cat lig1.csv
- name
- bob
- jerry
- zhangsan
- lisi
- alice
- lilei
- hehe
- 呵呵
- bobo
- jerry
- yaya
- [root@mongodb51 ~]# mongoexport --host 192.168.4.51 --port 27077 -d ddsdb -c t1 -q '{name:"bob"}' -f name,age --type=csv -o /root/lig2.csv
- 2018-09-28T18:31:25.627+0800 connected to: 192.168.4.51:27077
- 2018-09-28T18:31:25.628+0800 exported 1 record
用json的格式導出
- [root@mongodb51 ~]# mongoexport --host 192.168.4.51 --port 27077 -d ddsdb -c t1 --type=json -o /root/lig3.json
- 2018-09-28T18:33:13.349+0800 connected to: 192.168.4.51:27077
- 2018-09-28T18:33:13.350+0800 exported 17 records
- [root@mongodb51 ~]# mongoexport --host 192.168.4.51 --port 27077 -d ddsdb -c t1 -f name --type=json -o /root/lig4.json
- 2018-09-28T18:33:35.914+0800 connected to: 192.168.4.51:27077
- 2018-09-28T18:33:35.915+0800 exported 17 records
2)導入
- [root@mongodb51 ~]# mongo --host 192.168.4.51 --port 27077
- > use ddsdb
- switched to db ddsdb
- > show tables;
- col
- t1
- > db.t1.remove({})
- WriteResult({ "nRemoved" : 17 })
- > exit
用json的格式導入:表裏要沒有數據,否則導入不成功
- [root@mongodb51 ~]# mongoimport --host 192.168.4.51 --port 27077 -d ddsdb -c t1 --type=json /root/lig3.json
- 2018-09-28T18:35:22.341+0800 connected to: 192.168.4.51:27077
- 2018-09-28T18:35:22.343+0800 imported 17 documents
- [root@mongodb51 ~]# mongo --host 192.168.4.51 --port 27077
- > use ddsdb
- switched to db ddsdb
- > db.t1.count({})
- 17
用csv的格式導入:表裏能夠有數據
- [root@mongodb51 ~]# mongoimport --host 192.168.4.51 --port 27077 -d ddsdb -c t1 --headerline --type=csv /root/lig1.csv
- 2018-09-28T18:37:36.778+0800 connected to: 192.168.4.51:27077
- 2018-09-28T18:37:36.779+0800 imported 11 documents