MongoDB 副本集搭建
搭建mongodb副本集
[root@ mongodb]# cd /u02
[root@ u02]# mkdir -p mongodb/data_2777
[root@ u02]# mkdir -p mongodb/data_3777
[root@ u02]# mkdir -p mongodb/data_4777
[root@ mongodb]# tar mongodb-linux-x86_64-3.2.6.tgz
[root@ mongodb]# mv mongodb-linux-x86_64-3.2.6 mongodb
[root@ mongodb]# mkdir -p 2777 3777 4777
[root@ 2777]# mkdir {conf,log,keys,pid}
[root@ 3777]# mkdir {conf,log,keys,pid}
[root@ 4777]# mkdir {conf,log,keys,pid}
[root@ conf]# vi mongodb_2777.conf
systemLog:
destination: file
logAppend: true
path: /u02/mongodb/mongodb/2777/log/shard1.log
storage:
dbPath: /u02/mongodb/data_2777
journal:
enabled: true
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 10 # the maximum of half of physical RAM or 1 gigabyte
directoryForIndexes: true # mongod stores indexes and collections in separate subdirectories under the data (i.e. storage.dbPath) directory
collectionConfig:
blockCompressor: snappy # The default type of compression to use to compress collection data
processManagement:
fork: true # fork and run in background
pidFilePath: /u02/mongodb/mongodb/2777/pid/mongodb_2777.pid # location of pidfile
net:
bindIp: 0.0.0.0
port: 2777
operationProfiling:
slowOpThresholdMs: 500
mode: slowOp
replication:
replSetName: shard1 #副本集的名稱,副本集以此名稱來識別是否屬於同一個叢集中
oplogSizeMB: 512
#auditLog:
# destination: file
# format: JSON
# path: /u02/mongodb/mongodb/2777/log/audit_log/audit.json
#
啟動mongodb
/u02/mongodb/mongodb/bin/mongod -f /u02/mongodb/mongodb/2777/conf/mongodb_2777.conf
/u02/mongodb/mongodb/bin/mongod -f /u02/mongodb/mongodb/3777/conf/mongodb_3777.conf
/u02/mongodb/mongodb/bin/mongod -f /u02/mongodb/mongodb/4777/conf/mongodb_4777.conf
登入2777埠mongodb
[root@ conf]# /u02/mongodb/mongodb/bin/mongo --port 2777
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:2777/test
Server has startup warnings:
2019-10-08T19:47:35.621+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-10-08T19:47:35.621+0800 I CONTROL [initandlisten]
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten]
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten] ** WARNING: You are running on a NUMA machine.
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten] ** We suggest launching mongod like this to avoid performance problems:
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten] ** numactl --interleave=all mongod [other options]
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten]
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten]
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-10-08T19:47:35.622+0800 I CONTROL [initandlisten]
shard1:PRIMARY>
shard1:PRIMARY>conf = {_id:"shard1",members:[{_id:0,host:"127.0.0.1:2777"}]};
shard1:PRIMARY>rs.initiate(conf) ;
會顯示出你的當前資料庫服務例項已經加到副本集中,並且是master
shard1:PRIMARY> db.isMaster()
{
"hosts" : [
"127.0.0.1:2777",
"127.0.0.1:3777",
"127.0.0.1:4777"
],
"setName" : "shard1",
"setVersion" : 3,
"ismaster" : true,
"secondary" : false,
"primary" : "127.0.0.1:2777",
"me" : "127.0.0.1:2777",
"electionId" : ObjectId("7fffffff0000000000000001"),
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2019-10-08T12:12:35.869Z"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1
}
接著新增剛剛那2個服務例項到副本集中
shard1:PRIMARY>rs.add("127.0.0.1:3777");
shard1:PRIMARY>rs.add("127.0.0.1:4777");
檢視副本集:
shard1:PRIMARY> rs.status()
{
"set" : "shard1",
"date" : ISODate("2019-10-08T12:14:04.786Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:2777",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1589,
"optime" : {
"ts" : Timestamp(1570536146, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-10-08T12:02:26Z"),
"electionTime" : Timestamp(1570536062, 2),
"electionDate" : ISODate("2019-10-08T12:01:02Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "127.0.0.1:3777",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 706,
"optime" : {
"ts" : Timestamp(1570536146, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-10-08T12:02:26Z"),
"lastHeartbeat" : ISODate("2019-10-08T12:14:04.654Z"),
"lastHeartbeatRecv" : ISODate("2019-10-08T12:14:03.533Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "127.0.0.1:2777",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "127.0.0.1:4777",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 698,
"optime" : {
"ts" : Timestamp(1570536146, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-10-08T12:02:26Z"),
"lastHeartbeat" : ISODate("2019-10-08T12:14:04.654Z"),
"lastHeartbeatRecv" : ISODate("2019-10-08T12:14:03.414Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1
}
rs.status()
"health" : 1, #代表機器正常
"stateStr" : "PRIMARY", #代表是主節點,可讀寫,其中有以下幾下狀態
1. STARTUP:剛加入到複製集中,配置還未載入
2. STARTUP2:配置已載入完,初始化;
3. RECOVERING:正在恢復,不適用讀
4. ARBITER: 仲裁者
5. DOWN:節點不可到達
6. UNKNOWN:未獲取其他節點狀態而不知是什麼狀態,一般發生在只有兩個成員的架構
7. REMOVED:移除複製集
8. ROLLBACK:資料回滾,在回滾結束時,轉移到RECOVERING或SECONDARY狀態
9. FATAL:出錯。檢視日誌grep “replSet FATAL”找出錯原因,重新做同步
10. PRIMARY:主節點
11. SECONDARY:備份節點
建立資料庫:
shard1:PRIMARY> use yoon
檢視當前資料庫:
shard1:PRIMARY> db
yoon
插入資料:
shard1:PRIMARY> db.movie.insert({"name":"hank"});
WriteResult({ "nInserted" : 1 })
檢視當前庫下的表:
shard1:PRIMARY> show collections
movie
檢視錶中資料:
shard1:PRIMARY> db.movie.find()
{ "_id" : ObjectId("5d9c7ee9d7a8df8fd023d6ed"), "name" : "hank" }
登入3777埠檢視錶movie:
[root@ conf]# /u02/mongodb/mongodb/bin/mongo --port 3777
注意:新建的slave是不能讀和寫的,當在從伺服器上讀時會出現errmsg:not master and slaveOk=flase code:13435 錯誤,需要執行:rs.slaveOk()來開啟讀功能
shard1:SECONDARY> rs.slaveOk()
shard1:SECONDARY> show dbs
local 0.000GB
yoon 0.000GB
shard1:SECONDARY> use yoon
switched to db yoon
shard1:SECONDARY> show collections
movie
shard1:SECONDARY> db.movie.find()
{ "_id" : ObjectId("5d9c7ee9d7a8df8fd023d6ed"), "name" : "hank" }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-2659159/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mongodb 4.0副本集搭建MongoDB
- MongoDB 6.0.3副本集搭建MongoDB
- MongoDB日常運維-04副本集搭建MongoDB運維
- Mongodb3.0.5副本集搭建及spring和java連線副本集配置MongoDBSpringJava
- 搭建MongoDB副本集MongoDB
- MongoDB日常運維-05副本集故障切換MongoDB運維
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- 使用副本集搭建MongoDB叢集MongoDB
- MongoDB 4.2副本集新增/刪除副本(一主一副一仲裁)MongoDB
- 手把手教你搭建mongodb副本集MongoDB
- MongoDB 副本集的原理、搭建、應用MongoDB
- MongoDB 4.2副本集自動故障轉移(一主一副一仲裁)MongoDB
- 第六章 MongoDB副本集搭建MongoDB
- 搭建高可用MongoDB叢集(二): 副本集MongoDB
- MongoDB副本集MongoDB
- linux下Mongodb叢集搭建:分片+副本集LinuxMongoDB
- 【MongoDB】分片(sharding)+副本集(replSet)叢集搭建MongoDB
- 修改mongodb3.0副本集使用者密碼遇到的坑MongoDB密碼
- MongoDB部署副本集MongoDB
- MongoDB 副本集管理MongoDB
- MongoDB之副本集MongoDB
- 再看MongoDB副本集MongoDB
- 2.MongoDB 4.2副本集環境基於時間點的恢復MongoDB
- MongoDB日常運維-06副本集搭建錯誤彙總MongoDB運維
- 單機Linux下搭建MongoDB副本集-三節點LinuxMongoDB
- 使用Docker搭建MongoDB 5.0版本副本集叢集DockerMongoDB
- MongoDB - 副本集簡介MongoDB
- MongoDB副本集實踐MongoDB
- 如何配置 MongoDB 副本集MongoDB
- MongoDB 副本集原理及管理MongoDB
- MongoDB 副本集切換方法MongoDB
- 006.MongoDB副本集MongoDB
- 搭建高可用MongoDB叢集(三):深入副本集內部機制MongoDB
- mongdb副本集搭建
- mongo副本集搭建Go
- 小丸子學MongoDB系列之——部署MongoDB副本集MongoDB
- MongoDB Replica Set 副本集實踐MongoDB
- mongodb簡單副本集實驗MongoDB