简介:
1.Mongodb复制集由一组Mongod实例(进程)组成,包含一个Primary节点和多个Secondary节点,Mongodb Driver(客户端)的所有数据都写入Primary,Secondary从Primary同步写入的数据,以保持复制集内所有成员存储相同的数据集,提供数据的高可用。
2、客户端在主节点写入数据,在从节点读取数据,主节点和从节点进行数据交互保证数据一致性,如果其中一个节点出了故障,其他发节点马上将业务接过来无需停机操作。
优势
让数据更安全;
搞数据可用性;
灾难恢复;
无停机维护(如备份,重建索引,故障转移);
读缩放(额外的副本读取);
副本集对应用程序是透明的。
特点
N个几点的群集;
任何节点可作为主节点;
所有写入操作都在主节点上;
自动故障转移;
自动恢复。
(具体方法参考上一篇 mongodb基础操作篇一.)
复制集
重启方法
[root@pc-2 mongodb]# mongod -f /etc/mongod.conf --shutdown 停止
[root@pc-2 mongodb]# mongod -f /etc/mongod.conf 启动
mongod -f /etc/mongod2.conf --shutdown
mongod -f /etc/mongod2.conf
mongod -f /etc/mongod3.conf --shutdown
mongod -f /etc/mongod3.conf
mongod -f /etc/mongod4.conf --shutdown
mongod -f /etc/mongod4.conf
mongo ##登录017的数据库
> cfg={
"_id":"sha","members":[{
"_id":0,"host":"192.168.100.20:27017"},{
"_id":1,"host":"192.168.100.20:27018"},{
"_id":2,"host":"192.168.100.20:27019"}]}
{
"_id" : "sha",
"members" : [
{
"_id" : 0,
"host" : "192.168.100.20:27017"
},
{
"_id" : 1,
"host" : "192.168.100.20:27018"
},
{
"_id" : 2,
"host" : "192.168.100.20:27019"
}
]
}
查看复制集状态
> db.stats()
{
"db" : "test",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"fsUsedSize" : 0,
sha:SECONDARY> rs.status()
{
"set" : "sha",
"date" : ISODate("2020-09-12T04:07:23.821Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
}
},
"lastStableCheckpointTimestamp" : Timestamp(1599883618, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.100.20:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", //主
"uptime" : 688,
"optime" : {
"ts" : Timestamp(1599883638, 1),
"t" : NumberLong(1)
[root@pc-2 mongodb]# mongo --port 27017
sha:PRIMARY>
sha:PRIMARY> rs.add("192.168.100.20:27016")
{
"ok" : 1,
"operationTime" : Timestamp(1599883825, 1),
"_id" : 3,
"name" : "192.168.100.20:27016",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 31,
"optime" : {
sha:PRIMARY>
sha:PRIMARY> rs.remove("192.168.100.20:27016")
{
"ok" : 1,
"operationTime" : Timestamp(1599883915, 1),
"$clusterTime" : {
sha:PRIMARY>
[root@pc-2 mongodb]# ps aux | grep mongod
root 9266 0.9 5.0 1534688 94236 ? Sl 11:55 0:10 mongod -f /etc/mongod.conf
root 9402 1.0 5.0 1475704 93688 ? Sl 12:02 0:06 mongod -f /etc/mongod2.conf
root 9439 1.0 5.1 1515560 95084 ? Sl 12:03 0:06 mongod -f /etc/mongod3.conf
root 9476 0.9 4.8 1403876 91104 ? Sl 12:03 0:06 mongod -f /etc/mongod4.conf
root 9889 0.0 0.0 112724 988 pts/0 S+ 12:13 0:00 grep --color=auto mongod
[root@pc-2 mongodb]# kill -9 9266
[root@pc-2 mongodb]# ps aux | grep mongod
root 9402 1.0 5.0 1467508 94060 ? Sl 12:02 0:07 mongod -f /etc/mongod2.conf
root 9439 1.0 5.1 1499168 95324 ? Sl 12:03 0:07 mongod -f /etc/mongod3.conf
root 9476 0.9 4.9 1395680 91300 ? Sl 12:03 0:06 mongod -f /etc/mongod4.conf
root 9891 0.0 0.0 112724 988 pts/0 S+ 12:14 0:00 grep --color=auto mongod
[root@pc-2 mongodb]# mongo --port 27018
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 4.0.0
sha:SECONDARY> exit
bye
[root@pc-2 mongodb]# mongo --port 27019
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27019/
sha:PRIMARY> rs.status()
{
"set" : "sha",
"date" : ISODate("2020-09-12T04:16:03.135Z"),
"myState" : 1,
"_id" : 2,
"name" : "192.168.100.20:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 779,
"optime" : {
"ts" : Timestamp(1599884162, 1),
"t" : NumberLong(2)
sha:PRIMARY>
sha:PRIMARY> rs.freeze(30)
{
"operationTime" : Timestamp(1599884252, 1),
"ok" : 0,
"errmsg" : "cannot freeze node when primary or running for election. state: Primary",
"code" : 95,
"codeName" : "NotSecondary",
"$clusterTime" : {
"clusterTime" : Timestamp(1599884252, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
交出主节点位置,维持从节点状态不少于60s,等待30s使主节点和从节点日志同步(交换主从)
sha:PRIMARY> rs.stepDown(60,30)
2020-09-12T12:19:30.134+0800 E QUERY [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27019' :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1398:12
@(shell):1:1
2020-09-12T12:19:30.138+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27019 failed
2020-09-12T12:19:30.139+0800 I NETWORK [js] reconnect 127.0.0.1:27019 ok
sha:SECONDARY>
6.发现 27018 变为了主
sha:SECONDARY> rs.status()
{
"_id" : 1,
"name" : "192.168.100.20:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 817,
"optime" : {
"ts" : Timestamp(1599884419, 1),
"t" : NumberLong(3)
sha:SECONDARY>
1.标准节点 2.仲裁节点 3.被动节点
通过优先级设点,优先级高的是标准节点,低的是被动节点;最后是仲裁节点
重新创建4个实例
mongo
> cfg={
"_id":"shars","members":[{
"_id":0,"host":"192.168.100.20:27017","priority":100},{
"_id":1,"host":"192.168.100.20:27018","priority":100},{
"_id":2,"host":"192.168.100.20:27019","priority":0},{
"_id":3,"host":"192.168.100.20:27020","arbiterOnly":true}]}
{
"_id" : "shars",
"members" : [
{
"_id" : 0,
"host" : "192.168.100.20:27017",
"priority" : 100
},
{
"_id" : 1,
"host" : "192.168.100.20:27018",
"priority" : 100
},
{
"_id" : 2,
"host" : "192.168.100.20:27019",
"priority" : 0
},
{
"_id" : 3,
"host" : "192.168.100.20:27020",
"arbiterOnly" : true
}
]
}
> rs.initiate(cfg) ##将(cfg)进行初始化,会发现27017变为主键
{
"ok" : 1,
"operationTime" : Timestamp(1599903569, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1599903569, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
shars:SECONDARY> rs.isMaster()
{
"hosts" : [
"192.168.100.20:27017", //标准节点
"192.168.100.20:27018"
],
"passives" : [
"192.168.100.20:27019" //被动节点
],
"arbiters" : [
"192.168.100.20:27020" //仲裁节点
],
"setName" : "shars",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "192.168.100.20:27017",
"me" : "192.168.100.20:27017",
"electionId" : ObjectId("7fffffff0000000000000001"),
"
shars:PRIMARY>
shars:PRIMARY> use test ##使用test库
switched to db test
shars:PRIMARY> db.t1.insert({
"id":1,"name":"jack"}) #创建id为1的“jack”用户
WriteResult({
"nInserted" : 1 })
shars:PRIMARY> db.t1.insert({
"id":2,"name":"tom"}) #创建id为2的“tom”用户
WriteResult({
"nInserted" : 1 })
shars:PRIMARY> db.t1.find()
{
"_id" : ObjectId("5f5c987603a23cf20b5905e0"), "id" : 1, "name" : "jack" }
{
"_id" : ObjectId("5f5c988203a23cf20b5905e1"), "id" : 2, "name" : "tom" }
修改数据
shars:PRIMARY> db.t1.update({
"id":1},{
$set:{
"name":"jerrt"}})
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
shars:PRIMARY> db.t1.find()
{
"_id" : ObjectId("5f5c987603a23cf20b5905e0"), "id" : 1, "name" : "jerrt" }
{
"_id" : ObjectId("5f5c988203a23cf20b5905e1"), "id" : 2, "name" : "tom" }
删除数据
shars:PRIMARY> db.t1.remove({
"id":2})
WriteResult({
"nRemoved" : 1 })
shars:PRIMARY> db.t1.find()
{
"_id" : ObjectId("5f5c987603a23cf20b5905e0"), "id" : 1, "name" : "jerrt" }
shars:PRIMARY>
查看日志文件
shars:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB //##local库存放的日志
test 0.000GB
shars:PRIMARY> use local
switched to db local
shars:PRIMARY> show collections ##查看集合
oplog.rs ##存放了日志信息
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
shars:PRIMARY> db.oplog.rs.find() ##这个命令可以查看日志记录
{
"ts" : Timestamp(1599903569, 1), "h" : NumberLong("2054745077148917962"),
[root@pc-2 mongodb]# mongod -f /etc/mongod.conf --shutdown ##将主节点宕机
2020-09-12T17:48:03.039+0800 I CONTROL [main] Automatically disabling TLS 1.0, toforce-enable TLS 1.0 specify --sslDisabledProtocols 'none'
killing process with pid: 18382
[root@pc-2 mongodb]# mongo --port 27018 ##用主键中的27018去登录
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 4.0.0
Server has startup warnings:
shars:PRIMARY> exit 发现27018变为主键
bye
这时将27018也宕机,查看27019会不会参与选举
[root@pc-2 mongodb]# mongod -f /etc/mongod2.conf --shutdown
##发现27019不参与选举
shars:SECONDARY> exit
bye
[root@pc-2 mongodb]# mongod -f /etc/mongod.conf
2020-09-12T17:48:58.822+0800 I CONTROL [main] Automatically disabling TLS 1.0, toforce-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 20094
child process started successfully, parent exiting
[root@pc-2 mongodb]# mongo
shars:PRIMARY> exit
bye
[root@pc-2 mongodb]# mongod -f /etc/mongod2.conf
2020-09-12T17:50:11.229+0800 I CONTROL [main] Automatically disabling TLS 1.0, toforce-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 20209
child process started successfully, parent exiting
[root@pc-2 mongodb]# mongo --port 27018 ##登录到27018节点上
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 4.0.0
Server has startup warnings:
shars:SECONDARY> show dbs ##发现查看不到数据库信息
2020-09-12T17:51:17.180+0800 E QUERY [js] Error: listDatabases failed:{
"operationTime" : Timestamp(1599904272, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1599904272, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:865:19
shellHelper@src/mongo/shell/utils.js:755:15
@(shellhelp2):1:1
shars:SECONDARY> rs.slaveOk() ##允许从节点去读取信息
shars:SECONDARY> show dbs 查询到了结果数据库信息
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
shars:SECONDARY> rs.printReplicationInfo() ##查看日志文件大小
configured oplog size: 4329.4130859375MB ##日志文件大小为4329MB
log length start to end: 803secs (0.22hrs)
oplog first event time: Sat Sep 12 2020 17:39:29 GMT+0800 (CST)
oplog last event time: Sat Sep 12 2020 17:52:52 GMT+0800 (CST)
now: Sat Sep 12 2020 17:52:59 GMT+0800 (CST)
shars:SECONDARY> rs.printSlaveReplicationInfo() ##查看哪些节点可以找主节点同步
source: 192.168.100.20:27018
syncedTo: Sat Sep 12 2020 17:53:12 GMT+0800 (CST)
-10 secs (0 hrs) behind the primary
source: 192.168.100.20:27019
syncedTo: Sat Sep 12 2020 17:53:02 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
shars:SECONDARY>
仲裁节点不会去找主节点同步信息
shars:SECONDARY> use admin
switched to db admin
shars:SECONDARY> db.shutdownServer()
server should be down...
2020-09-12T17:55:03.820+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27018 failed
2020-09-12T17:55:03.820+0800 I NETWORK [js] reconnect 127.0.0.1:27018 failed failed
2020-09-12T17:55:03.823+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27018 failed
2020-09-12T17:55:03.823+0800 I NETWORK [js] reconnect 127.0.0.1:27018 failed failed
> exit
bye
shars:PRIMARY> rs.help() 查看命令帮助信息
[root@localhost logs]# vim /etc/mongod2.conf
net:
port: 27028 ##将端口号改为27028
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#replication: ##将参数注销
# replSetName: shars
[root@pc-2 mongodb]# vim /etc/mongod2.conf
[root@pc-2 mongodb]# mongod -f /etc/mongod2.conf
2020-09-12T17:58:27.287+0800 I CONTROL [main] Automatically disabling TLS 1.0, toforce-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 20417
child process started successfully, parent exiting
[root@pc-2 mongodb]# mongodump --port 27028 --db local --collection 'oplog.rs' ##备份日志信息
2020-09-12T17:58:47.952+0800 writing local.oplog.rs to
2020-09-12T17:58:47.954+0800 done dumping local.oplog.rs (94 documents)
[root@pc-2 mongodb]# mongo --port 27028 重新登录
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27028/
MongoDB server version: 4.0.0
Server has startup warnings:
> use local
switched to db local ##使用local库
> show tables
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
> db.oplog.rs.drop() ##将日志库删除
true
> db.run
local.run
> db.runCommand( {
create: "oplog.rs", capped: true, size: (2 * 1024 * 1024) } ) ##重建日志信息库
{
"ok" : 1 }
> use admin ##改完之后将服务停止
switched to db admin
> db.shutdownServer()
server should be down...
2020-09-12T18:00:45.909+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27028 failed
2020-09-12T18:00:45.909+0800 I NETWORK [js] reconnect 127.0.0.1:27028 failed failed
[root@pc-2 mongodb]# vim /etc/mongod2.conf
# network interfaces
net:
port: 27018
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
replication:
replSetName: shars
oplogSizeMB: 2048 ##添加日志库大小为2048M
[
root@pc-2 mongodb]# mongod -f /etc/mongod2.conf --shutdown
[root@pc-2 mongodb]# mongod -f /etc/mongod2.conf
```go
[root@pc-2 mongodb]# mongo --port 27018
MongoDB shell version v4.0.0
shars:SECONDARY> rs.printReplicationInfo() ##查看文件大小
configured oplog size: 2MB ///修改成功 2M
log length start to end: 20secs (0.01hrs)
oplog first event time: Sat Sep 12 2020 18:04:52 GMT+0800 (CST)
oplog last event time: Sat Sep 12 2020 18:05:12 GMT+0800 (CST)
now: Sat Sep 12 2020 18:05:12 GMT+0800 (CST)
shars:SECONDARY>
##登录到主节点上
[root@pc-2 mongodb]# mongo
MongoDB shell version v4.0.0
##将主位让出
shars:PRIMARY> rs.stepDown()
2020-09-12T18:06:27.019+0800 E QUERY [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27017' :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1398:12
@(shell):1:1
2020-09-12T18:06:27.021+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2020-09-12T18:06:27.022+0800 I NETWORK [js] reconnect 127.0.0.1:27017 ok
shars:SECONDARY> exit
bye
##再次登陆的27018节点上
[root@pc-2 mongodb]# mongo --port 27018
MongoDB shell version v4.0.0
shars:PRIMARY> ##27018变为主节点
shars:PRIMARY> use admin
switched to db admin
shars:PRIMARY> db.createUser({
"user":"root","pwd":"123","roles":["root"]})
Successfully added user: {
"user" : "root", "roles" : [ "root" ] }
shars:PRIMARY> exit
bye
[root@pc-2 mongodb]# vim /etc/mongod.conf
[root@pc-2 mongodb]# vim /etc/mongod2.conf
[root@pc-2 mongodb]# vim /etc/mongod3.conf
[root@pc-2 mongodb]# vim /etc/mongod4.conf
security:
keyFile: /usr/bin/sharskey1 ##验证路径
clusterAuthMode: keyFile ##密钥文件验证
security:
keyFile: /usr/bin/sharskey2
clusterAuthMode: keyFile
security:
keyFile: /usr/bin/sharskey3
clusterAuthMode: keyFile
security:
keyFile: /usr/bin/sharskey4
clusterAuthMode: keyFile
[root@localhost logs]# cd /usr/bin/
[root@localhost bin]# echo "shars key" > sharskey1
[root@localhost bin]# echo "shars key" >sharskey2
[root@localhost bin]# echo "shars key" > sharskey3
[root@localhost bin]# echo "shars key" > sharskey4
[root@localhost bin]# chmod 600 sha* ##将文件权限改为600
[root@pc-2 bin]# mongo --port 27018
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 4.0.0
shars:SECONDARY> exit
bye
[root@pc-2 bin]# mongo --port 27017
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 4.0.0
shars:PRIMARY> show dbs // 登录到主位节点,验证认证测试
2020-09-12T18:29:51.682+0800 E QUERY [js] Error: listDatabases failed:{
"operationTime" : Timestamp(1599906589, 1),
"ok" : 0,
"errmsg" : "command listDatabases requires authentication",
"code" : 13,
"codeName" : "Unauthorized",
"$clusterTime" : {
"clusterTime" : Timestamp(1599906589, 1),
"signature" : {
"hash" : BinData(0,"AStJmqe4VFYS0OmsYUypbImu66o="),
"keyId" : NumberLong("6871533557148286977")
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:865:19
shellHelper@src/mongo/shell/utils.js:755:15
@(shellhelp2):1:1
shars:PRIMARY> use admin
switched to db admin
shars:PRIMARY> db.auth("root","123") ##用root身份去登录
1
shars:PRIMARY> show dbs ##可以查看数据库信息了
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
shars:PRIMARY>
从位的节点都需要登录root用户才可以查看数据库信息