MongoDB 副本集搭建

Posted Hank.Yoon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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" }

 

以上是关于MongoDB 副本集搭建的主要内容,如果未能解决你的问题,请参考以下文章

搭建MongoDB副本集&分片

搭建mongodb集群(副本集+分片)

搭建MongoDB的副本集

MongoDB副本集replica set --副本集环境搭建

mongodb----副本集搭建及故障自动切换

MongoDB 搭建副本集