MongoDB部署linux
系統環境:CentOS7 下載地址:http://mirrors.163.com/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.isomongodb
MongoDB安裝包版本:4.0.5 下載地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.5.tgz數據庫
在CentOS上安裝軟件基本就是一些命令,如下列出所使用的命令:centos
一、關閉防火牆瀏覽器
systemctl stop firewalld.servicespa
systemctl disable firewalld.service日誌
二、上傳安裝包並解壓code
tar zxvf mongodb-linux-x86_64-rhel70-4.0.5.tgzblog
三、剪切到/usr/local下排序
mv mongodb-linux-x86_64-rhel70-4.0.5 /usr/local/mongodb
四、建立MongoDB相應的文件夾
mkdir -p /data/db
mkdir -p /data/log
mkdir -p /data/conf 在當前文件夾下建立一個配置文件mongodb.conf,內容以下:
1 dbpath=/data/db 2 #logpath=/data/log/mongodb.log 3 bind_ip=0.0.0.0 4 port=27017
五、進入/usr/local/mongodb/bin目前啓動MongoDB
./mongod --config /data/conf/mongodb.conf
六、用瀏覽器訪問如出現以下即爲安裝成功
MongoDB使用指南
使用的鏈接客戶端是Robo 3T,下載地址:https://robomongo.org/download
以下爲我在本地建立的集合:
以下爲常常會使用到的查詢語句:
1 //查詢姓名爲小小的數據 2 3 db.getCollection('student').find({"name":"xiaoxiao"}) 4 5 //並列查詢and 6 7 db.getCollection('student').find({"name":"xiaoxiao","age":29}) 8 9 //查詢age小於29 10 11 db.getCollection('student').find({"age":{$lt:29}}) 12 13 //或 查詢語句 14 15 db.getCollection('student').find({$or:[{"age":28},{"age":30}]}) 16 17 //limit查詢 18 19 db.getCollection('student').find({}).limit(2) 20 21 //skip跳過前兩條進行 查詢 22 23 db.getCollection('student').find({}).skip(2) 24 25 //按age字段進行升序排序(1),降序排序(-1) 26 27 db.getCollection('student').find({}).sort({"age":1}) 28 29 //模糊查詢name值帶有xiao(相似於SQL中的like) 30 31 db.getCollection('student').find({"name":{"$regex":"xiao"}})
過濾出status爲A的數據並分別統計出每一個name總的年齡:
1 db.getCollection('student').aggregate([ 2 {$match:{"status":"A"}}, 3 {$group:{_id:"$name",total:{$sum:"$age"}}} 4 ])
MongoDB監控
MongoDB監控可使用自帶的命令進行監控,固然也可使用zabbix。
這裏介紹下兩個MongoDB自帶的命令用於監控:
一、mongotop
二、mongostat
MongoDB調優
MongoDB數據庫不少用於日誌的記錄、地理位置存儲,因此在調優上主要涉及到比較多的是數據庫索引。
如何建立MongoDB的索引呢?
db.getCollection('student').ensureIndex({"age":1,"name":-1},{background:true})
如上建立了age字段正序,那麼字段倒序的索引。