mongo副本集的两种简单搭建方法

由于最近服务迁移,需要重新搭建mongodb的副本集,正好整理下文档,方便以后快速使用及维护更新。

1 基础环境说明

root@upsmiddelvm01:~# uname -a
Linux upsmiddelvm01 4.4.0-165-generic #193-Ubuntu SMP Tue Sep 17 17:42:52 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
root@upsmiddelvm01:~# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.6 LTS"
root@upsmiddelvm01:~# cat /etc/rc.local 

echo never > /sys/kernel/mm/transparent_hugepage/defrag
echo never  > /sys/kernel/mm/transparent_hugepage/enabled

cat /etc/security/limits.conf 
*     hard   nofile  65535
*     soft   nofile  65535

2.使用apt搭建mongodb副本

2.1 配置apt源

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

sudo apt-get update 

2.2 安装mongodb指定版本

sudo apt-get install -y mongodb-org=3.4.3 mongodb-org-server=3.4.3 mongodb-org-shell=3.4.3 mongodb-org-mongos=3.4.3 mongodb-org-tools=3.4.3

2.3 修改配置文件

sudo vim /etc/mongod.conf 
storage:
  dbPath: /data/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

replication:
  replSetName: upmongo
  oplogSizeMB: 10240
  secondaryIndexPrefetch: all
  enableMajorityReadConcern: false

名词解释
replSetName: upmongo                   #副本集名称
  oplogSizeMB: 10240                    #MongoDB副本集是通过oplogSizeMB日志来存储写操作的
  secondaryIndexPrefetch: all           #指定副本集成员在接受oplog之前是否加载索引到内存.默认会加载所有的索引到内存.none不加载,all加载所有,_id_only仅加载_id
  enableMajorityReadConcern: false      #MongoDB会起一个单独的snapshot线程,会周期性的对当前的数据集进行snapshot,并记录snapshot时最新oplog的时间戳,得到一个映射表

2.4 启动mongodb配置副本集

sudo service  mongod  restart
mongo
use  admin
config = { _id:"upmongo", members:[ {_id:0,host:"mongo01:27017"},{_id:1,host:"mongo02:27017"}, {_id:2,host:"mongo03:27017"} ] }
#初始化副本集配置
 rs.initiate(config);
#查看集群节点的状态
 rs.status();

2.5 验证副本集功能



mongo 127.0.0.1 #建立test 数据库。 use test; 往testdb表插入数据。 > db.testdb.insert({"test1":"testval1"}) #在副本节点 上连接到mongodb查看数据是否复制过来。 mongo #使用test 数据库。 repset:SECONDARY> use test; repset:SECONDARY> show tables; #mongodb默认是从主节点读写数据的,副本节点上不允许读,需要设置副本节点可以读。 repset:SECONDARY> db.getMongo().setSlaveOk(); #可以看到数据已经复制到了副本集。 repset:SECONDARY> db.testdb.find(); #输出 { "_id" : ObjectId("52c028460c7505626a93944f"), "test1" : "testval1" }

3 通过tar包方式搭建副本集

3.1 下载指定版本tar包

下载指定版本tar包
https://www.mongodb.com/download-center/community

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-3.4.24.tgz

3.2解压tgz包及创建目录

tar zxvf mongodb-linux-x86_64-ubuntu1604-3.4.24.tgz
mv mongodb-linux-x86_64-ubuntu1604-3.4.24 mongodb
mv mongodb /opt/
mkdir /data/dev/vdb1/mongodb/{data,log} -p
mkdir /opt/mongodb/conf

3.3 配置mongo配置文件

root@upsmiddelvm01:~# cat /opt/mongodb/conf/mongodb.conf
storage:
  dbPath: /data/dev/vdb1/mongdb/data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/dev/vdb1/mongodb/log/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

replication:
  replSetName: upmongo
  oplogSizeMB: 10240
  secondaryIndexPrefetch: all
  enableMajorityReadConcern: false

3.4 启动mongodb

cd /opt/mongodb/
./bin/mongod -f conf/mongodb.conf & 
cp bin/mongo* /usr/local/sbin/

3.5 配置副本集

use admin
config = { _id:"upsmartmongo", members:[ {_id:0,host:"10.0.162.11:27017"},{_id:1,host:"10.0.162.12:27017"}, {_id:2,host:"10.0.162.13:27017"} ] }
rs.initiate(config);

rs.status();

{
    "set" : "upmongo",
    "date" : ISODate("2020-04-09T07:51:18.615Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1586418672, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1586418672, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1586418672, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "10.0.162.11:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 4011,
            "optime" : {
                "ts" : Timestamp(1586418672, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2020-04-09T07:51:12Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1586415060, 1),
            "electionDate" : ISODate("2020-04-09T06:51:00Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "10.0.162.12:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 3628,
            "optime" : {
                "ts" : Timestamp(1586418672, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1586418672, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2020-04-09T07:51:12Z"),
            "optimeDurableDate" : ISODate("2020-04-09T07:51:12Z"),
            "lastHeartbeat" : ISODate("2020-04-09T07:51:17.874Z"),
            "lastHeartbeatRecv" : ISODate("2020-04-09T07:51:18.242Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "10.0.162.11:27017",
            "syncSourceHost" : "10.0.162.11:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "10.0.162.13:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 3628,
            "optime" : {
                "ts" : Timestamp(1586418672, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1586418672, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2020-04-09T07:51:12Z"),
            "optimeDurableDate" : ISODate("2020-04-09T07:51:12Z"),
            "lastHeartbeat" : ISODate("2020-04-09T07:51:17.874Z"),
            "lastHeartbeatRecv" : ISODate("2020-04-09T07:51:18.243Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "10.0.162.12:27017",
            "syncSourceHost" : "10.0.162.12:27017",
            "syncSourceId" : 1,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: