还是看官方文档吧!
新建目录# mkdir -p /data/rs0 /data/rs1 /data/rs2启动mongod replSet# ./bin/mongod --port 11100 --dbpath /data/rs0 --replSet fish --smallfiles# ./bin/mongod --port 11101 --dbpath /data/rs1 --replSet fish --smallfiles# ./bin/mongod --port 11102 --dbpath /data/rs2 --replSet fish --smallfilesmongod replSet 报错Fri Nov 30 16:07:27 [rsStart] warning: getaddrinfo("lenovo11") failed: Name or service not knownFri Nov 30 16:07:27 [rsStart] getaddrinfo("lenovo11") failed: Name or service not knownFri Nov 30 16:07:27 [rsStart] trying to contact lenovo11:11100Fri Nov 30 16:07:27 [rsStart] getaddrinfo("lenovo11") failed: Name or service not knownFri Nov 30 16:07:27 [rsStart] couldn't connect to lenovo11:11100: couldn't connect to server lenovo11:11100Fri Nov 30 16:07:27 [rsStart] getaddrinfo("lenovo11") failed: Name or service not knownFri Nov 30 16:07:27 [rsStart] replSet can't get local.system.replset config from self or any seed (yet)# /usr/local/mongodb/bin/mongo localhost:11100> config = { _id: 'fish' ,members: [{_id:0,host:"127.0.0.1:11100"},{_id:1,host:"127.0.0.1:11101"},{_id:2,host:"127.0.0.1:11102}]}> rs.initiate(config){ "startupStatus" : 4, "info" : "fish", "errmsg" : "all members and seeds must be reachable to initiate set", "ok" : 0}> rs.status(){ "startupStatus" : 4, "errmsg" : "can't currently get local.system.replset config from self or any seed (EMPTYUNREACHABLE)", "ok" : 0}> rs.conf()null> db.getReplicationInfo(){ "errmsg" : "neither master/slave nor replica set replication detected" }***************************************** 解决 **********************************************问了公司一个高手,虽然有点不屑这种小问题,不过还是指出问题所在。“这就是DNS的问题,它找不到“lenovo11”这个机器对应的ip。” ---> 原来是mongodb不能解析lenovo11.在/etc/hosts 添加一行:127.0.0.1 lenovo11[root@lenovo11 mongodb]# ./bin/mongo localhost:11100MongoDB shell version: 2.2.1connecting to: localhost:11100/test> config = { _id:'fish',members:[ {_id:0,host:'localhost:11100'}, {_id:1,host:'localhost:11101'}, {_id:2,host:'localhost:11102'}]}{ "_id" : "fish", "members" : [ { "_id" : 0, "host" : "localhost:11100" }, { "_id" : 1, "host" : "localhost:11101" }, { "_id" : 2, "host" : "localhost:11102" } ]}> rs.initiate(config){ "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1}> rs.status(){ "set" : "fish", "date" : ISODate("2012-11-30T09:41:51Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "localhost:11100", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 172, "optime" : Timestamp(1354268446000, 1), "optimeDate" : ISODate("2012-11-30T09:40:46Z"), "self" : true }, { "_id" : 1, "name" : "localhost:11101", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 55, "optime" : Timestamp(1354268446000, 1), "optimeDate" : ISODate("2012-11-30T09:40:46Z"), "lastHeartbeat" : ISODate("2012-11-30T09:41:50Z"), "pingMs" : 1, "errmsg" : "syncing to: localhost:11100" }, { "_id" : 2, "name" : "localhost:11102", "health" : 1, "state" : 3, "stateStr" : "RECOVERING", "uptime" : 55, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2012-11-30T09:41:49Z"), "pingMs" : 207 } ], "ok" : 1}fish:PRIMARY> rs.confrs.conf( rs.config(fish:PRIMARY> rs.conf(){ "_id" : "fish", "version" : 1, "members" : [ { "_id" : 0, "host" : "localhost:11100" }, { "_id" : 1, "host" : "localhost:11101" }, { "_id" : 2, "host" : "localhost:11102" } ]}fish:PRIMARY> rs.rs.add( rs.constructor rs.isMaster( rs.status(rs.addArb( rs.debug rs.propertyIsEnumerable( rs.stepDown(rs.apply( rs.freeze( rs.prototype rs.syncFrom(rs.call( rs.hasOwnProperty( rs.reconfig( rs.toLocaleString(rs.conf( rs.help( rs.remove( rs.toString(rs.config( rs.initiate( rs.slaveOk( rs.valueOf(fish:PRIMARY> rs.isMaster(){ "setName" : "fish", "ismaster" : true, "secondary" : false, "hosts" : [ "localhost:11100", "localhost:11102", "localhost:11101" ], "primary" : "localhost:11100", "me" : "localhost:11100", "maxBsonObjectSize" : 16777216, "localTime" : ISODate("2012-11-30T09:45:59.351Z"), "ok" : 1}