標籤:bind centos nosql cte min install 關聯式資料庫 control ant
MongoDB簡介:
MongoDB是一款跨平台、面向文檔的資料庫,可以實現高效能、高可用性,並且能夠輕鬆擴充.
MongoDB也是一個介於非關聯式資料庫和關聯式資料庫之間的產品,是非關係型資料庫中功能最豐富,最像關係型資料庫的.
運行方式主要基於兩個概念:集合與文檔
MongoDB的特點:
1、安裝簡單,提供了面向文檔儲存功能
2、提供了複製、高可用性和自動分區功能
3、支援豐富的查詢運算式,查詢指令使用JSON形式的標記
4、支援各種程式設計語言:Ruby、Pathon、Java、C++、PHP、c#等
下面我在Centos7安裝MongoDB並示範相關的各種基礎操作
一、安裝MongoDB(1)配置YUM源倉庫
[[email protected] ~]#cd /etc/yum.repos.d/[[email protected] yum.repos.d]#vim mongodb.repo[mongodb-org]name=MongoDB Repositorybaseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
(2)安裝MongoDB
[[email protected] yum.repos.d]#yum list //測試[[email protected] ~]#yum install -y mongodb-org[[email protected] ~]#vi /etc/mongod.confbindIp: 0.0.0.0 #監聽地址port: 27017 #監聽連接埠[[email protected] ~]#systemctl start mongod.service[[email protected] ~]#netstat -anpt | grep 27017tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 44010/mongod[[email protected] ~]#/usr/bin/mongo>db.version() //查看版本資訊>show dbsadmin 0.0000GBlocal 0.0000GB> DB.getMongo()2018-07-16T16:10:27.899+0800 E QUERY [thread1] TypeError: DB.getMongo is not a function :@(shell):1:1
二、啟動MongoDB多執行個體
[[email protected] ~]# cp -p /etc/mongod.conf /etc/mongod2.conf[[email protected] ~]# vim /etc/mongod2.confpath: /data/mongodb/mongod2.log //日誌位置dbPath: /data/mongodb/mongo //儲存路徑 port: 27018 //連接埠號碼[[email protected] ~]# mkdir -p /data/mongodb/[[email protected] ~]# cd /data/mongodb/[[email protected] mongodb]# mkdir mongo[[email protected] mongodb]# touch mongod2.log[[email protected] mongodb]# chmod 777 mongod2.log[[email protected] mongodb]# mongod -f /etc/mongod2.confabout to fork child process, waiting until server is ready for connections.forked process: 51823child process started successfully, parent exiting[[email protected] mongodb]# mongo --port 27018MongoDB shell version v3.6.6connecting to: mongodb://127.0.0.1:27018/MongoDB server version: 3.6.6Server has startup warnings: 2018-07-16T16:21:43.218+0800 I CONTROL [initandlisten] > exitbye[[email protected] mongodb]# netstat -antpActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 44010/mongod tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 51823/mongod tcp 0 0 127.0.0.1:49076 127.0.0.1:27018 TIME_WAIT - tcp 0 0 192.168.200.184:22 192.168.200.1:60220
三、MongoDB基本操作
> use mydb //建立並開啟mydb資料庫switched to db mydb> a=db.info.find() //起別名> db.createCollection(‘a‘) //向mydb資料庫添加集合a{ "ok" : 1 }> show collections //顯示資料庫中的所有集合a> typeof(a.id) //查看屬性類型2018-07-16T17:51:21.735+0800 E QUERY [thread1] ReferenceError: a is not defined :@(shell):1:1> db.a.insert({"id":1,"name":"zhangsan","hobby":["game","talk","read"]})WriteResult({ "nInserted" : 1 })["game","talk","read"] //字串數組 object(對象)> db.a.find() //顯示a集合中的所有文檔{ "_id" : ObjectId("5b4c58ddf7c9ed9a709175e9"), "id" : 1, "name" : "zhangsan", "hobby" : [ "game", "talk", "read" ] }> db.a.insert({"id":2,"name":"lisi","hobby":["game","talk","read"]})WriteResult({ "nInserted" : 1 })> db.a.insert({"id":3,"name":"wangwu","hobby":["game","talk","read"]})WriteResult({ "nInserted" : 1 })> db.a.find(){ "_id" : ObjectId("5b4c58ddf7c9ed9a709175e9"), "id" : 1, "name" : "zhangsan", "hobby" : [ "game", "talk", "read" ] }{ "_id" : ObjectId("5b4c67c5bacc9efbe3a0d477"), "id" : 2, "name" : "lisi", "hobby" : [ "game", "talk", "read" ] }{ "_id" : ObjectId("5b4c67ccbacc9efbe3a0d478"), "id" : 3, "name" : "wangwu", "hobby" : [ "game", "talk", "read" ] }> db.a.findOne({"id":1}) //尋找指定記錄{ "_id" : ObjectId("5b4c58ddf7c9ed9a709175e9"), "id" : 1, "name" : "zhangsan", "hobby" : [ "game", "talk", "read" ]}> db.a.update({"id":2},{$set:{"name":"xiaoqi"}}) //更改屬性WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.a.find(){ "_id" : ObjectId("5b4c58ddf7c9ed9a709175e9"), "id" : 1, "name" : "zhangsan", "hobby" : [ "game", "talk", "read" ] }{ "_id" : ObjectId("5b4c67c5bacc9efbe3a0d477"), "id" : 2, "name" : "xiaoqi", "hobby" : [ "game", "talk", "read" ] }{ "_id" : ObjectId("5b4c67ccbacc9efbe3a0d478"), "id" : 3, "name" : "wangwu", "hobby" : [ "game", "talk", "read" ] }> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBmydb 0.000GB> use mydbswitched to db mydb> db.a.drop() //刪除集合true> show collections> db.dropDatabase() //刪除資料庫{ "dropped" : "mydb", "ok" : 1 }> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB
四、MongoDB日常維護
參數說明:
-d:指明資料庫的名字-c:指明集合的名字-f:指明要匯出哪些列-o:指明要匯出的檔案名稱-q:指明匯出資料的過濾條件
(1)匯入匯出
使用mongoexport和mongoimport命令來匯入匯出MongoDB的資料
> use schoolswitched to db school> db.createCollection(‘info‘){ "ok" : 1 }> for(var i=1;i<=100;i++)db.info.insert({"id":i,"name":"jack"+i})WriteResult({ "nInserted" : 1 })> db.info.find(){ "_id" : ObjectId("5b4ca7864f78ab07f2b572da"), "id" : 1, "name" : "jack1" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572db"), "id" : 2, "name" : "jack2" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572dc"), "id" : 3, "name" : "jack3" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572dd"), "id" : 4, "name" : "jack4" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572de"), "id" : 5, "name" : "jack5" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572df"), "id" : 6, "name" : "jack6" > db.info.count() //顯示行數100[[email protected] ~]# mongoexport -d school -c info -o /opt/school.json //這裡注意匯出的檔案格式為*.jsonc2018-07-16T22:13:22.668+0800 connected to: localhost2018-07-16T22:13:22.675+0800 exported 100 records[[email protected] ~]# cd /opt[[email protected] opt]# lsrh school.json[[email protected] opt]# vim school.json [[email protected] opt]# mongoimport -d school -c test --file school.json2018-07-16T22:15:22.523+0800 connected to: localhost2018-07-16T22:15:22.700+0800 imported 100 documents[[email protected] opt]# mongo> use schoolswitched to db school> show tablesinfotest //匯入檔案test,是剛才匯出的內容> db.test.find(){ "_id" : ObjectId("5b4ca7864f78ab07f2b572da"), "id" : 1, "name" : "jack1" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572db"), "id" : 2, "name" : "jack2" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572dc"), "id" : 3, "name" : "jack3" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572dd"), "id" : 4, "name" : "jack4" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572de"), "id" : 5, "name" : "jack5" }{ "_id" : ObjectId("5b4ca7864f78ab07f2b572df"), "id" : 6, "name" : "jack6"[[email protected] opt]# mongoexport -d school -c info -q ‘{ "id":{"$eq":10}}‘ -o /opt/top10.json //篩選需要匯出的行數,-q後面有空格2018-07-16T22:20:20.610+0800 connected to: localhost2018-07-16T22:20:20.636+0800 exported 1 record[[email protected] opt]# cd /opt/[[email protected] opt]# lsrh school.json top10.json[[email protected] opt]# vim top10.json {"_id":{"$oid":"5b4ca7864f78ab07f2b572e3"},"id":10.0,"name":"jack10"}
(2)備份與恢複
使用Mongodump命令來備份MongoDB資料
-h:MongoDB所在伺服器位址-d:需要備份的資料庫執行個體-o:備份資料存放的位置,該目錄需提前建立
[[email protected] opt]# mkdir /backup[[email protected] opt]# mongodump -d school -o /backup/ //備份2018-07-16T22:43:29.828+0800 writing school.info to 2018-07-16T22:43:29.830+0800 writing school.test to 2018-07-16T22:43:29.836+0800 done dumping school.info (100 documents)2018-07-16T22:43:29.838+0800 done dumping school.test (100 documents)[[email protected] opt]# cd /backup/[[email protected] backup]# lsschool[[email protected] backup]# cd school/[[email protected] school]# lsinfo.bson info.metadata.json ==test.bson== test.metadata.json[[email protected] school]# mongorestore -d abc --dir=/backup/school/ //恢複 備忘:這裡的資料庫abc會自動建立,所以可以不存在2018-07-16T22:46:02.801+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
(3)複製資料庫
> show dbsabc 0.000GBadmin 0.000GBconfig 0.000GBlocal 0.000GBschool 0.000GB> show dbsabc 0.000GBadmin 0.000GBconfig 0.000GBlocal 0.000GBschool 0.000GB> db.copyDatabase("abc","abc1") //複製資料庫 { "ok" : 1 }> show dbsabc 0.000GBabc1 0.000GBadmin 0.000GBconfig 0.000GBlocal 0.000GBschool 0.000GB
(4)建立系統管理使用者
> use admin> db.createUser({"user":"root","pwd":"123","roles":["root"]})> db.auth("root","123")
(5)進程管理
> db.currentOp() //查看進程 "active" : true, "currentOpTime" : "2018-07-16T23:28:31.188+0800", "opid" : 19385, "secs_running" : NumberLong(0), > db.killOp(19385){ "info" : "attempting to kill op", "ok" : 1 }
每天一篇,學習業內領先的NoSQL資料庫=》MongoDB