本文檔參考 http://www.itdecent.cn/p/44499e245b38
Debain 和 CentOS 區(qū)別
1.軟件包管理工具不一樣
2.一些配置文件路徑不一樣
3.一些默認(rèn)設(shè)置不一樣
MongoDB應(yīng)用場景
1.基于地理位置計算
2.日志存儲
3.不同用戶不同選項
4.評論信息
5.問卷調(diào)查,《易企秀》
MongoDB 應(yīng)用場景? - 阿里云云棲社區(qū)的回答 - 知乎
https://www.zhihu.com/question/32071167/answer/147896283
MongoDB 和 mysql 區(qū)別
mongo
json 格式 http://www.bejson.com/
{"FirstName":"ke","LastName":"me","email":"hikeme@aa"}
格式化之后:
{
"FirstName": "zhang",
"LastName": "ya",
"email": "zhangya@oldboy.com",
"job": "linux"
}
mysql
user表
FirstName LastName email job
zhang ya zhangya@oldboy.com linux
MongoDB 特點
1.高性能,尤其是支持嵌入式數(shù)據(jù)模型減少數(shù)據(jù)庫系統(tǒng)上的I/O操作
2.高可用性,MongoDB的復(fù)制工具,副本集,提供自動故障轉(zhuǎn)移的數(shù)據(jù)冗余
3.水平可擴(kuò)展性
4.支持多種存儲引擎:WiredTiger存儲引擎
MongoDB 包含的存儲程序
Drivers
Atlas 云擴(kuò)展
看不慣云計算公司流氓行為,MongoDB 更改開源協(xié)議
https://www.oschina.net/news/100948/mongodb-switches-up-its-open-source-license
安裝部署
安裝服務(wù)套路:
1.規(guī)劃目錄
2.下載軟件包
3.解壓到指定目錄
4.創(chuàng)建數(shù)據(jù)目錄
5.創(chuàng)建用戶
6.更改目錄、授權(quán)
7.修改配置文件
8.啟動
9.測試
10.編寫啟動關(guān)停腳本
yum install libcurl openssl -y
mkdir /opt/mongo_cluster/ -p
#mkdir /data/soft -p
cd /data/soft/
#wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.13.tgz
tar zxvf mongodb-linux-x86_64-3.6.13.tgz -C /opt/mongo_cluster/
cd /opt/mongo_cluster/ && ln -s mongodb-linux-x86_64-3.6.13 mongodb
mkdir /opt/mongo_cluster/mongo_27017/{conf,logs,pid} -p
mkdir /data/mongo_cluster/mongo_27017 -p
創(chuàng)建完后目錄規(guī)劃:

image.png
配置啟動
1. 修改配置文件:
cat > /opt/mongo_cluster/mongo_27017/conf/monogdb.conf << EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongo_cluster/mongo_27017/logs/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/mongo_cluster/mongo_27017
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/mongo_cluster/mongo_27017/pid/mongod.pid
net:
port: 27017
bindIp: 127.0.0.1,10.0.0.51
EOF
2. 啟動命令
cd /opt/mongo_cluster/
mongodb/bin/mongod -f mongo_27017/conf/mongodb.conf
3.檢查
ps -ef|grep mongo
netstat -lntup|grep 27017
4.登錄
mongodb/bin/mongo db01:27017
5.關(guān)閉
mongodb/bin/mongod -f mongo_27017/conf/monogdb.conf --shutdown
6.寫入環(huán)境變量
echo 'PATH=$PATH:/opt/mongodb/bin' >> /etc/profile
tail -1 /etc/profile
PATH=$PATH:/opt/mongodb/bin
source /etc/profile
優(yōu)化警告
============= 解決大內(nèi)存頁警告 =============
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
vim /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides: disable-transparent-hugepages
# Required-Start: $local_fs
# Required-Stop:
# X-Start-Before: mongod mongodb-mms-automation-agent
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description: Disable Linux transparent huge pages, to improve
# database performance.
### END INIT INFO
case $1 in
start)
if [ -d /sys/kernel/mm/transparent_hugepage ]; then
thp_path=/sys/kernel/mm/transparent_hugepage
elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
thp_path=/sys/kernel/mm/redhat_transparent_hugepage
else
return 0
fi
echo 'never' > ${thp_path}/enabled
echo 'never' > ${thp_path}/defrag
re='^[0-1]+$'
if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
then
# RHEL 7
echo 0 > ${thp_path}/khugepaged/defrag
else
# RHEL 6
echo 'no' > ${thp_path}/khugepaged/defrag
fi
unset re
unset thp_path
;;
esac
#授權(quán)目錄
chmod 755 /etc/init.d/disable-transparent-hugepages
#設(shè)置開機(jī)自啟
chkconfig --add disable-transparent-hugepages
#普通用戶啟動
/opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf --shutdown
#創(chuàng)建mongo用戶、密碼、授權(quán)
useradd mongo
echo '123456'|passwd --stdin mongo
chown -R mongo:mongo /opt/mongo_cluster
chown -R mongo:mongo /data/mongo_cluster
#切換用戶登陸
su - mongo
vim .bashrc
export PATH=/opt/mongo_cluster/mongodb/bin:$PATH
source .bashrc
mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
mongo
============= rlimits 警告 =============
vim /etc/profile
ulimit -u 64000
source /etc/profile
重啟mongo服務(wù)
mongo基本操作
> show databases #查庫
admin 0.000GB
config 0.000GB
local 0.000GB
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db
test
> use local #切換
switched to db local
> db #查看在哪個庫
local
> show tables
startup_log
>
幫助命令
> help
> db.help()
> db.stats()
#插入數(shù)據(jù)
db.test.insert({"name":"zhangya","age":27,"ad":"北京市朝陽區(qū)"})
db.test.insert({"name":"zhangya","age":27,"ad":"北京市朝陽區(qū)"})
db.test.insert({"name":"yazhang","age":28,"ad":"北京市朝陽區(qū)"})
db.test.insert({"name":"xiaozhang","age":28,"ad":"北京市朝陽區(qū)"})
db.test.insert({"name":"xiaozhang","age":28,"ad":"北京市朝陽區(qū)","sex":"boy"})
#批量插入多條數(shù)據(jù)
db.inventory.insertMany( [
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
]);
#查詢所有
db.test.find()
db.test.findOne()
db.inventory.find()
db.inventory.findOne()
#按條件查詢
> db.inventory.find({"status":"D"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82a"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
> db.inventory.find({"size.uom":"cm"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee828"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82c"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
> db.inventory.find({"size.uom":"cm","qty":{$lt: 50}})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee828"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82c"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
> db.inventory.find({"size.uom":"cm","qty":{$eq: 75}})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
#分解
db.inventory.find(
{
"size.uom":"cm",
"qty":
{
$lt:50
}
}
)
db.inventory.find( {
$and : [
{ $or : [ { "status" : "D" }, {"qty":{$lt: 50}} },
{ $or : [ { "status" : "A" }, {"qty":{$lt: 50} } ] }
]
})
#按條件和正則查詢
> myCursor = db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
{ "_id" : ObjectId("5d22c043b2adc5091f6ee828"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee829"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82c"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
> myCursor = db.inventory.find( {status: "A",$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee828"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82c"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
#分解
myCursor = db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
#更新語句
##更新前數(shù)據(jù)
> db.inventory.find({"item":"paper"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82a"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "cm" }, "status" : "P", "lastModified" : ISODate("2019-07-08T07:23:31.570Z") }
##更新語句(更新第一條數(shù)據(jù))
> db.inventory.updateOne({"item":"paper"},{$set:{"size.h":"10","size.w":"15","status":"D"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
##更新后數(shù)據(jù)
> db.inventory.find({"item":"paper"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82a"), "item" : "paper", "qty" : 100, "size" : { "h" : "10", "w" : "15", "uom" : "cm" }, "status" : "D", "lastModified" : ISODate("2019-07-08T07:23:31.570Z") }
#刪除數(shù)據(jù)
##刪除前數(shù)據(jù)
> db.inventory.find( {"status" : "D"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82a"), "item" : "paper", "qty" : 100, "size" : { "h" : "10", "w" : "15", "uom" : "cm" }, "status" : "D", "lastModified" : ISODate("2019-07-08T07:23:31.570Z") }
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
##刪除語句(刪除第一條數(shù)據(jù))
> db.inventory.deleteOne(
... {"status" : "D"}
... )
{ "acknowledged" : true, "deletedCount" : 1 }
##刪除后數(shù)據(jù)
> db.inventory.find( {"status" : "D"})
{ "_id" : ObjectId("5d22c043b2adc5091f6ee82b"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
mongo 分析工具,系統(tǒng)自帶
[mongo@db01 ~]$ mongotop
2019-07-08T15:39:09.899+0800 connected to: 127.0.0.1
ns total read write 2019-07-08T15:39:10+08:00
admin.system.roles 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
config.system.sessions 0ms 0ms 0ms
local.startup_log 0ms 0ms 0ms
local.system.replset 0ms 0ms 0ms
test.inventory 0ms 0ms 0ms
test.test 0ms 0ms 0ms
[mongo@db01 ~]$ mongostat
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 972M 45.0M 0|0 1|0 156b 59.9k 1 Jul 8 15:38:53.268
*0 *0 *0 *0 0 1|0 0.0% 0.0% 0 972M 45.0M 0|0 1|0 157b 60.5k 1 Jul 8 15:38:54.270
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 972M 45.0M 0|0 1|0 158b 60.7k 1 Jul 8 15:38:55.268
用戶授權(quán)認(rèn)證
use admin
db.createUser(
{
user: "admin",
pwd: "123456",
roles:[ { role: "root", db:"admin"}]}
)
修改配置文件
vim /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
security:
authorization: enabled
重啟服務(wù)
mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf --shutdown
mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
登錄
[mongo@db01 ~]$ mongo -uadmin -p
MongoDB shell version v3.6.13
Enter password:
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b97926dc-1614-4da8-96c0-67d8e930b195") }
MongoDB server version: 3.6.13
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
1.使用admin用戶登錄
mongo -uadmin -p
show dbs
2.切換到test庫,并創(chuàng)建一個用戶賦予不同庫的不同角色
use test
db.createUser(
{
user: "myTester",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "test2" } ]
}
)
3.在test庫下插入測試數(shù)據(jù)
db.test.insert({"name":"zhangya","age":27,"ad":"北京市朝陽區(qū)"})
db.test.insert({"name":"zhangya","age":27,"ad":"北京市朝陽區(qū)"})
db.test.insert({"name":"yazhang","age":28,"ad":"北京市朝陽區(qū)"})
db.test.insert({"name":"xiaozhang","age":28,"ad":"北京市朝陽區(qū)"})
4.在test2庫下插入測試數(shù)據(jù)
use test2
db.test2.insert({"name":"zhangya","age":27,"ad":"北京市昌平區(qū)"})
db.test2.insert({"name":"zhangya","age":27,"ad":"北京市昌平區(qū)"})
db.test2.insert({"name":"yazhang","age":28,"ad":"北京市昌平區(qū)"})
5.使用myTester用戶登錄到test驗證庫
mongo -umyTester -pxyz123 db01:27017/test
show dbs
db
6.切換到test庫,測試能否讀寫
use test
db.test.insert({"name":"58NB","age":27,"ad":"北京市朝陽區(qū)"})
db.test.find()
7.切換到test2庫,測試能否讀寫
use test2
db.test2.insert({"name":"58NB","age":27,"ad":"北京市昌平區(qū)"})
db.test2.find()
副本集配置
[mongo@db01 ~]$ 下執(zhí)行
1.創(chuàng)建配置文件目錄
mkdir -p /opt/mongo_cluster/mongo_2801{7,8,9}/{conf,logs,pid}
2.修改副本集配置文件
cat >/opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongo_cluster/mongo_28017/logs/mongodb.log
storage:
journal:
enabled: true
dbPath: /data/mongo_cluster/mongo_28017
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 0.5
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /opt/mongo_cluster/mongo_28017/pid/mongod.pid
net:
port: 28017
bindIp: 127.0.0.1,10.0.0.51
replication:
oplogSizeMB: 1024
replSetName: dba58
EOF
3.復(fù)制配置文件到其他目錄
cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28018/conf/mongo_28018.conf
cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28019/conf/mongo_28019.conf
4.修改配置文件里的端口
sed -i 's#28017#28018#g' /opt/mongo_cluster/mongo_28018/conf/mongo_28018.conf
sed -i 's#28017#28019#g' /opt/mongo_cluster/mongo_28019/conf/mongo_28019.conf
5.創(chuàng)建數(shù)據(jù)目錄
mkdir /data/mongo_cluster/mongo_2801{7,8,9}
6.啟動多實例
mongod -f /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf
mongod -f /opt/mongo_cluster/mongo_28018/conf/mongo_28018.conf
mongod -f /opt/mongo_cluster/mongo_28019/conf/mongo_28019.conf
7.初始化副本集
mongo db01:28017
> config = {
_id : "dba58",
members : [
{_id : 0, host : "db01:28017"},
{_id : 1, host : "db01:28018"},
{_id : 2, host : "db01:28019"},
] }
rs.initiate(config)
8.查看副本集狀態(tài)
dba58:PRIMARY> rs.status()
{
"set" : "dba58",
"date" : ISODate("2019-07-08T09:50:16.711Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "db01:28017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1805,
"optime" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-08T09:50:12Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1562579282, 1),
"electionDate" : ISODate("2019-07-08T09:48:02Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "db01:28018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 145,
"optime" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-08T09:50:12Z"),
"optimeDurableDate" : ISODate("2019-07-08T09:50:12Z"),
"lastHeartbeat" : ISODate("2019-07-08T09:50:16.648Z"),
"lastHeartbeatRecv" : ISODate("2019-07-08T09:50:15.644Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "db01:28017",
"syncSourceHost" : "db01:28017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "db01:28019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 145,
"optime" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1562579412, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-08T09:50:12Z"),
"optimeDurableDate" : ISODate("2019-07-08T09:50:12Z"),
"lastHeartbeat" : ISODate("2019-07-08T09:50:16.648Z"),
"lastHeartbeatRecv" : ISODate("2019-07-08T09:50:15.648Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "db01:28017",
"syncSourceHost" : "db01:28017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1562579412, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1562579412, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
權(quán)重調(diào)整+主庫降級
1.查看配置信息
rs.conf()
2.配置權(quán)重
config = rs.conf()
config.members[2].priority=100
3.重新導(dǎo)入配置信息
rs.reconfig(config)
4.主庫降級,發(fā)起重新選舉
rs.stepDown()
5.恢復(fù)成默認(rèn)權(quán)重
config = rs.conf()
config.members[2].priority=1
rs.reconfig(config)
rs.stepDown()
增加節(jié)點
1.安裝配置啟動新節(jié)點
mkdir /opt/mongo_cluster/mongo_28010/{conf,logs,pid} -p
mkdir /data/mongo_cluster/mongo_28010
cp /opt/mongo_cluster/mongo_28017/conf/mongo_28017.conf /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
sed -i 's#28017#28010#g' /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
mongo db01:28010
2.主庫執(zhí)行添加新節(jié)點操作
use admin
rs.add("db01:28010")
刪除舊節(jié)點
1.主庫操作,刪除舊節(jié)點
mongo db01:28017
rs.remove("db01:28010")
2.節(jié)點下線
mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf --shutdown
添加仲裁節(jié)點
1.清空28010節(jié)點的數(shù)據(jù)
rm -rf /data/mongo_cluster/mongo_28010/*
2.重新啟動
mongod -f /opt/mongo_cluster/mongo_28010/conf/mongo_28010.conf
3.檢查
mongo db01:28010
4.主庫操作
mongo db01:28017
rs.addArb("db01:28010")
5.查看節(jié)點狀態(tài)
mongo db01:28010
數(shù)據(jù)備份導(dǎo)出
創(chuàng)建備份目錄并授權(quán)(注意在不同用戶下操作)
[root@db01 ~]# mkdir /backup
[root@db01 ~]# chown -R mongo:mongo /backup/
導(dǎo)出數(shù)據(jù)
[mongo@db01 ~]$ mongoexport --port 28017 -d test -c inventory -o /backup/log.json
2019-07-08T19:20:43.728+0800 connected to: localhost:28017
2019-07-08T19:20:43.729+0800 exported 5 records
查看導(dǎo)出數(shù)據(jù)
[mongo@db01 /backup]$ cat log.json
{"_id":{"$oid":"5d23149b20a88ce703b89dbf"},"item":"journal","qty":25.0,"size":{"h":14.0,"w":21.0,"uom":"cm"},"status":"A"}
{"_id":{"$oid":"5d23149b20a88ce703b89dc0"},"item":"notebook","qty":50.0,"size":{"h":8.5,"w":11.0,"uom":"in"},"status":"A"}
{"_id":{"$oid":"5d23149b20a88ce703b89dc1"},"item":"paper","qty":100.0,"size":{"h":8.5,"w":11.0,"uom":"in"},"status":"D"}
{"_id":{"$oid":"5d23149b20a88ce703b89dc2"},"item":"planner","qty":75.0,"size":{"h":22.85,"w":30.0,"uom":"cm"},"status":"D"}
{"_id":{"$oid":"5d23149b20a88ce703b89dc3"},"item":"postcard","qty":45.0,"size":{"h":10.0,"w":15.25,"uom":"cm"},"status":"A"}
備份恢復(fù)
導(dǎo)出
mongoexport --port 28017 -d dba -c inventory -o /backup/log.json
mongoexport --port 28017 -d dba -c inventory --type=csv -f item,qty,size,status -o /backup/log.csv
導(dǎo)入
mongoimport --port 28017 -d oldboy -c inventory /backup/log.json
導(dǎo)入CSV
mongoimport --port 28017 -d dba58 -c log2 --type=csv --headerline -f item,qty,size,status --file /backup/log.csv
mysql導(dǎo)出csv
select * from world.city into outfile '/tmp/city1.csv' fields terminated by ',';
導(dǎo)入csv
mongoimport --port 28017 -d world -c city --type=csv -f ID,Name,CountryCode,District,Population --file /tmp/city1.csv
全備
mongodump --port 28017 -o /data/backup
只備一個庫
mongodump --port 28017 -d world -o /data/backup
恢復(fù):
mongorestore --port 28017 -d world1 /data/backup/world/
恢復(fù)之前先刪除
mongorestore --port 28017 -d world1 /data/backup/world/ --drop
今天回顧:
1.sql和nosql關(guān)系
2.mongo介紹
3.mongo的應(yīng)用場景
4.安裝部署
5.修改配置文件,啟動關(guān)閉
6.優(yōu)化警告
7.插入
- 單條插入
- 多條插入
8.查詢
- 查詢所有
- 按條件查詢
- 按條件和正則查詢
9.更新
10.刪除
- 按條件刪除
- 刪除整個庫或者表
11.授權(quán)
- 創(chuàng)建admin用戶
- 創(chuàng)建普通用戶授予不同角色和不同權(quán)限
12.副本集操作
- 配置文件添加副本集參數(shù)
- 啟動所有節(jié)點
- 配置副本集命令
- 初始化副本集
- 故障轉(zhuǎn)移
- 增加節(jié)點
- 刪除節(jié)點
- 仲裁節(jié)點
- 權(quán)重調(diào)整,主庫降級
- 故障案例,4臺競爭,只剩一臺,老張的憂傷
13.工具使用
mongostat
mongotop
14.備份恢復(fù)
mongoexport/mongoimport:json csv
- 異構(gòu)平臺遷移 mysql <---> mongodb
mongodump/mongorestore
-日常備份恢復(fù)時使用.
15.故障恢復(fù)案例
- 全備
- 誤操作
- 查詢誤操作時間點
- 備份oplog
- 恢復(fù)到誤操作時間點以前的數(shù)據(jù)
- 檢查數(shù)據(jù)是否恢復(fù)完整
報錯記錄1
主庫插入數(shù)據(jù)
dba58:PRIMARY>
db.inventory.insertMany( [
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
]);
從庫查看數(shù)據(jù)
dba58:SECONDARY> show tables
2019-07-08T18:03:06.965+0800 E QUERY [thread1] Error: listCollections failed: {
"operationTime" : Timestamp(1562580184, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1562580184, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:941:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:953:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:964:16
shellHelper.show@src/mongo/shell/utils.js:853:9
shellHelper@src/mongo/shell/utils.js:750:15
@(shellhelp2):1:1
臨時解決
dba58:SECONDARY> rs.slaveOk()
永久解決
[mongo@db01 ~]$ echo "rs.slaveOk();" >> .mongorc.js
[mongo@db01 ~]$ cat .mongorc.js
rs.slaveOk();
報錯記錄2
從庫執(zhí)行命令
db.inventory.deleteOne( {"item" : "journal"})
2019-07-08T18:03:45.247+0800 E QUERY [thread1] WriteCommandError: not master :
WriteCommandError({
"operationTime" : Timestamp(1562580215, 1),
"ok" : 0,
"errmsg" : "not master",
"code" : 10107,
"codeName" : "NotMaster",
"$clusterTime" : {
"clusterTime" : Timestamp(1562580215, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
})
問題原因:
從庫不能執(zhí)行增刪改命令
報錯記錄3
2019-07-08T18:58:31.608+0800 I NETWORK [thread1] trying reconnect to db01:28010 (10.0.0.51) failed
2019-07-08T18:58:31.609+0800 I NETWORK [thread1] reconnect db01:28010 (10.0.0.51) ok
dba58:OTHER>
dba58:OTHER>
問題原因:
不在集群內(nèi)或者被集群移除了
報錯記錄4
> db.test.insert({"name":"yazhangya","age":27,"ad":"北京市朝陽區(qū)"})
WriteResult({
"writeError" : {
"code" : 13,
"errmsg" : "not authorized on test2 to execute command { insert: \"test\", ordered: true, lsid: { id: UUID(\"01431e12-5d4a-47a9-8ac4-657ab08fda7c\") }, $db: \"test2\" }"
}
})
問題原因:
當(dāng)前登錄的用戶對這個庫沒有寫入權(quán)限