副本集就是有自动故障恢复功能的主从集群。
主从集群和副本集最大的区别就是副本集没有固定的“主节点”;整个集群会选出一个“主节点”,当其挂掉后,又在剩下的从节点中选中其他节点为“主节点”,副本集总有一个活跃点(primary)和一个或多个备份节点(secondary)。
以三个节点为例:
1.创建目录/usr/local/mongodb/data
[wj@localhost data]$ sudo mkdir 17 18 19
2.启动3个实例,且声明实例属于某复制集 复制集名称随意
[wj@localhost bin]$ sudo ./mongod --port 27017 --dbpath /usr/local/mongodb/data/17 --smallfiles --replSet rs1 --fork --logpath /usr/local/mongodb/logs/mongo17.log
[wj@localhost bin]$ sudo ./mongod --port 27018 --dbpath /usr/local/mongodb/data/18 --smallfiles --replSet rs1 --fork --logpath /usr/local/mongodb/logs/mongo18.log
about to fork child process, waiting until server is ready for connections.
forked process: 13175
all output going to: /usr/local/mongodb/logs/mongo18.log
child process started successfully, parent exiting
[wj@localhost bin]$ sudo ./mongod --port 27019 --dbpath /usr/local/mongodb/data/19 --smallfiles --replSet rs1 --fork --logpath /usr/local/mongodb/logs/mongo19.log
3.找其中一台连上去:
[wj@localhost bin]$ ./mongo
> use admin 管理 切换到管理库
声明变量 配置选项 id为0的默认为primary
> var rsconf = {
... _id:'rs1',
... members:
... [
... {_id:0,
... host:'192.168.125.128:27017'
... },
... {_id:1,
... host:'192.168.125.128:27018'
... },
... {_id:2,
... host:'192.168.125.128:27019'
... }
... ]
... }
打印变量:
> printjson(rsconf)
4. 根据配置做初始化 可能得等几分钟
rs.initiate(rsconf);
> rs.initiate(rsconf);
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
> rs.status();会显示rs1:PRIMARY>
至此完成replicationSet搭建
rs1:PRIMARY> rs.remove("192.168.125.128:27019") 删除一个节点正常操作
Wed Dec 23 18:52:39.293 DBClientCursor::init call() failed
Wed Dec 23 18:52:39.295 JavaScript execution failed: Error: error doing query: failed at src/mongo/shell/query.js:L78
Wed Dec 23 18:52:39.295 trying reconnect to 127.0.0.1
删除后直接添加节点:报错
rs1:PRIMARY> rs.add("192.168.125.128:27018")
{
"errmsg" : "exception: need most members up to reconfigure, not ok : 192.168.125.128:27018",
"code" : 13144,
"ok" : 0
}
应该是重新声明下变量,rs.reconfig(rsconf)即可
查看大小
rs1:PRIMARY> show dbs
local 1.03076171875GB
主库添加集合和文档后
rs1:PRIMARY> db.stu.find()
{ "_id" : ObjectId("567a8167c7af1309769c2b65"), "name" : "haha" }
从库查看时报错:
rs1:SECONDARY> show collections
Wed Dec 23 19:17:39.731 JavaScript execution failed: error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:L128
出现上述错误,是因为slave默认不许读写
>rs.slaveOk();
>show tables
将主库服务器停机:从库服务器会自动有1个变为主库
rs1:PRIMARY> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
rs1:PRIMARY> use admin
switched to db admin
rs1:PRIMARY> db.shutdownServer()
不是主库不允许写操作
rs1:SECONDARY> db.stu.insert({name:"ldjfksdjf"})
not master
从客户端连接单个库选择Direct 连接多个时选择Replica Set时选择
节点1:
HOST:localhost:10001
Log File:D:\mongodb\logs\node1\logs.txt
Data File:D:\mongodb\dbs\node1
节点2:
HOST:localhost:10002
Log File:D:\mongodb\logs\node2\logs.txt
Data File:D:\mongodb\dbs\node2
节点3:
HOST:localhost:10003
Log File:D:\mongodb\logs\node3\logs.txt
Data File:D:\mongodb\dbs\node3
启动节点1:
mongod --dbpath D:\mongodb\dbs\node1 --logpath D:\mongodb\logs\node1\logs.txt --logappend --port 10001 --replSet it/localhost:10002 --master
启动节点2:
mongod --dbpath D:\mongodb\dbs\node2 --logpath D:\mongodb\logs\node2\logs.txt --logappend --port 10002 --replSet it/localhost:10001
启动节点3:
mongod --dbpath D:\mongodb\dbs\node3 --logpath D:\mongodb\logs\node3\logs.txt --logappend --port 10003 --replSet it/localhost:10001,localhost:10002
初始化节点(只能初始化一次):随便登录一个节点,以10001为例
mongo localhost:10001/admin
db.runCommand({
"replSetInitiate":{
"_id":“it",
"members":[
{
"_id":1,
"host":"localhost:10001",
"priority":3
},
{
"_id":2,
"host":"localhost:10002",
"priority":2
},
{
"_id":3,
"host":"localhost:10003",
"priority":1
}
]}});
参数解释:
--replSet 指定副本集 后面紧跟着副本集的名称
--logappend 日志文件末尾添加
--port 指定端口号
db.runCommand({}) 初始化副本集
初始化文档:
"_id":“it", 指副本集的名称
"members":[...] 副本集的服务器列表 每个列表有个
"_id": 每个服务器的唯一id,
"host" 指定服务器的主机,
"priority"设置优先级,默认优先级为1,可以是1-1000的数字
查询当前主库,登录10002
mongo localhost:10002
db.$cmd.findOne ( {ismaster: 1 } );