replicattion set 就是多台服务器维护相同的数据副本,提高服务器的可用性.
Replication set 设置全过程
0:创建目录
mkdir -p /data/r0 /data/r1 /data/r2
1:启动3个实例,且声明实例属于某复制集
./bin/mongod --port 27017 --dbpath /data/r0 --smallfiles --replSet rsa --fork --logpath /var/log/mongo17.log
./bin/mongod --port 27018 --dbpath /data/r1 --smallfiles --replSet rsa --fork --logpath /var/log/mongo18.log
./bin/mongod --port 27019 --dbpath /data/r2 --smallfiles --replSet rsa --fork --logpath /var/log/mongo19.log
2:启动mongo.exe,随意连接一个服务器,启动配置
use admin
var rsconf = {
_id:‘rsa‘,
members:
[
{
_id:0,
host:‘127.0.0.1:27017‘
},
{
_id:1,
host:‘127.0.0.1:27018‘
},
{
_id:2,
host:‘127.0.0.1:27019‘
}
]
}
3: 根据配置做初始化
rs.initiate(rsconf);
4:查看状态
rs.status();
我们得到了三个节点"stateStr" : "SECONDARY", "stateStr" : "SECONDARY", "stateStr" : "PRIMARY",
5: 添加节点
rs.add(‘192.168.1.201:27018‘);
rs.add(‘192.168.1.201:27019‘);
6:删除节点
rs.remove(‘192.168.1.201:27019‘);
7:主节点插入数据
>use test
>db.user.insert({uid:1,name:‘lily‘});
8:连接secondary查询同步情况
./bin/mongo --port 27019
>use test
>show collections;
rsa:SECONDARY> show collections;
2015-11-10T21:56:14.075+0800 E QUERY Error: listDatabases failed:{ "note" : "
from execCommand", "ok" : 0, "errmsg" : "not master" }
at Error (<anonymous>)
at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
at shellHelper.show (src/mongo/shell/utils.js:630:33)
at shellHelper (src/mongo/shell/utils.js:524:36)
at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
8.1 出现上述错误,是因为slave默认不许读写
>rs.slaveOk();
>show collections;
看到与primary 一致的数据