当我停止副本集的节点并再次启动它们时,主节点进入“恢复”状态。
我创建了一个未经授权运行的副本集。为了使用授权,我添加了用户“db.createUser(...)”,并在配置文件中启用了授权:
security:
authorization: "enabled"
在停止副本集之前(甚至在不添加安全参数的情况下重新启动集群),rs.status()显示:
{
"set" : "REPLICASET",
"date" : ISODate("2016-09-08T09:57:50.335Z"),
"myState" : 1,
"term" : NumberLong(7),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.1.167:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 301,
"optime" : {
"ts" : Timestamp(1473328390, 2),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2016-09-08T09:53:10Z"),
"electionTime" : Timestamp(1473328390, 1),
"electionDate" : ISODate("2016-09-08T09:53:10Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.168:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 295,
"optime" : {
"ts" : Timestamp(1473328390, 2),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2016-09-08T09:53:10Z"),
"lastHeartbeat" : ISODate("2016-09-08T09:57:48.679Z"),
"lastHeartbeatRecv" : ISODate("2016-09-08T09:57:49.676Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.1.167:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.1.169:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 295,
"optime" : {
"ts" : Timestamp(1473328390, 2),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2016-09-08T09:53:10Z"),
"lastHeartbeat" : ISODate("2016-09-08T09:57:48.680Z"),
"lastHeartbeatRecv" : ISODate("2016-09-08T09:57:49.054Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.1.168:27017",
"configVersion" : 1
}
],
"ok" : 1
}
为了开始使用此配置,我按如下方式停止了每个节点:
[root@n--- etc]# mongo --port 27017 --eval 'db.adminCommand("shutdown")'
MongoDB shell version: 3.2.9
connecting to: 127.0.0.1:27017/test
2016-09-02T14:26:15.784+0200 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-09-02T14:26:15.785+0200 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:231:14
关闭后,我通过检查ps -ax | grep mongo
的输出确认该进程不存在。
但是当我再次启动节点并使用我的凭据登录时,rs.status()现在指示:
{
"set" : "REPLICASET",
"date" : ISODate("2016-09-08T13:19:12.963Z"),
"myState" : 3,
"term" : NumberLong(7),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.1.167:27017",
"health" : 1,
"state" : 3,
"stateStr" : "RECOVERING",
"uptime" : 42,
"optime" : {
"ts" : Timestamp(1473340490, 6),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2016-09-08T13:14:50Z"),
"infoMessage" : "could not find member to sync from",
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.168:27017",
"health" : 0,
"state" : 6,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2016-09-08T13:19:10.553Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"authenticated" : false,
"configVersion" : -1
},
{
"_id" : 2,
"name" : "192.168.1.169:27017",
"health" : 0,
"state" : 6,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2016-09-08T13:19:10.552Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"authenticated" : false,
"configVersion" : -1
}
],
"ok" : 1
}
为什么呢?也许停机并不是阻止mongod的好方法;但我也测试过使用'kill pid',但重启最终会处于相同的状态。
在这种状态下我不知道如何修复集群;我又重新开始了(删除dbpath文件并重新配置副本集);我试过'--repair'但没有工作。
关于我的系统的信息:
mongod --conf /etc/mongod.conf
一台机器上mongod.conf的“net.bindIp”配置示例:
net:
port: 27017
bindIp: 127.0.0.1,192.168.1.167
答案 0 :(得分:1)
注意:此解决方案是特定于Windows的,但可以轻松移植到基于* nix的系统。
您需要按顺序采取措施。首先,启动你的mongod实例。
$(document).ready
将mongo连接到每个节点并创建管理员用户。我更喜欢创建超级用户。
start "29001" mongod --dbpath "C:\data\db\r1" --port 29001
start "29002" mongod --dbpath "C:\data\db\r2" --port 29002
start "29003" mongod --dbpath "C:\data\db\r3" --port 29003
您可以根据需要创建其他用户。
创建密钥文件。有关有效的密钥文件内容,请参阅文档。
注意:在基于* nix的系统上,将密钥文件的chmod设置为400
就我而言,我创建了密钥文件
> use admin
> db.createUser({user: "root", pwd: "123456", roles:["root"]})
现在重启MongoDB服务器并启用echo mysecret==key > C:\data\key\key.txt
和--keyFile
标志。
--replSet
一旦所有start "29001" mongod --dbpath "C:\data\db\r1" --port 29001 --replSet "rs1" --keyFile C:\data\key\key.txt
start "29002" mongod --dbpath "C:\data\db\r2" --port 29002 --replSet "rs1" --keyFile C:\data\key\key.txt
start "29003" mongod --dbpath "C:\data\db\r3" --port 29003 --replSet "rs1" --keyFile C:\data\key\key.txt
实例启动并运行,请将任何一个实例与身份验证连接。
mongod
启动replicaset,
mongo --port 29001 -u "root" -p "123456" --authenticationDatabase "admin"
注意:您可能需要将
> use admin > rs.initiate() > rs1:PRIMARY> rs.add("localhost:29002") { "ok" : 1 } > rs1:PRIMARY> rs.add("localhost:29003") { "ok" : 1 }
替换为机器名称或IP地址。
答案 1 :(得分:0)
节点应该一次关闭一个节点,因此其他的第二个成员将选择主节点。并且它将在恢复节点中同步到另一个成员。这种逐个关闭不需要重新添加节点。
答案 2 :(得分:0)
最后我解决了这个问题,因为群集副本集是MANDATORY一个keyFile来传递所有节点,当我指示keyFile时它返回错误,因为在mongod.log中指示:
def graphWriterIRIandRut():
# Set up the plots
plt.subplot(2, 1, 1)
plt.grid(True)
plt.ylabel('IRI value', fontsize=12)
plt.title('Right IRI data per mile for 2016 calibrations:')
plt.tick_params(axis='both', which='major', labelsize=8)
plt.hold(True)
plt.subplot(2, 1, 2)
plt.grid(True)
plt.ylabel('IRI value', fontsize=12)
plt.title('Left IRI data per mile for 2016 calibrations:')
plt.tick_params(axis='both', which='major', labelsize=8)
plt.hold(True)
# Iterate over the files in the current directory
for filename in os.listdir(os.getcwd()):
# Initialize a new set of lists for each file
startList = []
endList = []
iriRList = []
iriLList = []
# Load the file
with open(filename, 'r') as file:
for row in csv.DictReader(file):
startList.append(float(row['Start-Mi']))
endList.append(float(row[' End-Mi']))
iriRList.append(float(row[' IRI R e']))
iriLList.append(float(row[' IRI LWP']))
# Add new data to the plots
plt.subplot(2, 1, 1)
plt.plot(startList, iriRList)
plt.subplot(2, 1, 2)
plt.plot(startList, iriLList)
plt.show()
plt.close('all')
keyfile必须具有400个权限。谢谢@Saleem
当人们说“你可以添加密钥文件”时,我认为它是一个可选的参数,但它是强制性的。