initiate

반응형

uncaught exception: Error: couldn't add user

MongoDB 설치 후 Create User 생성을 시도하다보면 아래와 같은 메세지를 맞이하게 됩니다.(docker 컨테이너 생성시 entry point로 default rs 정보를 추가할 경우는 발생 안함)

최초 설치 후 default replica set 설정을 하지 않은 경우 발생하는데, 이럴 경우 rs.initiate() 명령어로 replica 초기화를 수행하면 정상적으로 mongodb admin 생성이 가능하게 됩니다.

> db.createUser(
...   {
...      user: "admin",
...      pwd: passwordPrompt(), // or cleartext password
...      roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
...   }
... )
Enter password:
uncaught exception: Error: couldn't add user: not master :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1386:11
@(shell):1:1

> rs.status()    //replica set 상태 확인
{
    "ok" : 0,
    "errmsg" : "no replset config has been received",
    "code" : 94,
    "codeName" : "NotYetInitialized"
}

> rs.initiate()   //replica default 설정

r0:PRIMARY>    //prompt PRIMARY 변경 확인

r0:PRIMARY> rs.status()  //replica set 상태 재확인
{
    "set" : "r0",
    "date" : ISODate("2022-06-30T04:12:32.988Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 1,
    "writeMajorityCount" : 1,
    "votingMembersCount" : 1,
    "writableVotingMembersCount" : 1,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1656562345, 1),
            "t" : NumberLong(1)
        },
        "lastCommittedWallTime" : ISODate("2022-06-30T04:12:25.854Z"),
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1656562345, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityWallTime" : ISODate("2022-06-30T04:12:25.854Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1656562345, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1656562345, 1),
            "t" : NumberLong(1)
        },
        "lastAppliedWallTime" : ISODate("2022-06-30T04:12:25.854Z"),
        "lastDurableWallTime" : ISODate("2022-06-30T04:12:25.854Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(1656562335, 1),
    "electionCandidateMetrics" : {
        "lastElectionReason" : "electionTimeout",
        "lastElectionDate" : ISODate("2022-06-30T04:03:15.746Z"),
        "electionTerm" : NumberLong(1),
        "lastCommittedOpTimeAtElection" : {
            "ts" : Timestamp(0, 0),
            "t" : NumberLong(-1)
        },
        "lastSeenOpTimeAtElection" : {
            "ts" : Timestamp(1656561795, 1),
            "t" : NumberLong(-1)
        },
        "numVotesNeeded" : 1,
        "priorityAtElection" : 1,
        "electionTimeoutMillis" : NumberLong(10000),
        "newTermStartDate" : ISODate("2022-06-30T04:03:15.778Z"),
        "wMajorityWriteAvailabilityDate" : ISODate("2022-06-30T04:03:15.853Z")
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "dc0c851d73a7:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1945,
            "optime" : {
                "ts" : Timestamp(1656562345, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2022-06-30T04:12:25Z"),
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1656561795, 2),
            "electionDate" : ISODate("2022-06-30T04:03:15Z"),
            "configVersion" : 1,
            "configTerm" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1656562345, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1656562345, 1)
}

 

오늘은 MongoDB admin user 생성시 발생할 수 있는 오류를 처리하는 방법에 대하여 정리해 보았습니다.

반응형

+ Recent posts

반응형