MongoDB replica setup (one master and two spare)
Master: 192.168.0.237
Prepared 1:192.168.0.122
Prepared 2:192.168.0.123
Three machines are also operated
Tar zxf mongodb-linux-i686-1.8.1.tgz
MV Mongodb-linux-i686-1.8.1/usr/local/mongodb
cd/usr/local/mongodb/
mkdir etc
mkdir logs
CD logs/
Touch Mongodb.log
Cd..
CD etc/
VI mongodb.cnf
Join
dbpath=/data/cc
mkdir/data/cc
/usr/local/mongodb/bin/mongod--dbpath=/data/cc--logpath=/usr/local/mongodb/logs/mongodb.log-f/usr/local/ MONGODB/ETC/MONGODB.CNF--replset Repset--fork
Install all and start successfully
Any machine on three machines landed on MongoDB
/usr/local/mongodb/bin/mongo
#使用admin数据库
Use admin
#定义副本集配置变量, here's _id: "Repset" and the above command parameter "–replset Repset" to remain the same.
Config = {_id: "Repset", members:[
... {_id:0,host: "192.168.0.122:27017"},
... {_id:1,host: "192.168.0.123:27017"},
... {_id:2,host: "192.168.0.237:27017"}]
... }
#输出
{
"_id": "Repset",
"Members": [
{
"_id": 0,
"Host": "192.168.0.122:27017"
},
{
"_id": 1,
"Host": "192.168.0.123:27017"
},
{
"_id": 2,
"Host": "192.168.0.237:27017"
}
]
}
#初始化副本集配置
Rs.initiate (config);
#输出成功
1
{
"Info": "Config now saved locally. Should come online in about a minute. ",
"OK": 1
}
#查看集群节点的状态
Rs.status ();
#输出
{
"Set": "Repset",
"Date": Isodate ("2014-05-05t10:51:50z"),
"MyState": 2,
"Members": [
{
"_id": 0,
"Name": "192.168.0.122:27017",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Optime": {
"T": 1399286403000,
"I": 1
},
"Optimedate": Isodate ("2014-05-05t10:40:03z"),
"Self": true
},
{
"_id": 1,
"Name": "192.168.0.123:27017",
"Health": 1,
"State": 2,
"Statestr": "Secondary",
"Uptime": 920,
"Optime": {
"T": 1399286403000,
"I": 1
},
"Optimedate": Isodate ("2014-05-05t10:40:03z"),
"Lastheartbeat": Isodate ("2014-05-05t10:51:48z")
},
{
"_id": 2,
"Name": "192.168.0.237:27017",
"Health": 1,
"State": 1,
"Statestr": "PRIMARY",
"Uptime": 920,
"Optime": {
"T": 1399286403000,
"I": 1
},
"Optimedate": Isodate ("2014-05-05t10:40:03z"),
"Lastheartbeat": Isodate ("2014-05-05t10:51:48z")
}
],
"OK": 1
}
The entire replica set has been built successfully.
6. Test copy Set data copy function
#在主节点192.168.0.237 connected to the terminal:
MONGO 127.0.0.1
#建立test database.
Use test;
Insert data toward the TestDB table.
> Db.testdb.insert ({"Test1": "Testval1"})
#在副本节点 192.168.0.122, 192.168.0.123 Connect to MongoDB to see if the data is copied over.
/usr/local/mongodb/bin/mongo 192.168.0.122:27017
#使用test database.
repset:secondary> use test;
Repset:secondary> Show tables;
#输出
Sun Dec 21:50:48.590 Error: {"$err": "Not Master and Slaveok=false", "Code": 13435} at src/mongo/shell/query.js:128
This is normal, because secondary is not allowed to read and write, if not to resolve, the method is as follows:
Repset:secondary> Rs.slaveok ();
Repset:secondary> Db.getmongo (). Setslaveok ();
You can see that the data has been copied to the replica set.
Repset:secondary> Db.testdb.find ();
#输出
{"_id": ObjectId ("52c028460c7505626a93944f"), "test1": "Testval1"}
This method fails by manually specifying the MongoDB IP
This article is from the "Lonely" blog, make sure to keep this source http://304076020.blog.51cto.com/7503470/1408248