将独立的 MongoDB 实例转换为单节点副本集

Posted

技术标签:

【中文标题】将独立的 MongoDB 实例转换为单节点副本集【英文标题】:Converting a standalone MongoDB instance to a single-node replica set 【发布时间】:2021-03-08 07:02:55 【问题描述】:

我正在尝试将我的独立 MongoDB 实例转换为单节点副本集,以便实时迁移到 Atlas。 我遵循了这个程序:https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/

我采取的步骤是:

$sudo service mongodb stop
$sudo service mongod start
$mongo
 >rs.initiate()

    "info2" : "no configuration explicitly specified -- making one",
    "me" : "staging3.domain.io:27017",
    "info" : "Config now saved locally.  Should come online in about a minute.",
    "ok" : 1

 singleNodeRepl:PRIMARY> rs.status()

    "set" : "singleNodeRepl",
    "date" : ISODate("2020-11-26T00:46:25Z"),
    "myState" : 1,
    "members" : [
        
            "_id" : 0,
            "name" : "staging4.domain.io:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1197,
            "optime" : Timestamp(1606350415, 1),
            "optimeDate" : ISODate("2020-11-26T00:26:55Z"),
            "electionTime" : Timestamp(1606350415, 2),
            "electionDate" : ISODate("2020-11-26T00:26:55Z"),
            "self" : true
        
    ],
    "ok" : 1

singleNodeRepl:PRIMARY> db.oplog.rs.find()
 "ts" : Timestamp(1606350415, 1), "h" : NumberLong(0), "v" : 2, "op" : "n", "ns" : "", "o" :  "msg" : "initiating set"  

在这一点上,它似乎没有问题。 但是,我的应用程序无法像以前那样工作。

非常感谢您在解决问题方面提供的任何帮助。 谢谢。

编辑:

按照建议,我将replSet 包含在配置文件中,而不是将其作为参数传递。 这是我的配置文件:

# mongod.conf

#where to log
logpath=/var/log/mongodb/mongod.log

logappend=true

# fork and run in background
fork=true

#port=27017

dbpath=/var/lib/mongo

# location of pidfile
pidfilepath=/var/run/mongodb/mongod.pid

# Listen to local interface only. Comment out to listen on all interfaces.
#bind_ip=127.0.0.1

# Disables write-ahead journaling
# nojournal=true

# Enables periodic logging of CPU utilization and I/O wait
#cpu=true

# Turn on/off security.  Off is currently the default
#noauth=true
#auth=true

# Verbose logging output.
verbose=true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck=true

# Enable db quota management
#quota=true

# Set oplogging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog=0

# Ignore query hints
#nohints=true

# Enable the HTTP interface (Defaults to port 28017).
#httpinterface=true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting=true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan=true

# Disable data file preallocation.
#noprealloc=true

# Specify .ns file size for new databases.
# nssize=<size>

# Replication Options

# in replicated mongo databases, specify the replica set name here
replSet=singleNodeRepl
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile

以及详细的日志文件: 看起来一切正常。但是,我的应用程序无法像以前那样连接到数据库。

2020-11-26T00:26:55.852+0000 [conn1] replSet replSetInitiate admin command received from client
2020-11-26T00:26:55.853+0000 [conn1] replSet info initiate : no configuration specified.  Using a default configuration for the set
2020-11-26T00:26:55.853+0000 [conn1] replSet created this configuration for initiation :  _id: "singleNodeRepl", members: [  _id: 0, host: "staging4.domain.io:27017"  ] 
2020-11-26T00:26:55.853+0000 [conn1] replSet replSetInitiate config object parses ok, 1 members specified
2020-11-26T00:26:55.853+0000 [conn1] getMyAddrs(): [127.0.0.1] [10.20.26.228] [::1] [fe80::8ed:65ff:fe9e:15ab%eth0]
2020-11-26T00:26:55.853+0000 [conn1] getallIPs("staging4.domain.io"): [127.0.0.1]
2020-11-26T00:26:55.853+0000 [conn1] replSet replSetInitiate all members seem up
2020-11-26T00:26:55.853+0000 [conn1] ******
2020-11-26T00:26:55.853+0000 [conn1] creating replication oplog of size: 2570MB...
2020-11-26T00:26:55.853+0000 [conn1] create collection local.oplog.rs  size: 2695574937.6, capped: true, autoIndexId: false 
2020-11-26T00:26:55.853+0000 [conn1] Database::_addNamespaceToCatalog ns: local.oplog.rs
2020-11-26T00:26:55.866+0000 [conn1] ExtentManager::increaseStorageSize ns:local.oplog.rs desiredSize:2146426624 fromFreeList: 0 eloc: 1:2000
2020-11-26T00:26:55.876+0000 [conn1] ExtentManager::increaseStorageSize ns:local.oplog.rs desiredSize:549148160 fromFreeList: 0 eloc: 2:2000
2020-11-26T00:26:55.878+0000 [conn1] ******
2020-11-26T00:26:55.878+0000 [conn1] replSet info saving a newer config version to local.system.replset:  _id: "singleNodeRepl", version: 1, members: [  _id: 0, host: "staging4.domain.io:27017"  ] 
2020-11-26T00:26:55.878+0000 [conn1] Database::_addNamespaceToCatalog ns: local.system.replset
2020-11-26T00:26:55.878+0000 [conn1] ExtentManager::increaseStorageSize ns:local.system.replset desiredSize:8192 fromFreeList: 0 eloc: 2:20bb8000
2020-11-26T00:26:55.878+0000 [conn1] Database::_addNamespaceToCatalog ns: local.system.replset.$_id_
2020-11-26T00:26:55.878+0000 [conn1] build index on: local.system.replset properties:  v: 1, key:  _id: 1 , name: "_id_", ns: "local.system.replset" 
2020-11-26T00:26:55.878+0000 [conn1] local.system.replset: clearing plan cache - collection info cache reset
2020-11-26T00:26:55.878+0000 [conn1] allocating new extent
2020-11-26T00:26:55.878+0000 [conn1] ExtentManager::increaseStorageSize ns:local.system.replset.$_id_ desiredSize:131072 fromFreeList: 0 eloc: 2:20bba000
2020-11-26T00:26:55.878+0000 [conn1]     added index to empty collection
2020-11-26T00:26:55.878+0000 [conn1] local.system.replset: clearing plan cache - collection info cache reset
2020-11-26T00:26:55.878+0000 [conn1] replSet saveConfigLocally done
2020-11-26T00:26:55.878+0000 [conn1] replSet replSetInitiate config now saved locally.  Should come online in about a minute.
2020-11-26T00:26:55.878+0000 [conn1] command admin.$cmd command: replSetInitiate  replSetInitiate: undefined  keyUpdates:0 numYields:0 locks(micros) W:25362 reslen:206 25ms
2020-11-26T00:26:55.879+0000 [conn1] command test.$cmd command: isMaster  isMaster: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:270 0ms
2020-11-26T00:27:01.256+0000 [conn1] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:27:01.257+0000 [conn1] command test.$cmd command: isMaster  isMaster: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:367 0ms
2020-11-26T00:27:10.688+0000 [conn1] query local.system.replset planSummary: COLLSCAN ntoskip:0 nscanned:1 nscannedObjects:1 keyUpdates:0 numYields:0 locks(micros) r:97 nreturned:1 reslen:126 0ms
2020-11-26T00:27:10.689+0000 [conn1] command test.$cmd command: isMaster  isMaster: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:367 0ms
2020-11-26T00:27:28.889+0000 [clientcursormon]  connections:1
2020-11-26T00:27:33.333+0000 [conn1] end connection 127.0.0.1:50580 (0 connections now open)
2020-11-26T00:27:57.230+0000 [initandlisten] connection accepted from 127.0.0.1:50582 #2 (1 connection now open)
2020-11-26T00:27:57.230+0000 [conn2] command admin.$cmd command: whatsmyuri  whatsmyuri: 1  ntoreturn:1 keyUpdates:0 numYields:0  reslen:62 0ms
2020-11-26T00:27:57.232+0000 [conn2] command admin.$cmd command: getLog  getLog: "startupWarnings"  keyUpdates:0 numYields:0  reslen:70 0ms
2020-11-26T00:27:57.233+0000 [conn2] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:28:00.237+0000 [conn2] command admin.$cmd command: serverStatus  serverStatus: 1.0  keyUpdates:0 numYields:0 locks(micros) r:13 reslen:3402 0ms
2020-11-26T00:28:00.242+0000 [conn2] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:28:16.560+0000 [conn2] end connection 127.0.0.1:50582 (0 connections now open)
2020-11-26T00:32:28.904+0000 [clientcursormon]  connections:0
2020-11-26T00:36:32.398+0000 [initandlisten] connection accepted from 127.0.0.1:50588 #3 (1 connection now open)
2020-11-26T00:36:32.398+0000 [conn3] command admin.$cmd command: whatsmyuri  whatsmyuri: 1  ntoreturn:1 keyUpdates:0 numYields:0  reslen:62 0ms
2020-11-26T00:36:32.399+0000 [conn3] command admin.$cmd command: getLog  getLog: "startupWarnings"  keyUpdates:0 numYields:0  reslen:70 0ms
2020-11-26T00:36:32.400+0000 [conn3] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:36:34.603+0000 [conn3] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:36:37.326+0000 [conn3] query local.oplog.rs planSummary: COLLSCAN ntoreturn:0 ntoskip:0 nscanned:1 nscannedObjects:1 keyUpdates:0 numYields:0 locks(micros) r:66 nreturned:1 reslen:106 0ms
2020-11-26T00:36:37.328+0000 [conn3] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:37:28.832+0000 [initandlisten] connection accepted from 10.20.37.160:54484 #4 (2 connections now open)
2020-11-26T00:37:28.832+0000 [conn4] command admin.$cmd command: isMaster  isMaster: 1, compression: [], client:  driver:  name: "mongo-ruby-driver", version: "2.13.1" , os:  type: "linux", name: "linux-gnu", architecture: "x86_64" , platform: "mongoid-6.4.1, Ruby 2.6.5, x86_64-linux, x86_64-pc-linux-gnu"   keyUpdates:0 numYields:0  reslen:367 0ms
2020-11-26T00:37:28.919+0000 [clientcursormon]  connections:2
2020-11-26T00:37:33.568+0000 [initandlisten] connection accepted from 10.20.37.160:54492 #5 (3 connections now open)
2020-11-26T00:37:33.569+0000 [conn5] command admin.$cmd command: isMaster  isMaster: 1, compression: [], client:  driver:  name: "mongo-ruby-driver", version: "2.13.1" , os:  type: "linux", name: "linux-gnu", architecture: "x86_64" , platform: "mongoid-6.4.1, Ruby 2.6.5, x86_64-linux, x86_64-pc-linux-gnu"   keyUpdates:0 numYields:0  reslen:367 0ms
2020-11-26T00:37:36.586+0000 [conn3] end connection 127.0.0.1:50588 (2 connections now open)
2020-11-26T00:39:35.621+0000 [initandlisten] connection accepted from 127.0.0.1:50592 #6 (3 connections now open)
2020-11-26T00:39:35.621+0000 [conn6] command admin.$cmd command: whatsmyuri  whatsmyuri: 1  ntoreturn:1 keyUpdates:0 numYields:0  reslen:62 0ms
2020-11-26T00:39:35.622+0000 [conn6] command admin.$cmd command: getLog  getLog: "startupWarnings"  keyUpdates:0 numYields:0  reslen:70 0ms
2020-11-26T00:39:35.623+0000 [conn6] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:39:37.589+0000 [conn6] opening db:  test
2020-11-26T00:39:37.589+0000 [conn6] query test.oplog.rs planSummary: EOF ntoreturn:0 ntoskip:0 nscanned:0 nscannedObjects:0 keyUpdates:0 numYields:0 locks(micros) W:186 r:19 nreturned:0 reslen:20 0ms
2020-11-26T00:39:37.590+0000 [conn6] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:39:41.891+0000 [conn6] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:39:43.266+0000 [conn6] query local.oplog.rs planSummary: COLLSCAN ntoreturn:0 ntoskip:0 nscanned:1 nscannedObjects:1 keyUpdates:0 numYields:0 locks(micros) r:62 nreturned:1 reslen:106 0ms
2020-11-26T00:39:43.268+0000 [conn6] command admin.$cmd command: replSetGetStatus  replSetGetStatus: 1.0, forShell: 1.0  keyUpdates:0 numYields:0  reslen:300 0ms
2020-11-26T00:39:52.681+0000 [conn6] end connection 127.0.0.1:50592 (2 connections now open)
2020-11-26T00:42:28.934+0000 [clientcursormon]  connections:2

【问题讨论】:

/etc/mongod.conf的内容是什么? 您遇到了什么问题?您发布的日志没有显示任何问题。 你的配置文件语法错误,见https://docs.mongodb.com/manual/reference/configuration-options/index.html#replication-options - 它是YAML格式,空格缩进很重要。尝试命令db.adminCommand( getCmdLineOpts: 1 ) 来查看实际加载的内容。 【参考方案1】:

你不应该混合使用配置文件,即

mongod --config /etc/mongod.conf

和命令行选项

mongod --replSet rs0 --bind_ip localhost

很可能在您的配置中您没有在/etc/mongod.conf 中设置

replication:
   replSetName: <string>

因此,当您使用 service mongodb start 启动 MongoDB 时,您可能会有不同的配置。

注意,检查服务文件(在我的 Redhat 中 /etc/systemd/system/mongod.service),它甚至可能指向不同的 .conf 文件。

【讨论】:

谢谢。我按照您的建议在配置文件中包含了 replSet 并重新启动了数据库。看起来一切都很好。我在问题中包含了新的日志文件。但是,我的应用程序仍然无法像往常一样运行。

以上是关于将独立的 MongoDB 实例转换为单节点副本集的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB副本节点重建

MongoDB副本节点重建

搭建MongoDB副本集&分片

MongoDB副本集--Secondary节点实例恢复

Mongodb的副本集节点角色介绍及选举过程浅析

Mongodb备份还原常用命令