mongodb副本叢集和分片叢集佈署

z597011036發表於2014-11-26

節點規劃

172.16.8.23    node1      PRIMARY                     172.16.8.178  node3     SECONDARY   

172.16.8.24    node2     SECONDARY                172.16.8.179  node4      ARBITER


一.mongodb副本集使用自帶功能進行主從切換(將node1,node2,node3節點配置成副本集)

(1).下載安裝,3臺節點是一樣(mongodb-4.0.8不支援主從複製架構)

[root@node1 ~]# wget

[root@node1 ~]# tar xvf mongodb-linux-x86_64-rhel70-4.0.8.tgz -C /opt/

[root@node1 opt]# mv mongodb-linux-x86_64-rhel70-4.0.8/ mongodb-4.0.8

[root@node1 opt]# cd mongodb-4.0.8/

[root@node1 mongodb-4.0.8]# vim mongo.conf

dbpath=/opt/mongodb-4.0.8/data    --指定資料目錄

logpath=/opt/mongodb-4.0.8/logs   --指定日誌目錄

logappend=true          --日誌追加方式寫入

directoryperdb=true     --每個資料庫生成一個目錄

journal=true       

port=27017        --埠

replSet=wuhan     --副本集名字

bind_ip=172.16.8.23    --繫結IP地址

fork=true              --後臺啟動

maxConns=1000          --最大連線數

[root@node1 mongodb-4.0.8]# ./bin/mongod -f mongo.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 23625

child process started successfully, parent exiting

[root@node1 mongodb-4.0.8]# netstat -antulp | grep mongo

tcp        0      0 172.16.8.23:27017       0.0.0.0:*               LISTEN      23625/./bin/mongod  

[root@node1 mongodb-4.0.8]#

(2).在任意一臺節點建立副本集

[root@node1 mongodb-4.0.8]# ./bin/mongo --host 172.16.8.23 --port 27017

MongoDB shell version v4.0.8

connecting to: mongodb://172.16.8.23:27017/?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("739167ac-f8aa-421e-88ca-8eb5035f3dc7") }

MongoDB server version: 4.0.8

Server has startup warnings: 

> rs.initiate( {_id : "wuhan",members: [{ _id: 0, host: "172.16.8.23:27017" },{ _id: 1, host: "172.16.8.24:27017" },{ _id: 2, host: 

"172.16.8.178:27017" }]})  --配置副本集

wuhan:PRIMARY> rs.conf();        --檢視配置資訊

wuhan:PRIMARY> rs.status();      --檢視狀態資訊    

wuhan:PRIMARY> use tong

switched to db tong

wuhan:PRIMARY> db.user.insert({"id":1});

WriteResult({ "nInserted" : 1 })

wuhan:PRIMARY> db.user.find()

{ "_id" : ObjectId("5ca1f4170fc6cba325bfeec0"), "id" : 1 }

wuhan:PRIMARY>

(3).slave節點檢視資料

wuhan:SECONDARY> use tong

switched to db tong

wuhan:SECONDARY> db.user.find()

{ "_id" : ObjectId("5ca1f4170fc6cba325bfeec0"), "id" : 1 }

wuhan:SECONDARY> 

(4).副本集master和slave節點切換(當master節點當機後,slave節點會選出一個當master節點)

[root@node1 mongodb-4.0.8]# pkill  mongod

[root@node1 mongodb-4.0.8]# netstat -antulp | grep mongo       --node1節點關閉mongodb服務

[root@node1 mongodb-4.0.8]# 

[root@node2 mongodb-4.0.8]# ./bin/mongo --host=172.16.8.24 --port 27017

MongoDB shell version v4.0.8

connecting to: mongodb://172.16.8.24:27017/?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("3a926e39-b5f5-44dc-850c-13b1618c9e74") }

MongoDB server version: 4.0.8

wuhan:SECONDARY> rs.status();

{
"_id" : 1,
"name" : "172.16.8.24:27017",        
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",       --node2變成PRIMARY狀態

wuhan:PRIMARY>


二.mongodb使用仲裁節點實現主從切換(將node1,node2,node3節點配置成副本集,node4是仲裁節點)

[root@node2 mongodb-4.0.8]# ./bin/mongo --host=172.16.8.24 --port 27017     --在主節點新增仲裁節點

MongoDB shell version v4.0.8

connecting to: mongodb://172.16.8.24:27017/?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("d1913a0d-ce00-45f3-88c2-edd1259d9520") }

MongoDB server version: 4.0.8

Server has startup warnings: 

wuhan:PRIMARY> rs.addArb("172.16.8.179:27017")           --新增仲裁節點


三.使用分片實現mongodb叢集(node1,node2,node3是副本集和分片節點,node4是路由)

mongos:查詢某資料時,先找configsvr詢問資料在哪個shard上.

configsvr:不儲存真正資料,儲存後設資料資訊,即某條資料在哪個分片上.

     mongodb從3.4版本開始只支援副本集叢集才可以使用分片模式,配置伺服器(configsvr)少於3個情況下使用叢集分片只能用於測試環境,不能用於生產.

(1).在node1,2,3節點修改配置檔案(三個node都是一樣的)

[root@node1 mongodb-4.0.8]# vim mongo.conf       --三個node節點都相同

dbpath=/opt/mongodb-4.0.8/data

logpath=/opt/mongodb-4.0.8/logs

logappend=true

directoryperdb=true

journal=true

port=27017

bind_ip=172.16.8.23

fork=true

maxConns=1000

replSet=wuhan

shardsvr=true

[root@node1 mongodb-4.0.8]# ./bin/mongod -f mongo.conf    --啟動服務

about to fork child process, waiting until server is ready for connections.

forked process: 32353

child process started successfully, parent exiting

[root@node1 mongodb-4.0.8]# 

(2).在node1,2,3節點配置configsvr檔案(三臺cofigsvr除IP不一樣都是一樣的)

[root@node1 mongodb-4.0.8]# vim configsvr.conf 

dbpath=/opt/mongodb-4.0.8/data1        --目錄需要重新建立

logpath=/opt/mongodb-4.0.8/logs1

logappend=true

directoryperdb=true

journal=true

port=27019

configsvr=true         --開啟configsve

replSet=cfgset        

bind_ip=172.16.8.23

fork=true

maxConns=1000

[root@node1 mongodb-4.0.8]# ./bin/mongod -f configsvr.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 9857

child process started successfully, parent exiting

[root@node1 mongodb-4.0.8]#

(3).在node4節點配置mongos檔案(路由節點)

[root@node4 mongodb-4.0.8]# vim mongos.conf 

logpath=/opt/mongodb-4.0.8/logs1

logappend=true

port=27030

configdb=cfgset/172.16.8.23:27019,172.16.8.24:27019,172.16.8.178:27019    -- cfgset 是副本集名字,IP地址和埠是configsvr的

bind_ip=172.16.8.179

fork=true

maxConns=1000

[root@node4 mongodb-4.0.8]# ./bin/mongos -f mongos.conf &    --需要後臺啟動

[1] 792

[root@node4 mongodb-4.0.8]# 2019-04-04T11:31:17.220+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should 

only be done for testing purposes and is not recommended for production.

about to fork child process, waiting until server is ready for connections.

forked process: 794

child process started successfully, parent exiting

[root@node4 mongodb-4.0.8]# 

(4).在mongos中定義分片

[root@node4 mongodb-4.0.8]# ./bin/mongo --host 172.16.8.179 --port 27030   --進入mongos介面

MongoDB shell version v4.0.8

connecting to: mongodb://172.16.8.179:27030/?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("d74da65a-9d81-48b1-9071-acd4c7d3d31f") }

MongoDB server version: 4.0.8

Server has startup warnings: 

mongos> sh.addShard("wuhan/172.16.8.23:27017,172.16.8.24:27017,172.16.8.178:27017");

{
"shardAdded" : "wuhan",
"ok" : 1,
"operationTime" : Timestamp(1554352779, 11),
"$clusterTime" : {
"clusterTime" : Timestamp(1554352779, 11),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

mongos> 

(5).建立資料庫使用分片資料

mongos> use preson

switched to db preson

mongos> show tables;

mongos> sh.enableSharding("preson");

{
"ok" : 1,
"operationTime" : Timestamp(1554353102, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1554353102, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

mongos> sh.shardCollection("preson.teacher",{name:"hashed"});

{
"collectionsharded" : "preson.teacher",
"collectionUUID" : UUID("6383cf50-20aa-4643-86bd-32f17cfbf0f1"),
"ok" : 1,
"operationTime" : Timestamp(1554353124, 15),
"$clusterTime" : {
"clusterTime" : Timestamp(1554353124, 15),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

mongos> for (var i=0;i<100;i++) db.teacher.insert({name:"zs"+i,age:i});      --生成資料

WriteResult({ "nInserted" : 1 })

mongos> show tables;

teacher

mongos> db.teacher.find()

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e68"), "name" : "zs0", "age" : 0 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e69"), "name" : "zs1", "age" : 1 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6a"), "name" : "zs2", "age" : 2 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6b"), "name" : "zs3", "age" : 3 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6c"), "name" : "zs4", "age" : 4 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6d"), "name" : "zs5", "age" : 5 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6e"), "name" : "zs6", "age" : 6 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6f"), "name" : "zs7", "age" : 7 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e70"), "name" : "zs8", "age" : 8 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e71"), "name" : "zs9", "age" : 9 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e72"), "name" : "zs10", "age" : 10 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e73"), "name" : "zs11", "age" : 11 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e74"), "name" : "zs12", "age" : 12 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e75"), "name" : "zs13", "age" : 13 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e76"), "name" : "zs14", "age" : 14 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e77"), "name" : "zs15", "age" : 15 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e78"), "name" : "zs16", "age" : 16 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e79"), "name" : "zs17", "age" : 17 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e7a"), "name" : "zs18", "age" : 18 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e7b"), "name" : "zs19", "age" : 19 }

Type "it" for more

mongos> 

(6).在mongodb副制集檢視資料(node1節點)

wuhan:PRIMARY> show tables;

teacher

wuhan:PRIMARY> db.teacher.find();

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e68"), "name" : "zs0", "age" : 0 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e69"), "name" : "zs1", "age" : 1 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6a"), "name" : "zs2", "age" : 2 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6e"), "name" : "zs6", "age" : 6 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6f"), "name" : "zs7", "age" : 7 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e70"), "name" : "zs8", "age" : 8 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e72"), "name" : "zs10", "age" : 10 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e73"), "name" : "zs11", "age" : 11 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e75"), "name" : "zs13", "age" : 13 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e78"), "name" : "zs16", "age" : 16 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e7a"), "name" : "zs18", "age" : 18 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e7b"), "name" : "zs19", "age" : 19 }

Type "it" for more

wuhan:PRIMARY>

(7).在mongodb副制集檢視資料(node2節點)

wuhan:SECONDARY> db.teacher.find();

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6b"), "name" : "zs3", "age" : 3 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6c"), "name" : "zs4", "age" : 4 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e6d"), "name" : "zs5", "age" : 5 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e71"), "name" : "zs9", "age" : 9 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e74"), "name" : "zs12", "age" : 12 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e76"), "name" : "zs14", "age" : 14 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e77"), "name" : "zs15", "age" : 15 }

{ "_id" : ObjectId("5ca58bfd0fd7364c87ce9e79"), "name" : "zs17", "age" : 17 }

Type "it" for more

wuhan:SECONDARY>



錯誤處理:

[root@node1 mongodb-4.0.8]# ./bin/mongod -f mongo.conf 

2019-04-01T18:19:39.842+0800 F CONTROL  [main] Master/slave replication is no longer supported

[root@node1 mongodb-4.0.8]#

解決方法:(mongodb-4.0.8不支援主從架構)

[root@node1 mongodb-4.0.8]# vim mongo.conf

master=true       --去掉這行

[root@node1 mongodb-4.0.8]# 


slave節點命令出錯:

wuhan:SECONDARY> show dbs

2019-04-01T19:22:12.459+0800 E QUERY    [js] Error: listDatabases failed:{

"operationTime" : Timestamp(1554117728, 1),

"ok" : 0,

"errmsg" : "not master and slaveOk=false",

"code" : 13435,

"codeName" : "NotMasterNoSlaveOk",

解決方法:

wuhan:SECONDARY> rs.slaveOk()

wuhan:SECONDARY> show dbs

admin   0.000GB

config  0.000GB

local   0.000GB

tong    0.000GB

wuhan:SECONDARY> 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25854343/viewspace-1347163/,如需轉載,請註明出處,否則將追究法律責任。

相關文章