Initializing a new replica set

In this recipe, we will be setting up the first node of a three node replica set on a single server. In a production setup, this should be on three physically separate servers.

Create individual directories for each MongoDB instance

mkdir -p /data/server{1,2,3}/{conf,logs,db}

Start the first node in the replica set

mongod --dbpath /data/server1/db --replSet MyReplicaSet

2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten] MongoDB starting : pid=4443 port=27017 dbpath=/data/server1/db 64-bit host=m0
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten] db version v3.6.5
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten] git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten] modules: none
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten] build environment:
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten]     distarch: x86_64
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2018-06-01T09:41:01.539+0000 I CONTROL  [initandlisten] options: { replication: { replSet: "MyReplicaSet" }, storage: { dbPath: "/data/server1/db" } }
2018-06-01T09:41:01.539+0000 I STORAGE  [initandlisten]
2018-06-01T09:41:01.539+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-06-01T09:41:01.539+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-06-01T09:41:01.539+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3479M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),cache_cursors=false,log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-06-01T09:41:02.017+0000 I STORAGE  [initandlisten] WiredTiger message [1527846062:17665][4443:0x7f16806659c0], txn-recover: Set global recovery timestamp: 0
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten]
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten]
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten]
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten]
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-06-01T09:41:02.044+0000 I CONTROL  [initandlisten]
2018-06-01T09:41:02.046+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with no UUID.
2018-06-01T09:41:02.054+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/server1/db/diagnostic.data'
2018-06-01T09:41:02.054+0000 I STORAGE  [initandlisten] createCollection: local.me with no UUID.
2018-06-01T09:41:02.064+0000 I STORAGE  [initandlisten] createCollection: local.replset.minvalid with no UUID.
2018-06-01T09:41:02.071+0000 I REPL     [initandlisten] Did not find local voted for document at startup.
2018-06-01T09:41:02.071+0000 I REPL     [initandlisten] Did not find local Rollback ID document at startup. Creating one.
2018-06-01T09:41:02.071+0000 I STORAGE  [initandlisten] createCollection: local.system.rollback.id with no UUID.
2018-06-01T09:41:02.077+0000 I REPL     [initandlisten] Initialized the rollback ID to 1
2018-06-01T09:41:02.077+0000 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2018-06-01T09:41:02.078+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

Initialize the replica set

mongo

> rs.status()
{
        "info" : "run rs.initiate(...) if not yet done for the set",
        "ok" : 0,
        "errmsg" : "no replset config has been received",
        "code" : 94,
        "codeName" : "NotYetInitialized"
}

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "localhost:27017",
        "ok" : 1,
        "operationTime" : Timestamp(1527846189, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1527846189, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

MyReplicaSet:OTHER> rs.status()
{
        "set" : "MyReplicaSet",
        "date" : ISODate("2018-06-01T09:43:58.177Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1527846231, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1527846231, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1527846231, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1527846231, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 177,
                        "optime" : {
                                "ts" : Timestamp(1527846231, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-06-01T09:43:51Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1527846189, 2),
                        "electionDate" : ISODate("2018-06-01T09:43:09Z"),
                        "configVersion" : 1,
                        "self" : true
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1527846231, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1527846231, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

Switch back to the mongod Terminal window and inspect the server logs

2018-06-01T09:41:02.077+0000 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2018-06-01T09:41:02.078+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-06-01T09:42:10.570+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:58082 #1 (1 connection now open)
2018-06-01T09:42:10.570+0000 I NETWORK  [conn1] received client metadata from 127.0.0.1:58082 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.6.5" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04" } }
2018-06-01T09:43:09.546+0000 I COMMAND  [conn1] initiate : no configuration specified. Using a default configuration for the set
2018-06-01T09:43:09.546+0000 I COMMAND  [conn1] created this configuration for initiation : { _id: "MyReplicaSet", version: 1, members: [ { _id: 0, host: "localhost:27017" } ] }
2018-06-01T09:43:09.546+0000 I REPL     [conn1] replSetInitiate admin command received from client
2018-06-01T09:43:09.546+0000 I REPL     [conn1] replSetInitiate config object with 1 members parses ok
2018-06-01T09:43:09.546+0000 I REPL     [conn1] ******
2018-06-01T09:43:09.546+0000 I REPL     [conn1] creating replication oplog of size: 2371MB...
2018-06-01T09:43:09.546+0000 I STORAGE  [conn1] createCollection: local.oplog.rs with no UUID.
2018-06-01T09:43:09.550+0000 I STORAGE  [conn1] Starting WiredTigerRecordStoreThread local.oplog.rs
2018-06-01T09:43:09.550+0000 I STORAGE  [conn1] The size storer reports that the oplog contains 0 records totaling to 0 bytes
2018-06-01T09:43:09.550+0000 I STORAGE  [conn1] Scanning the oplog to determine where to place markers for truncation
2018-06-01T09:43:09.558+0000 I REPL     [conn1] ******
2018-06-01T09:43:09.558+0000 I STORAGE  [conn1] createCollection: local.system.replset with no UUID.
2018-06-01T09:43:09.565+0000 I COMMAND  [conn1] Assigning UUID 39579608-cb61-44e6-9cad-1ab9fc47fb76 to collection local.system.rollback.id
2018-06-01T09:43:09.565+0000 I COMMAND  [conn1] Assigning UUID 57d6a0d2-1616-4a8b-b130-688ca0cae323 to collection local.system.replset
2018-06-01T09:43:09.565+0000 I COMMAND  [conn1] Assigning UUID 4d867c95-e419-4ed6-a248-61e5a4703b20 to collection local.me
2018-06-01T09:43:09.565+0000 I COMMAND  [conn1] Assigning UUID 2131fd88-2f36-4739-a520-8d8b34ef4d48 to collection local.startup_log
2018-06-01T09:43:09.565+0000 I COMMAND  [conn1] Assigning UUID 43290c58-c255-40c2-a762-8b4348d8a478 to collection local.replset.minvalid
2018-06-01T09:43:09.566+0000 I COMMAND  [conn1] Assigning UUID f3fd2395-ae8b-464a-9164-bc4a011f327b to collection local.oplog.rs
2018-06-01T09:43:09.566+0000 I STORAGE  [conn1] createCollection: admin.system.version with provided UUID: 9e3510f9-982d-46e6-a0d9-7e1340caba2e
2018-06-01T09:43:09.573+0000 I COMMAND  [conn1] setting featureCompatibilityVersion to 3.6
2018-06-01T09:43:09.573+0000 I NETWORK  [conn1] Skip closing connection for connection # 1
2018-06-01T09:43:09.573+0000 I REPL     [conn1] New replica set config in use: { _id: "MyReplicaSet", version: 1, protocolVersion: 1, members: [ { _id: 0, host: "localhost:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, catchUpTimeoutMillis: -1, catchUpTakeoverDelayMillis: 30000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 }, replicaSetId: ObjectId('5b11152d01691a2f1ed3ec86') } }
2018-06-01T09:43:09.573+0000 I REPL     [conn1] This node is localhost:27017 in the config
2018-06-01T09:43:09.573+0000 I REPL     [conn1] transition to STARTUP2 from STARTUP
2018-06-01T09:43:09.573+0000 I REPL     [conn1] Starting replication storage threads
2018-06-01T09:43:09.573+0000 I REPL     [conn1] transition to RECOVERING from STARTUP2
2018-06-01T09:43:09.573+0000 I REPL     [conn1] Starting replication fetcher thread
2018-06-01T09:43:09.573+0000 I REPL     [conn1] Starting replication applier thread
2018-06-01T09:43:09.573+0000 I REPL     [conn1] Starting replication reporter thread
2018-06-01T09:43:09.575+0000 I REPL     [rsSync] transition to SECONDARY from RECOVERING
2018-06-01T09:43:09.575+0000 I REPL     [rsSync] conducting a dry run election to see if we could be elected. current term: 0
2018-06-01T09:43:09.575+0000 I REPL     [replexec-0] dry election run succeeded, running for election in term 1
2018-06-01T09:43:09.575+0000 I STORAGE  [replexec-0] createCollection: local.replset.election with generated UUID: d69fb8af-8496-40d6-b1e6-d2b7145da253
2018-06-01T09:43:09.582+0000 I REPL     [replexec-0] election succeeded, assuming primary role in term 1
2018-06-01T09:43:09.582+0000 I REPL     [replexec-0] transition to PRIMARY from SECONDARY
2018-06-01T09:43:09.582+0000 I REPL     [replexec-0] Entering primary catch-up mode.
2018-06-01T09:43:09.582+0000 I REPL     [replexec-0] Exited primary catch-up mode.
2018-06-01T09:43:11.575+0000 I STORAGE  [rsSync] createCollection: config.transactions with generated UUID: ecc98ad2-2192-4986-8ed4-3ac0a6243444
2018-06-01T09:43:11.582+0000 I REPL     [rsSync] transition to primary complete; database writes are now permitted
2018-06-01T09:43:11.582+0000 I STORAGE  [monitoring keys for HMAC] createCollection: admin.system.keys with generated UUID: c55a13fa-8d21-4ec9-a459-fbe8149438a5

results matching ""

    No results matching ""