• 早期版本使用master-slave,一主一從和MySQL相似,但slave在此架構中爲只讀,當主庫宕機後,從庫不能自動切換爲主linux
• 目前已經淘汰master-slave模式,改成副本集,這種模式下有一個主(primary),和多個從(secondary),只讀。支持給它們設置權重,當主宕掉後,權重最高的從切換爲主web
• 在此架構中還能夠創建一個仲裁(arbiter)的角色,它只負責裁決,而不存儲數據mongodb
• 再此架構中讀寫數據都是在主上,要想實現負載均衡的目的須要手動指定讀庫的目標server數據庫
• 三臺機器: 192.168.133.130(primary) 192.168.133.132(secondary) 192.168.133.133(secondary)json
• 編輯三臺機器的配置文件,更改或增長:vim
• replication://把此行前面的#刪除緩存
• ##oplog大小服務器
• oplogSizeMB: 20//前面有兩個空格架構
• ##複製集名稱app
• replSetName: aminglinux//前面有兩個空格
• 分別重啓三臺機器
• 鏈接主,在主上運行命令mongo
>use admin
>config={_id:"aminglinux",members:[{_id:0,host:"192.168.133.130:27017"},{_id:1,host:"192.168.133.132:27017"},{_id:2,host:"192.168.133.133:27017"}]} #配置副本集
>rs.initiate(config)
• rs.status() //查看狀態
• 若是兩個從上的狀態爲"stateStr" : "STARTUP",說明不成功, 則須要進行以下操做
• rs.add
•> var config={_id:"aminglinux",members:[{_id:0,host:"192.168.133.130:27017"},{_id:1,host:"192.168.133.132:27017"},{_id:2,host:"192.168.133.133:27017"}]}
id後面是副本集的名字
members:第一個是主,第二個是從,第三個...
• >rs.reconfig(config) #初始化,結果是1,說明初始化成功了
• rs.status() #查看狀態
• 此時再次查看rs.status()會發現從的狀態變爲SECONDARY,說明配置成功了
實例:
作實驗以前,把三臺機器的防火牆關閉
[root@localhost 02]# vim /etc/yum.repos.d/mongo.repo [mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc [root@localhost 02]# yum install -y mongodb-org [root@localhost 02]# vim /etc/mongod.conf #把裏面的ip換成本機的ip replication: #replication前面的#去掉 oplogSizeMB: 20/ replSetName: aminglinux/ [root@localhost 02]# systemctl start mongod [root@localhost 02]# ps aux | grep mongo #查看進程 [root@localhost 02]# netstat -lntp | grep mongo [root@localhost 03]# iptables -nvL #查看防火牆規則 [root@localhost 03]# yum install -y mongodb-org [root@localhost 03]# vim /etc/yum.repos.d/mongo.repo [mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc [root@localhost 03]# vim /etc/mongod.conf #把裏面的ip換成本機的ip replication: #replication前面的#去掉 oplogSizeMB: 20/ replSetName: aminglinux/ [root@localhost 03]# systemctl start mongod [root@localhost 03]# ps aux | grep mongo #查看進程 [root@localhost 03]# netstat -lntp | grep mongo [root@localhost 03]# iptables -nvL #查看防火牆規則 在192.168.133.130(primary)(01) [root@localhost 01]# vim /etc/mongod.conf replication: #replication前面的#去掉 oplogSizeMB: 20/ replSetName: aminglinux/ [root@localhost 01]# systemctl start mongod [root@localhost 01]# ps aux | grep mongo #查看進程 [root@localhost 01]# netstat -lntp | grep mongo [root@localhost 01]# vim /etc/mongod.conf [root@localhost 01]# systemctl restart mongod [root@localhost 01]# ps aux | grep mongo #查看進程 [root@localhost 01]# iptables -nvL [root@localhost 01]#
鏈接主,在主上運行命令mongo
[root@localhost 01]# mongo > config={_id:"aminglinux",members:[{_id:0,host:"192.168.133.130:27017"},{_id:1,host:"192.168.133.132:27017"},{_id:2,host:"192.168.133.133:27017"}]} > rs.reconfig(config) {"OK" : 1} 結果是1,說明初始化成功了 > rs.status() #查看狀態 > var config={_id:"aminglinux",members:[{_id:0,host:"192.168.133.130:27017"},{_id:1,host:"192.168.133.132:27017"},{_id:2,host:"192.168.133.133:27017"}]} > rs.status() #查看狀態 > > > > [root@localhost 01]#
• 主上建庫,建集合
• >use mydb
• >db.acc.insert({AccountID:1,UserName:"123",password:"123456"})
• >show dbs
• 從上查看
• >show dbs
• 若出現錯誤Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" },須要執行
• >rs.slaveok()
實例:
在01機器裏面操做 [root@localhost 01]# mongo aming:PRIMARY> use admin #切換到admin用戶 aming:PRIMARY> use mydb #建立庫 aming:PRIMARY >db.acc.insert({AccountID:1,UserName:"123",password:"123456"}) #建立集合 aming:PRIMARY> show dbs #查看建立的庫和集合 aming:PRIMARY> use mydb #切換到mydb庫 'aming:PRIMARY> show tabls #查看庫裏面的表 aming:PRIMARY> use mydb aming:PRIMARY> use mydb
在02機器裏面操做 [root@localhost 02]# mongo aminglinux:SECONDARY > show dbs Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } aminglinux:SECONDARY > rs.slaveok() aminglinux:SECONDARY > show dbs #查看全部的庫 admin db1 local mydb test aminglinux:SECONDARY > use mydb #切換到mydb庫裏面 switched to db mydb aminglinux:SECONDARY > show tables acc 在03機器裏面操做 [root@localhost 01]# mongo aminglinux:SECONDARY > show dbs # Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } aminglinux:SECONDARY > rs.slaveok() aminglinux:SECONDARY > show dbs aminglinux:SECONDARY > use mydb switched to db mydb aminglinux:SECONDARY > show tables acc
• 默認三臺機器權重都爲1,若是任何一個權重設置爲比其餘的高,則該臺機器立刻切換爲primary角色,因此咱們預設三臺機器的權重分別爲:130:3,132:2,133:1
• 在主上執行
• cfg = rs.conf()
• cfg.members[0].priority = 3
• cfg.members[1].priority = 2
• cfg.members[2].priority = 1
• rs.reconfig(cfg)
• 這樣的話,第二個節點將會成爲候選主節點。
• 主上執行 iptables -I INPUT -p tcp --dport 27017 -j DROP
實例:
在主上執行
[root@localhost 01]# aminglinux:SECONDARY > rs.conf() #查看權重 aminglinux:SECONDARY > quit aminglinux:SECONDARY > aminglinux:SECONDARY > aminglinux:SECONDARY > aminglinux:SECONDARY > 模擬主機宕機 [root@localhost 01]# iptables -I INPUT -p TCP --dport 27017 -j DROP [root@localhost 01]# iptables -D INPUT -p TCP --dport 27017 -j DROP [root@localhost 01]# mongo #查看權重 aminglinux:PRIMARY > #主機是PRIMARY 在02機器上面操做 [root@localhost 02]# mongo #登陸 aminglinux:SECONDARY > rs.status() #查看狀態 更改權重 aminglinux:PRIMARY > cfg.members[0].priority = 3 3 aminglinux:PRIMARY > cfg.members[1].priority = 2 2 aminglinux:PRIMARY > cfg.members[2].priority = 1 1 aminglinux:PRIMARY > rs.reconfig(cfg) #生效 aminglinux:PRIMARY > rs.reconfig(cfg) #查看是否生效 { 「ok」 : 1 } aminglinux:PRIMARY > rs.conf() #查看新的權重,權重越大,優先級越大 aminglinux:PRIMARY > aminglinux:PRIMARY > aminglinux:PRIMARY > aminglinux:PRIMARY > [root@localhost 02]#
• 分片就是將數據庫進行拆分,將大型集合分隔到不一樣服務器上。好比,原本100G的數據,能夠分割成10份存儲到10臺服務器上,這樣每臺機器只有10G的數據。
• 經過一個mongos的進程(路由)實現分片後的數據存儲與訪問,也就是說mongos是整個分片架構的核心,對客戶端而言是不知道是否有分片的,客戶端只須要把讀寫操做轉達給mongos便可。
• 雖然分片會把數據分隔到不少臺服務器上,可是每個節點都是須要有一個備用角色的,這樣能保證數據的高可用。
• 當系統須要更多空間或者資源的時候,分片可讓咱們按需方便擴展,只須要把mongodb服務的機器加入到分片集羣中便可
• mongos: 數據庫集羣請求的入口,全部的請求都經過mongos進行協調,不須要在應用程序添加一個路由選擇器,mongos本身就是一個請求分發中心,它負責把對應的數據請求請求轉發到對應的shard服務器上。在生產環境一般有多mongos做爲請求的入口,防止其中一個掛掉全部的mongodb請求都沒有辦法操做。
• config server: 配置服務器,存儲全部數據庫元信息(路由、分片)的配置。mongos自己沒有物理存儲分片服務器和數據路由信息,只是緩存在內存裏,配置服務器則實際存儲這些數據。mongos第一次啓動或者關掉重啓就會從 config server 加載配置信息,之後若是配置服務器信息變化會通知到全部的 mongos 更新本身的狀態,這樣 mongos 就能繼續準確路由。在生產環境一般有多個 config server 配置服務器,由於它存儲了分片路由的元數據,防止數據丟失!
• shard: 存儲了一個集合部分數據的MongoDB實例,每一個分片是單獨的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}
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
• mongodb3.4版本之後須要對config server建立副本集
• 添加配置文件(三臺機器都操做)
• mkdir /etc/mongod/
• 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 = 0.0.0.0
port = 21000
fork = true
configsvr = true #declare this is a config db of a cluster;
replSet=configs #副本集名稱
maxConns=20000 #設置最大鏈接數
• 啓動三臺機器的config server
• mongod -f /etc/mongod/config.conf //三臺機器都要操做
• 登陸任意一臺機器的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"}] }
• rs.initiate(config)
{ "ok" : 1 }
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 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
port = 27001
fork = true
httpinterface=true #打開web監控
rest=true
replSet=shard1 #副本集名稱
shardsvr = true #declare this is a shard db of a cluster;
maxConns=20000 #設置最大鏈接數
•
• 啓動shard1
• mongod -f /etc/mongod/shard1.conf //三臺機器都要操做
• 登陸130或者132任何一臺機器的27001端口初始化副本集,133之因此不行,是由於shard1咱們把133這臺機器的27001端口做爲了仲裁節點
• mongo --port 27001
• use admin
• config = { _id: "shard1", members: [ {_id : 0, host : "192.168.133.130:27001"}, {_id: 1,host : "192.168.133.132:27001"},{_id : 2, host : "192.168.133.133:27001",arbiterOnly:true}] }
• rs.initiate(config)
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
•
• 啓動shard2
• mongod -f /etc/mongod/shard2.conf //三臺機器都要操做
• 登陸132或者133任何一臺機器的27002端口初始化副本集,130之因此不行,是由於shard2咱們把130這臺機器的27002端口做爲了仲裁節點
• mongo --port 27002
• use admin
• config = { _id: "shard2", members: [ {_id : 0, host : "192.168.133.130:27002" ,arbiterOnly:true},{_id : 1, host : "192.168.133.132:27002"},{_id : 2, host : "192.168.133.133:27002"}] }
• rs.initiate(config)
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
• 啓動shard3
• mongod -f /etc/mongod/shard3.conf //三臺機器都要操做
• 登陸130或者133任何一臺機器的27003端口初始化副本集,132之因此不行,是由於shard3咱們把132這臺機器的27003端口做爲了仲裁節點
• mongo --port 27003
• use admin
• config = { _id: "shard3", members: [ {_id : 0, host : "192.168.133.130:27003"}, {_id : 1, host : "192.168.133.132:27003", arbiterOnly:true}, {_id : 2, host : "192.168.133.133:27003"}] }
• rs.initiate(config)
•
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
• 添加配置文件(三臺機器都操做)
• 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.133.130:21000, 192.168.133.132:21000, 192.168.133.133:21000 #監聽的配置服務器,只能有1個或者3個,configs爲配置服務器的副本集名字
maxConns=20000 #設置最大鏈接數
•啓動mongos服務,注意命令,前面都是mongod,這裏是mongos
• mongos -f /etc/mongod/mongos.conf
實例:
注意:三臺機器都要按照mongodb,
[root@localhost 02]# vim /etc/yum.repos.d/mongo.repo [mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc [root@localhost 03]# yum install -y mongodb-org [root@localhost 03]# vim /etc/yum.repos.d/mongo.repo [mongodb-org-4.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc [root@localhost 03]# yum install -y mongodb-org 在 [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
• 登陸任何一臺20000端口
• mongo --port 20000
• 把全部分片和路由器串聯
•sh.addShard("shard1/192.168.133.130:27001,192.168.133.132:27001,192.168.133.133:27001")
•sh.addShard("shard2/192.168.133.130:27002,192.168.133.132:27002,192.168.133.133:27002")
•sh.addShard("shard3/192.168.133.130:27003,192.168.133.132:27003,192.168.133.133:27003")
• 查看集羣狀態
• sh.status()
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
• 登陸任何一臺20000端口
• mongo --port 20000
• use admin
• db.runCommand({ enablesharding : "testdb"}) 或者
• sh.enableSharding("testdb") //指定要分片的數據庫
• db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } ) 或者
• sh.shardCollection("testdb.table1",{"id":1} ) //#指定數據庫裏須要分片的集合和片鍵
• use testdb
• for (var i = 1; i <= 10000; i++) db.table1.save({id:i,"test1":"testval1"})//插入測試數據
• db.table1.stats()//查看table1狀態
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
• 備份指定庫
• mongodump --host 127.0.0.1 --port 20000 -d mydb -o /tmp/mongobak
• 它會在/tmp/目錄下面生成一個mydb的目錄
• 備份全部庫
• mongodump --host 127.0.0.1 --port 20000 -o /tmp/mongobak/alldatabase
• 指定備份集合
• mongodump --host 127.0.0.1 --port 20000 -d mydb -c c1 -o /tmp/mongobak/
• 它依然會生成mydb目錄,再在這目錄下面生成兩個文件
• 導出集合爲json文件
• mongoexport --host 127.0.0.1 --port 20000 -d mydb -c c1 -o /tmp/mydb2/1.json
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
•恢復全部庫
• mongorestore -h 127.0.0.1 --port 20000 --drop dir/ //其中dir是備份全部庫的目錄名字,其中--drop可選,意思是當恢復以前先把以前的數據刪除,不建議使用
• 恢復指定庫
• mongorestore -d mydb dir/ //-d跟要恢復的庫名字,dir就是該庫備份時所在的目錄
• 恢復集合
•mongorestore -d mydb -c testc dir/mydb/testc.bson // -c後面跟要恢復的集合名字,dir是備份mydb庫時生成文件所在路徑,這裏是一個bson文件的路徑
• 導入集合
• mongoimport -d mydb -c testc --file /tmp/testc.json
實例:
[root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 0]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]# [root@localhost 01]#
常見問題:
高可用,就是說當一臺機器宕機,另一臺能夠立刻頂上去。 只有主從,是作不到的。
一、某個從節點應該是有問題。 檢查每一個節點是否都是正常的。 還有iptables啥的。
二、錯誤1、
作副本集,一直提示,可是個人防火牆確定都是關閉的,不知道是啥狀況
> rs.initiate(config)
{
"ok" : 0,
"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.216.130:27017 failed with not running with --replSet",
"code" : 74,
"codeName" : "NodeNotFound"
}
兩錯誤交替出現
> rs.initiate(config)
{
"ok" : 0,
"errmsg" : "'192.168.216.130:27017' has data already, cannot initiate set.",
"code" : 110,
"codeName" : "CannotInitializeNodeWithData"
答:把你192.168.216.130:27017這裏面的數據先刪掉,搞一個乾淨的庫。