21.36 mongodb分片介紹mysql
21.37/21.38/21.39 mongodb分片搭建linux
21.40 mongodb分片測試web
21.41 mongodb備份恢復sql
21.36 mongodb分片介紹mongodb
分片是由一個一個的副本集組成的數據庫
1.分片就是將數據庫進行拆分,將大型集合分隔到不一樣服務器上。好比,原本100G的數據,能夠分割成10份存儲到10臺服務器上,這樣每臺機器只有10G的數據。json
2.經過一個mongos的進程(路由分發)實現分片後的數據存儲與訪問,也就是說mongos是整個分片架構的核心,對客戶端而言是不知道是否有分片的,客戶端只須要把讀寫操做轉達給mongos便可。vim
3.雖然分片會把數據分隔到不少臺服務器上,可是每個節點都是須要有一個備用角色的,這樣能保證數據的高可用。緩存
#節點其實就是一個副本集。能夠理解成分片的下一級別的單位就是副本集。分片是由副本集組成的安全
4.當系統須要更多空間或者資源的時候,分片可讓咱們按需方便擴展,只須要把mongodb服務的機器加入到分片集羣中便可
#支持橫向擴展,不夠了的時候直接加副本集節點進來就能夠了
MongoDB分片架構圖
MongoDB分片相關概念
1.mongos: 數據庫集羣請求的入口,全部的請求都經過mongos進行協調,不須要在應用程序添加一個路由選擇器,mongos本身就是一個請求分發中心,它負責把對應的數據請求請求轉發到對應的shard服務器上。在生產環境一般有多mongos做爲請求的入口,防止其中一個掛掉全部的mongodb請求都沒有辦法操做。
#就是實現一個路由,反饋,的分發
2.config server: 配置服務器,存儲全部數據庫元信息(路由、分片)的配置。mongos自己沒有物理存儲分片服務器和數據路由信息,只是緩存在內存裏,配置服務器則實際存儲這些數據。mongos第一次啓動或者關掉重啓就會從 config server 加載配置信息,之後若是配置服務器信息變化會通知到全部的 mongos 更新本身的狀態,這樣 mongos 就能繼續準確路由。在生產環境一般有多個 config server 配置服務器,由於它存儲了分片路由的元數據,防止數據丟失!
#存儲配置的
3.shard: 存儲了一個集合部分數據的MongoDB實例,每一個分片是單獨的mongodb服務或者副本集,在生產環境中,全部的分片都應該是副本集。
#存儲配置的
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21.37/21.38/21.39 mongodb分片搭建
一.分片搭建 -服務器規劃
三臺機器 A B C
A搭建:mongos、config server、副本集1主節點、副本集2仲裁、副本集3從節點
#至關於每一個機器上都要開啓這五個端口
B搭建:mongos、config server、副本集1從節點、副本集2主節點、副本集3仲裁
C搭建:mongos、config server、副本集1仲裁、副本集2從節點、副本集3主節點
端口分配:mongos 20000、config 21000、副本集1 2700一、副本集2 2700二、副本集3 27003
三臺機器所有關閉firewalld服務和selinux,或者增長對應端口的規則
二.分片搭建 – 建立目錄
分別在三臺機器上建立各個角色所須要的目錄
#每臺機器都要執行這個五條
mkdir -p /data/mongodb/mongos/log
mkdir -p /data/mongodb/config/{data,log}
mkdir -p /data/mongodb/shard1/{data,log}
mkdir -p /data/mongodb/shard2/{data,log}
mkdir -p /data/mongodb/shard3/{data,log}
三.分片搭建–config server配置(config server)
1.mongodb3.4版本之後須要對config server建立副本集
2.添加配置文件(三臺機器都操做)
3.mkdir /etc/mongod/
#分別建立三臺配置文件的目錄,三臺都要操做
4.vim /etc/mongod/config.conf(也就是config sever的配置文件) //加入以下內容
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 192.168.208.128 #內網IP。也能夠所有監聽0.0.0.0(不安全)
port = 21000 #指定端口數
fork = true #進程相關
configsvr = true #declare this is a config db of a cluster;
replSet=configs #副本集名稱
maxConns=20000 #設置最大鏈接數
5.啓動三臺機器的config server
mongod -f /etc/mongod/config.conf(啓動的時候加-f指定他的配置文件就能夠了) //三臺機器都要操做
6.登陸任意一臺機器的21000端口,初始化副本集
mongo --port 21000
config = { _id: "configs", members: [ {_id : 0, host : "192.168.133.130:21000"},{_id : 1, host : "192.168.133.132:21000"},{_id : 2, host : "192.168.133.133:21000"}] }
#config命令,重要一步,將三個機器聯繫起來
rs.initiate(config) #初始化
{ "ok" : 1 }
四.分片搭建–分片配置(shard)
#分片至少要保證有三個副本集進來,就是下面的shard1 2 3
添加配置文件(三臺機器都操做)
#每一臺機器都要添加這三個配置文件(shard1 2 3 )
1.shard1配置:
1.1vim /etc/mongod/shard1.conf //加入以下內容
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 192.168.208.128 #01機器的內網ip
port = 27001
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard1 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
shard2配置:
2.1vim /etc/mongod/shard2.conf //加入以下內容
pidfilepath = /var/run/mongodb/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 192.168.208.130 #02機器的內網ip
port = 27002
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard2 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
shard3配置:
3.1vim /etc/mongod/shard3.conf //加入以下內容
pidfilepath = /var/run/mongodb/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 192.168.208.133 #03機器的內網ip
port = 27003
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard3 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
2.啓動shard1,並把shard1的副本集搭建起來
mongod -f /etc/mongod/shard1.conf //三臺機器都要操做
登陸128或者130任何一臺機器的27001端口初始化副本集,133之因此不行,是由於shard1咱們把133這臺機器的27001端口做爲了仲裁節點
mongo --host 192.168.208.128 --port 27001
use admin
config = { _id: "shard1", members: [ {_id : 0, host : "192.168.208.128:27001"}, {_id: 1,host : "192.168.208.130:27001"},{_id : 2, host : "192.168.208.133:27001",arbiterOnly:true}] }
rs.initiate(config)
啓動shard2,並把shard2的副本集搭建起來
mongod -f /etc/mongod/shard2.conf //三臺機器都要操做
登陸130或者133任何一臺機器的27002端口初始化副本集,128之因此不行,是由於shard2咱們把128這臺機器的27002端口做爲了仲裁節點
mongo --host 192.168.208.130 --port 27002
use admin
config = { _id: "shard2", members: [ {_id : 0, host : "192.168.208.128:27002" ,arbiterOnly:true},{_id : 1, host : "192.168.208.130:27002"},{_id : 2, host : "192.168.208.133:27002"}] }
rs.initiate(config)
啓動shard3,並把shard3的副本集搭建起來
mongod -f /etc/mongod/shard3.conf //三臺機器都要操做
登陸128或者133任何一臺機器的27003端口初始化副本集,130之因此不行,是由於shard3咱們把130這臺機器的27003端口做爲了仲裁節點
mongo --host 192.168.208.133 --port 27003
use admin
config = { _id: "shard3", members: [ {_id : 0, host : "192.168.208.128:27003"}, {_id : 1, host : "192.168.208.130:27003", arbiterOnly:true}, {_id : 2, host : "192.168.208.133:27003"}] }
rs.initiate(config)
五.分片搭建–配置路由服務器(mongos)
mongos放在最後的緣由是,mongos要想啓動起來,他先要知道你的config server是誰、還有那三個shard(副本集)都是誰
mongos沒有數據,他不須要存數據,因此他不須要data的指定目錄
添加配置文件(三臺機器都操做)
1.vim /etc/mongod/mongos.conf //加入以下內容
pidfilepath = /var/run/mongodb/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = configs/192.168.208.128:21000,192.168.208.128:21000,192.168.208.128:21000
#關鍵,指定config server
#監聽的配置服務器,只能有1個或者3個,configs爲配置服務器的副本集名字
maxConns=20000 #設置最大鏈接數
2.啓動mongos服務,注意命令,前面都是mongod,這裏是mongos
mongos -f /etc/mongod/mongos.conf
六.分片搭建–啓用分片
登陸任何一臺20000端口
mongo --port 20000
把全部分片和路由器串聯(也就是進入mongos鏈接分片{shard1 2 3})
sh.addShard("shard1/192.168.208.128:27001,192.168.208.130:27001,192.168.208.133:27001") #注意語法,逗號分隔沒有空格。這是鏈接shard1
sh.addShard("shard2/192.168.208.128:27002,192.168.208.130:27002,192.168.208.133:27002") #注意語法,逗號分隔沒有空格。這是鏈接shard2
sh.addShard("shard3/192.168.208.128:27003,192.168.208.130:27003,192.168.208.133:27003") #注意語法,逗號分隔沒有空格。這是鏈接shard3
查看集羣狀態
sh.status() #查看分片。與rs.status()(查看副本集)不同
實例:
[root@axinlinux-01 ~]# mkdir -p /data/mongodb/mongos/log
[root@axinlinux-01 ~]# mkdir -p /data/mongodb/config/{data,log}
[root@axinlinux-01 ~]# mkdir -p /data/mongodb/shard1/{data,log}
[root@axinlinux-01 ~]# mkdir -p /data/mongodb/shard2/{data,log}
[root@axinlinux-01 ~]# mkdir -p /data/mongodb/shard3/{data,log}
[root@axinlinux-01 ~]# mkdir /etc/mongod/ #建立他的配置文件目錄
#!!02 03上也要這樣建立目錄,這裏就不作演示了
[root@axinlinux-01 ~]# vim /etc/mongod/config.conf
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 192.168.208.128 #內網IP
port = 21000
fork = true #進程相關
configsvr = true #declare this is a config db of a cluster;
replSet=configs #副本集名稱
maxConns=20000 #設置最大鏈接數
#!!以上,02 03機器也要這樣操做。注意監聽IP要修改本機
[root@axinlinux-01 ~]# mongod -f /etc/mongod/config.conf #-f指定他的配置文件。這樣把它啓動起來
about to fork child process, waiting until server is ready for connections.
forked process: 2070
child process started successfully, parent exiting
[root@axinlinux-01 ~]# ps aux |grep mongod #查看有沒有指定配置文件的進程
root 2070 5.2 2.6 1083880 49716 ? Sl 21:08 0:01 mongod -f /etc/mongod/config.conf
[root@axinlinux-01 ~]# netstat -lntp #還要查看有沒有指定的端口數
tcp 0 0 192.168.208.128:21000 0.0.0.0:* LISTEN 2070/mongod
#!!以上,02 03都要這樣-f指定剛建好的配置文件,將他啓動起來。並查看進程和端口是否是咱們指定的
[root@axinlinux-01 ~]# mongo --host 192.168.208.128 --port 21000 #進入任何一個機器裏
> config = { _id: "configs", members: [ {_id : 0, host : "192.168.208.128:21000"},{_id : 1, host : "192.168.208.130:21000"},{_id : 2, host : "192.168.208.133:21000"}] } #執行該命令
{
"_id" : "configs",
"members" : [
{
"_id" : 0,
"host" : "192.168.208.128:21000"
},
{
"_id" : 1,
"host" : "192.168.208.130:21000"
},
{
"_id" : 2,
"host" : "192.168.208.133:21000"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
四.分片搭建–分片配置
[root@axinlinux-01 ~]# vim /etc/mongod/shard1.conf
pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0 #可設置爲01機器的內網ip。爲方便實驗設置了監聽全部網段,不安全!
port = 27001
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard1 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
[root@axinlinux-01 ~]# vim /etc/mongod/shard2.conf
pidfilepath = /var/run/mongodb/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0 #可設置爲02機器的內網ip。爲方便實驗設置了監聽全部網段,不安全!
port = 27002
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard2 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
[root@axinlinux-01 ~]# vim /etc/mongod/shard3.conf
pidfilepath = /var/run/mongodb/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0 #可設置爲02機器的內網ip。爲方便實驗設置了監聽全部網段,不安全!
port = 27003
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard3 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
#!!注意三臺機器都要添加這三個shard 1 2 3 的文件。02 03的監聽內網ip和pid文件的數字要注意修改
阿鑫在實驗的時候,就是沒有每臺機器配置三個shard.conf,致使出錯。
!!總結:每臺機器都要配置這三個shard配置文件。而且每臺都要開啓相應的shard 1 2 3 服務!!
[root@axinlinux-01 ~]# mongod -f /etc/mongod/shard1.conf #開啓shard1這個服務
#02 03機器也要開啓shard1.conf這個服務
[root@axinlinux-01 ~]# mongo --host 192.168.208.128 --port 27001 #進入128機器
> config = { _id: "shard1", members: [ {_id : 0, host : "192.168.208.128:27001"}, {_id: 1,host : "192.168.208.130:27001"},{_id : 2, host : "192.168.208.133:27001",arbiterOnly:true}] }
> rs.initiate(config)
{ "ok" : 1 } #ok才能夠
[root@axinlinux-02 ~]# mongod -f /etc/mongod/shard2.conf #三臺機器上都要開啓這個shard2服務
[root@axinlinux-02 ~]# mongo --port 27002 #要在02 機器上登陸mongodb,是由於shard2咱們把128這臺機器的27002端口做爲了仲裁節點
> config = { _id: "shard2", members: [ {_id : 0, host : "192.168.208.128:27002" ,arbiterOnly:true},{_id : 1, host : "192.168.208.130:27002"},{_id : 2, host : "192.168.208.133:27002"}] }
> rs.initiate(config)
{ "ok" : 1 }
[root@axinlinux-01 mongod]# mongod -f /etc/mongod/shard3.conf #全部機器上都要啓動shard3服務
[root@axinlinux-01 mongod]# mongo --port 27003 #在01機器上登陸mongod
> use admin
> config = { _id: "shard3", members: [ {_id : 0, host : "192.168.208.128:27003"}, {_id : 1, host : "192.168.208.130:27003", arbiterOnly:true}, {_id : 2, host : "192.168.208.133:27003"}] }
> rs.initiate(config)
{ "ok" : 1 }
五.分片搭建–配置路由服務器(mongos)
[root@axinlinux-01 ~]# vim /etc/mongod/mongos.conf #建立mongos
cat /etc/mongod/mongos.conf
pidfilepath = /var/run/mongodb/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = configs/192.168.208.128:21000,192.168.208.128:21000,192.168.208.128:21000
#關鍵,指定config server
#監聽的配置服務器,只能有1個或者3個,configs爲配置服務器的副本集名字
maxConns=20000 #設置最大鏈接數
#以上建立mongos,02 03機器上也要作
[root@axinlinux-01 ~]# scp /etc/mongod/mongos.conf 192.168.208.130:/etc/mongod/ #可以使用scp
[root@axinlinux-01 ~]# scp /etc/mongod/mongos.conf 192.168.208.133:/etc/mongod/
[root@axinlinux-01 ~]# mongos -f /etc/mongod/mongos.conf #01 02 03均開啓mongos
[root@axinlinux-02 ~]# mongos -f /etc/mongod/mongos.conf
[root@axinlinux-03 ~]# mongos -f /etc/mongod/mongos.conf
六.分片搭建–啓用分片
[root@axinlinux-01 ~]# mongo --port 20000 #任何一臺機器上登陸2000端口。也就是進入了mongos
mongos> sh.addShard("shard1/192.168.208.128:27001,192.168.208.130:27001,192.168.208.133:27001")
#注意語法,ip之間逗號鏈接,沒有空格。鏈接shard1
{ "shardAdded" : "shard1", "ok" : 1 } #顯示ok爲1正常
mongos> sh.addShard("shard2/192.168.208.128:27002,192.168.208.130:27002,192.168.208.133:27002")
{ "shardAdded" : "shard2", "ok" : 1 } #鏈接shard2
mongos> sh.addShard("shard3/192.168.208.128:27003,192.168.208.130:27003,192.168.208.133:27003")
{ "shardAdded" : "shard3", "ok" : 1 } #鏈接shard3
mongos> sh.status() #查看分片
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5bf419a348657fd107487a17")
}
shards: #正常的話就會看到下面這三個
{ "_id" : "shard1", "host" : "shard1/192.168.208.128:27001,192.168.208.130:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.208.130:27002,192.168.208.133:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.208.128:27003,192.168.208.133:27003", "state" : 1 }
active mongoses:
"3.4.18" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no #沒有啓動是由於,尚未建立任何的庫和表
NaN
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21.40 mongodb分片測試
1.登陸任何一臺20000端口
2.mongo --port 20000
3.use admin #進入admin庫
4.db.runCommand({ enablesharding : "testdb"}) 或者
sh.enableSharding("testdb") //指定要分片的數據庫
#這兩條命令都是同樣的,都是指定要分片的庫。沒有的話會被建立
5.db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } ) 或者
sh.shardCollection("testdb.table1",{"id":1} ) //#指定數據庫裏須要分片的集合和片鍵
#也就是testdb庫裏的table1集合
6.use testdb #在進入咱們剛建立的testdb庫
7.for (var i = 1; i <= 10000; i++) db.table1.save({id:i,"test1":"testval1"})//插入測試數據
#在測試庫裏插入10000條數據
8.db.table1.stats()//查看table1狀態
#多建幾個會自動的均衡分到各個sgard裏面去
實例:
[root@axinlinux-01 ~]# mongo --port 20000 #進入20000
mongos> use admin #要先進入admin
switched to db admin
mongos> db.runCommand({ enablesharding : "testdb"}) #建立testdb測試庫
{ "ok" : 1 }
mongos> db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } ) #指定要分片的集合
{ "collectionsharded" : "testdb.table1", "ok" : 1 }
mongos> use testdb #進入測試庫
switched to db testdb
mongos> for (var i = 1; i <= 10000; i++) db.table1.save({id:i,"test1":"testval1"}) #插入一些數據(10000條)
WriteResult({ "nInserted" : 1 })
mongos> show dbs #能夠看一下
admin 0.000GB
config 0.001GB
testdb 0.000GB
mongos> sh .status() #看一下分片狀態
。。。
databases:
{ "_id" : "testdb", "primary" : "shard2", "partitioned" : true } #這能夠看到咱們建立的testdb
testdb.table1
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard2 1 #這些數據在shard2裏面
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0)
mongos> sh.enableSharding("db2") #爲知足實驗效果,在多建立幾個庫和要分片的集合。db2庫裏的axin2集合
{ "ok" : 1 }
mongos> sh.shardCollection("db2.axin2",{"id":1} )
{ "collectionsharded" : "db2.axin2", "ok" : 1 }
mongos> sh.enableSharding("db3") #爲知足實驗效果,在多建立幾個庫和要分片的集合。db2庫裏的axin2集合
{ "ok" : 1 }
mongos> sh.shardCollection("db3.axin3",{"id":1} )
{ "collectionsharded" : "db3.axin3", "ok" : 1 }
mongos> sh.status() #咱們再來看一下分片
。。。。。
databases:
{ "_id" : "testdb", "primary" : "shard2", "partitioned" : true }
testdb.table1
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard2 1 #能夠看到testdb1仍是在shard2裏
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0)
{ "_id" : "db2", "primary" : "shard3", "partitioned" : true }
db2.axin2
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard3 1 #db2被分到了shaed3裏
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard3 Timestamp(1, 0)
{ "_id" : "db3", "primary" : "shard1", "partitioned" : true }
db3.axin3
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard1 1 #db3被分到了shard1裏
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21.41 mongodb備份恢復
!!咱們使用了分片那麼在備份的時候,也是要到分片的這個端口裏去備份的(也就是20000,mongos服務的那個端口)
MongoDB備份
1.備份指定庫
mongodump --host 127.0.0.1 --port 20000 -d mydb -o /tmp/mongobak
#備份使用mongodump來備份,跟mysqldump類似
#-d 指定數據庫、-o 指定你把數據庫備份在哪裏,它會在/tmp/mongobak目錄下面生成一個mydb的目錄
他會生成對應集合的兩個文件.bson .json
2.備份全部庫
mongodump --host 127.0.0.1 --port 20000 -o /tmp/mongobak/alldatabase
#跟指定庫備份同樣,不用-d指定庫便可。目標目錄自動生成
3.指定備份集合
mongodump --host 127.0.0.1 --port 20000 -d mydb -c c1 -o /tmp/mongobak/
#-c指定集合
它依然會生成mydb目錄,再在這目錄下面生成兩個文件
4.導出集合爲json文件
mongoexport --host 127.0.0.1 --port 20000 -d mydb -c c1 -o /tmp/mydb2/1.json
MongoDB恢復
1.恢復全部庫
mongorestore -host 127.0.0.1 --port 20000 --drop dir/
#恢復的庫的時候,要指定到這個備份庫的所在目錄下
//其中dir是備份全部庫的目錄名字,其中--drop可選,意思是當恢復以前先把以前的數據刪除,不建議使用
2.恢復指定庫
mongorestore --host 127.0.0.1 --port 20000 -d testdb dir/
//-d跟要恢復的庫名字,dir就是該庫備份時所在的目錄
3.恢復集合
mongorestore -d mydb -c testc dir/mydb/testc.bson
// -c後面跟要恢復的集合名字,dir是備份mydb庫時生成文件所在路徑,這裏是一個bson文件的路徑,也就是說恢復集合的時候,只須要指定這個bson的文件
4.導入集合(若是恢復的是導出集合的json文件就使用mongoimport)
mongoimport -d mydb -c testc --file /tmp/testc.json
#--file指定這個json的文件
實例:
1.備份指定庫
[root@axinlinux-01 ~]# mkdir /tmp/mongobak
[root@axinlinux-01 ~]# mongodump --host 127.0.0.1 --port 20000 -d testdb -o /tmp/mongobak/
2018-11-21T15:04:49.290+0800 writing testdb.table1 to
2018-11-21T15:04:49.911+0800 [........................] testdb.table1 101/10000 (1.0%)
2018-11-21T15:04:50.039+0800 [########################] testdb.table1 10000/10000 (100.0%)
2018-11-21T15:04:50.039+0800 done dumping testdb.table1 (10000 documents)
[root@axinlinux-01 ~]# ls /tmp/mongobak/testdb/
table1.bson table1.metadata.json
2.備份全部庫
[root@axinlinux-01 ~]# mongodump --host 127.0.0.1 --port 20000 -o /tmp/mongobak/alldatabase
[root@axinlinux-01 ~]# ls /tmp/mongobak/alldatabase/
admin config db2 db3 testdb #全部的目錄對應全部的庫
3.備份指定集合
[root@axinlinux-01 ~]# mongodump --host 127.0.0.1 --port 20000 -d testdb -c table1 -o /tmp/mongobak3/
#-c 指定備份集合
2018-11-21T15:16:55.774+0800 writing testdb.table1 to
2018-11-21T15:16:57.262+0800 done dumping testdb.table1 (10000 documents)
[root@axinlinux-01 ~]# ls /tmp/mongobak3/testdb/ #這個目錄裏就是咱們備份集合的兩個文件
table1.bson table1.metadata.json
4.導出集合爲json文件
[root@axinlinux-01 ~]# mongoexport --host 127.0.0.1 --port 20000 -d testdb -c table1 -o /tmp/mongobak4/1.json #咱們處處以前插入在testdb的table集合。裏面是咱們插入的10000條數據
[root@axinlinux-01 ~]# vim !$
vim /tmp/mongobak4/1.json
{"_id":{"$oid":"5bf4f6db490e5e6d7b55b539"},"id":6.0,"test1":"testval1"}
{"_id":{"$oid":"5bf4f6db490e5e6d7b55b55e"},"id":43.0,"test1":"testval1"}
{"_id":{"$oid":"5bf4f6d8490e5e6d7b55b534"},"id":1.0,"test1":"testval1"}
。。。。。
恢復數據以前先把以前的數據刪掉,好達到實驗效果:
[root@axinlinux-01 ~]# mongo --port 20000 #先進入mongodb,要進mongos服務的那個20000端口
mongos> use testdb #進入testdb庫,直接刪掉
switched to db testdb
mongos> db.dropDatabase()
{ "dropped" : "testdb", "ok" : 1 }
mongos> show databases #查看一下,把db2 db3也一塊兒刪掉
admin 0.000GB
config 0.001GB
db2 0.000GB
db3 0.000GB
mongos> use db2
switched to db db2
mongos> db.dropDatabase()
{ "dropped" : "db2", "ok" : 1 }
mongos> use db3
switched to db db3
mongos> db.dropDatabase()
{ "dropped" : "db3", "ok" : 1 }
mongos> show dbs
admin 0.000GB
config 0.001GB
1.恢復全部庫
[root@axinlinux-01 tmp]# mongorestore -h 127.0.0.1 --port 20000 --drop /tmp/mongobak/alldatabase #選擇這個文件,就是恢復這個文件的全部
2018-11-21T15:41:38.681+0800 preparing collections to restore from
2018-11-21T15:41:38.687+0800 Failed: cannot do a full restore on a sharded system - remove the 'config' directory from the dump directory first
#報錯,是由於這個目錄裏有admin和config文件的備份,這兩個比較重要,可能不能恢復。咱們也沒有將他們刪除,因此先把這兩個備份文件刪掉
[root@axinlinux-01 tmp]# rm -rf /tmp/mongobak/alldatabase/
admin/ config/ db2/ db3/ testdb/
[root@axinlinux-01 tmp]# rm -rf /tmp/mongobak/alldatabase/admin/
[root@axinlinux-01 tmp]# rm -rf /tmp/mongobak/alldatabase/config/
[root@axinlinux-01 tmp]# mongorestore -h 127.0.0.1 --port 20000 --drop /tmp/mongobak/alldatabase #這樣就能夠了
[root@axinlinux-01 tmp]# mongo --port 20000 #咱們在進入到mongodb分片裏看一下是否已恢復
mongos> show dbs #查看一下就有了
admin 0.000GB
config 0.001GB
db2 0.000GB
db3 0.000GB
testdb 0.000GB
2.恢復指定庫
[root@axinlinux-01 tmp]# mongorestore --host 127.0.0.1 --port 20000 -d testdb /tmp/mongobak/alldatabase/testdb