zhmg23

我们是如此的不同

安装配置mongodb副本集3.4在RHEL7.2


1、安装环境及配置yum

# more /etc/redhat-release 

Red Hat Enterprise Linux Server release 7.2 (Maipo)


# vi /etc/yum.repos.d/mongodb-org-3.4.repo 

[mongodb-org-3.4]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc


注:如果要省去gpg验证,可以修改 gpgcheck=0


2、安装MongoDB 3.4

# yum update

# yum -y install mongodb-org  


要安装特定版本的MongoDB,请分别指定每个组件包,并将版本号附加到包名称中,如以下示例所示:

sudo yum install -y mongodb-org-3.4.6 mongodb-org-server-3.4.6 mongodb-org-shell-3.4.6 mongodb-org-mongos-3.4.6 mongodb-org-tools-3.4.6



3、配置启动

# mongo

MongoDB shell version v3.4.6

connecting to: mongodb://127.0.0.1:27017

MongoDB server version: 3.4.6

Server has startup warnings: 

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] 

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] 

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] 

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] 

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2017-07-08T01:20:58.281+0800 I CONTROL  [initandlisten] 


上面的告警,可以用如下消除,建议加入到/etc/rc.local

echo "mongod     soft    nofiles   64000" >> /etc/security/limits.conf

echo "mongod     soft    nproc     64000" >> /etc/security/limits.conf

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

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



启动:sudo systemctl start mongod

重启:sudo systemctl restart mongod

停止:sudo systemctl stop mongod


设置开机启动

sudo systemctl enable mongod



4、配置副本集

mkdir -p  /data/mongodb/37017

touch /var/log/mongodb/mongod37017.log

chown mongodb: /data/mongodb/37017

chown mongodb: /var/log/mongodb/mongod37017.log

cp /etc/mongod.conf  /etc/mongod37017.conf 

vi  /etc/mongod37017.conf

processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod37017.pid

net:
port: 37017
bindIp: 0.0.0.0
ipv6: false
unixDomainSocket:
enabled: true
pathPrefix: /var/run/mongodb
filePermissions: 0700
http:
enabled: true
RESTInterfaceEnabled: false


storage:
dbPath: /data/mongodb/37017
indexBuildRetry: true
journal:
enabled: true
directoryPerDB: true
syncPeriodSecs: 60
engine: wiredTiger

wiredTiger:
engineConfig:
cacheSizeGB: 35
statisticsLogDelaySecs: 0
journalCompressor: snappy
directoryForIndexes: false
collectionConfig:
blockCompressor: snappy
indexConfig:
prefixCompression: true

operationProfiling:
slowOpThresholdMs: 100
mode: off

replication:
replSetName: mgofbj

systemLog:
path: /var/log/mongodb/mongod37017.log
destination: file
timeStampFormat: ctime


以上操作,在要10.1.73.6、10.1.73.7、10.1.73.8三台上同步操作。



在三台机器上任意一台机器登陆mongodb,我是在10.1.73.7进行登陆配置

$ mongo -port 37017

 

#使用admin数据库

> use admin


#然后配置副本集

> config={_id:"mgofbj",members:[{_id:0,host:"10.1.73.6:37017"},{_id:1,host:"10.1.73.7:37017"},{_id:2,host:"10.1.73.8:37017"}]}


回车后输出:

{

"_id" : "mgofbj",

"members" : [

{

"_id" : 0,

"host" : "10.1.73.6:37017"

},

{

"_id" : 1,

"host" : "10.1.73.7:37017"

},

{

"_id" : 2,

"host" : "10.1.73.8:37017"

}

]

}

>rs.initiate(config)    #初始化配置 

{ "ok" : 1 } 

 安装配置mongodb副本集3.4在RHEL7.2 - zhm - 合肥运维

 

> rs.status()       #查看状态     

{

"set" : "mgofbj",

"date" : ISODate("2017-07-10T17:17:12.051Z"),

"myState" : 1,

"term" : NumberLong(1),

"heartbeatIntervalMillis" : NumberLong(2000),

"optimes" : {

"lastCommittedOpTime" : {

"ts" : Timestamp(1499707030, 1),

"t" : NumberLong(1)

},

"appliedOpTime" : {

"ts" : Timestamp(1499707030, 1),

"t" : NumberLong(1)

},

"durableOpTime" : {

"ts" : Timestamp(1499707030, 1),

"t" : NumberLong(1)

}

},

"members" : [

{

"_id" : 0,

"name" : "10.1.73.6:37017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 153,

"optime" : {

"ts" : Timestamp(1499707030, 1),

"t" : NumberLong(1)

},

"optimeDurable" : {

"ts" : Timestamp(1499707030, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-07-10T17:17:10Z"),

"optimeDurableDate" : ISODate("2017-07-10T17:17:10Z"),

"lastHeartbeat" : ISODate("2017-07-10T17:17:11.651Z"),

"lastHeartbeatRecv" : ISODate("2017-07-10T17:17:11.934Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "10.1.73.8:37017",

"configVersion" : 1

},

{

"_id" : 1,

"name" : "10.1.73.7:37017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 240,

"optime" : {

"ts" : Timestamp(1499707030, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-07-10T17:17:10Z"),

"electionTime" : Timestamp(1499706889, 1),

"electionDate" : ISODate("2017-07-10T17:14:49Z"),

"configVersion" : 1,

"self" : true

},

{

"_id" : 2,

"name" : "10.1.73.8:37017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 153,

"optime" : {

"ts" : Timestamp(1499707030, 1),

"t" : NumberLong(1)

},

"optimeDurable" : {

"ts" : Timestamp(1499707030, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2017-07-10T17:17:10Z"),

"optimeDurableDate" : ISODate("2017-07-10T17:17:10Z"),

"lastHeartbeat" : ISODate("2017-07-10T17:17:11.649Z"),

"lastHeartbeatRecv" : ISODate("2017-07-10T17:17:11.909Z"),

"pingMs" : NumberLong(0),

"syncingTo" : "10.1.73.7:37017",

"configVersion" : 1

}

],

"ok" : 1

}


安装配置mongodb副本集3.4在RHEL7.2 - zhm - 合肥运维

 



注:如果运行,如下返回

> rs.initiate(config_rs1)

{

"ok" : 0,

"errmsg" : "'10.1.73.7:27017' has data already, cannot initiate set.",

"code" : 110,

"codeName" : "CannotInitializeNodeWithData"


则执行如下命令清空,我的数据没用,是因为在创建副本集前,写了测试数据

> db.dropDatabase()

{

"ok" : 0,

"errmsg" : "not master",

"code" : 10107,

"codeName" : "NotMaster"


但是还是建议重新安装一个全部的实例,来创建副本集。


 

5、测试副本集数据复制功能

在Primary(10.1.73.7:37017)上插入数据:


repset:PRIMARY> for(var i=0;i<10000;i++){db.test.insert({"name":"test"+i,"age":123})}

repset:PRIMARY> db.test.count() 

 安装配置mongodb副本集3.4在RHEL7.2 - zhm - 合肥运维

 

#在副本节点 10.1.73.6、10.1.73.8 上连接到mongodb的37017查看数据是否复制过来

mgofbj:SECONDARY> show dbs;

2017-07-11T01:54:57.274+0800 E QUERY    [thread1] Error: listDatabases failed:{

"ok" : 0,

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

"code" : 13435,

"codeName" : "NotMasterNoSlaveOk"

} :

_getErrorWithCode@src/mongo/shell/utils.js:25:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

shellHelper.show@src/mongo/shell/utils.js:769:19

shellHelper@src/mongo/shell/utils.js:659:15

@(shellhelp2):1:1

mgofbj:SECONDARY> 


#mongodb默认是从主节点读写数据的,副本节点上不允许读,需要设置副本节点可以读。

repset:SECONDARY> db.getMongo().setSlaveOk();


#可以看到数据已经复制到了副本集。

repset:SECONDARY> db.test.find();

{ "_id" : ObjectId("5963beb69de80e506c218ef8"), "name" : "test5", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218ef4"), "name" : "test1", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218efa"), "name" : "test7", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218ef6"), "name" : "test3", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218ef3"), "name" : "test0", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218ef5"), "name" : "test2", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218ef9"), "name" : "test6", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218ef7"), "name" : "test4", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218efb"), "name" : "test8", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218efc"), "name" : "test9", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218efd"), "name" : "test10", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218eff"), "name" : "test12", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218efe"), "name" : "test11", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218f00"), "name" : "test13", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218f01"), "name" : "test14", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218f02"), "name" : "test15", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218f06"), "name" : "test19", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218f03"), "name" : "test16", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218f04"), "name" : "test17", "age" : 123 }

{ "_id" : ObjectId("5963beb69de80e506c218f05"), "name" : "test18", "age" : 123 }

 安装配置mongodb副本集3.4在RHEL7.2 - zhm - 合肥运维

 

评论