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:配置
rsconf = {
_id:'rsa',
members:
[
{_id:0,
host:'xxxx:27018'
},
{_id:1,
host:'xxxx:27019'
},
{_id:2,
host:'xxxx:27017'
},
]
}
3: 根据配置做初始化
rs.initiate(rsconf);
这一步如果出现 errMsg:"Quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: 192.168.2.153:27017; the following nodes did not respond affirmatively: 192.168.2.153:27019 failed with Error connecting to 192.168.2.153:27019 :: caused by :: 由于目标计算机积极拒绝,无法连接。"
因此可能会对较新版本的MongoDB进行更改。特别是,默认情况下,MongoDB 3.6及更新版本将绑定到localhost,因此如果要允许远程连接进行复制,则需要将--bind_ip
参数传递给mongod
实例或net.bindIp
在其配置文件中进行设置,rsconf 配置中不能配置IP 写成localhost即可,除非是要外网访问则写成IP
4: 添加节点
rs.add('xxxxx:27018');
rs.add('xxxxx:27019');
5:查看状态
rs.status();
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 tables
rsa:SECONDARY> show tables;
Sat Aug 17 16:03:55.786 JavaScript execution failed: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
8.1 出现上述错误,是因为slave默认不许读写
>rs.slaveOk();
>show tables