ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Ubuntu18.04_Mongo DB Replica Shard 구성
    데이터베이스/MongoDB 2021. 5. 11. 15:27
    728x90

    > replSet1 구성

    • config 파일 위치 : /home/mongo/config
    • data 파일 위치 : /home/mongo/data/replSet1
    • log 파일 위치 : /home/mongo/log/replSet1

     

    > replSet2 구성

    • config 파일 위치 : /home/mongo/config
    • data 파일 위치 : /home/mongo/data/replSet2
    • log 파일 위치 : /home/mongo/log/replSet2

    MongoDB 유저 생성

    • mongo 유저 및 mongod 그룹을 생성한다.
    [root] adduser mongo
    -> mongo
    -> 1
    -> 
    ->
    ->
    -> y
    
    [root] groupadd mongod
    [root] usermod -a -G mongod mongo

     

    • 필요 디렉토리를 생성한다.
    #필요 디렉토리도 같이 생성해준다
    [root@/home/mongo] mkdir -p engine data/replSet1/pri data/replSet1/repl1 data/replSet1/repl2 log/replSet1/pri log/replSet1/repl1 log/replSet1/repl2
    
    [root@/home/mongo] mkdir -p data/replSet2/pri data/replSet2/repl1 data/replSet2/repl2 log/replSet2/pri log/replSet2/repl1 log/replSet2/repl2
    
    #권한을 바꿔준다
    [root@/home] chown -R mongo:mongod engine data log

     

    • shard 구성을 위한 key 디렉토리를 생성한다. 
    [mongo] mkdir -p /home/mongo/engine/shard/keyfile  
    [mongo] openssl rand -base64 741 > /home/mongo/engine/shard/keyfile/shard_keyfile
    [mongo] chmod 600 /home/mongo/engine/shard/keyfile/shard_keyfile

    MongoDB Install

    • mongodb tar 파일을 받아온다.
    [root] su - mongo
    
    [mongo] wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.4.5.tgz
    [mongo] tar -xvzf mongodb-linux-x86_64-ubuntu1804-4.4.5.tgz
    [mongo] mv mongodb-linux-x86_64-ubuntu1804-4.4.5 mongodb

     

    • bin 파일을 원하는 디렉토리로 옮겨준다. 
    [mongo] mv mongodb/bin /home/mongo/engine 

     

    • 환경 변수를 추가한다. 
    [mongo@~] vi .bash_profile 
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi
    
    if [ -f ~/.profile ]; then
        . ~/.profile
    fi
    
    ##LOCAL PATH
    PATH=$PATH:$HOME/.local/bin:$HOME/bin
    export PATH
    
    ##MONGO PATH
    MONGODB_HOME=/home/mongo
     
    PATH=$PATH:$MONGODB_HOME/engine/bin
     
    export MONGODB_HOME
    export PATH
    
    
    #적용
    [mongo] . ~/.bash_profile 
    
    
    #mongo 버전을 확인하여 환경변수 설정에 이상이 없는지 확인한다. 
    [mongo] mongo -version
    
    #정상
    MongoDB shell version v4.4.5
    Build Info: {
        "version": "4.4.5",
        "gitVersion": "ff5cb77101b052fa02da43b8538093486cf9b3f7",
        "openSSLVersion": "OpenSSL 1.1.1  11 Sep 2018",
        "modules": [],
        "allocator": "tcmalloc",
        "environment": {
            "distmod": "ubuntu1804",
            "distarch": "x86_64",
            "target_arch": "x86_64"
        }
    }
    > mongo: error while loading shared libraries: libcurl.so.4: cannot open shared object file 에러
    원인 : 최신 커뮤니티 패키지를 설치하지 않았기 때문이다. 
    해결 : 패키지를 설치해준다. 
    #libcurl4에 libcurl.so.4가 포함되어 있음

    [root] apt-get install curl libcurl4

     

    • MongoDB 구성 파일 생성 (경로 : /home/mongo/config)

    replSet1의 cnf 파일 구성 

    더보기
    #primary
    [mongo] vi replSet1_pri.cnf
    #mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /home/mongo/log/replSet1/pri/mongodb.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/replSet1/pri
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/replSet1/pri/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27017 
      bindIp: 0.0.0.0
    
    # security:
    security:
      authorization: enabled
      keyFile: /home/mongo/engine/shard/keyfile/shard_keyfile
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet1" 
    
    sharding:
       clusterRole: shardsvr
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
    
    ------------------------------------------------------------------------------
    #standby
    [mongo] vi replSet1_repl1.cnf
    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /home/mongo/log/replSet1/repl1/mongodb1.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/replSet1/repl1
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/replSet1/repl1/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27018 
      bindIp: 0.0.0.0
    
    # security:
    security:
      authorization: enabled
      keyFile: /home/mongo/engine/shard/keyfile/shard_keyfile
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet1" 
    
    sharding:
       clusterRole: shardsvr
    
    ## Enterprise-Onlysharding:
    
    #auditLog:
    
    #snmp:
    
    ------------------------------------------------------------------------------
    #stand by
    [mongo] vi replSet1_repl2.cnf
    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /home/mongo/log/replSet1/repl2/mongodb2.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/replSet1/repl2
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/replSet1/repl2/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27019
      bindIp: 0.0.0.0
    
    # security:
    security:
      authorization: enabled
      keyFile: /home/mongo/engine/shard/keyfile/shard_keyfile
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet1" 
    
    sharding:
       clusterRole: shardsvr
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
    

    replSet2의 cnf 파일 구성

    더보기
    #primary
    [mongo] vi replSet2_pri.cnf
    #mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /home/mongo/log/replSet2/pri/mongodb.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/replSet2/pri
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/replSet2/pri/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27027 
      bindIp: 0.0.0.0
    
    # security:
    security:
      authorization: enabled
      keyFile: /home/mongo/engine/shard/keyfile/shard_keyfile
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet2" 
    
    sharding:
       clusterRole: shardsvr
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
    
    ------------------------------------------------------------------------------
    #standby
    [mongo] vi replSet2_repl1.cnf
    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /home/mongo/log/replSet2/repl1/mongodb1.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/replSet2/repl1
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/replSet2/repl1/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27028 
      bindIp: 0.0.0.0
    
    # security:
    security:
      authorization: enabled
      keyFile: /home/mongo/engine/shard/keyfile/shard_keyfile
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet2" 
    
    sharding:
       clusterRole: shardsvr
    
    ## Enterprise-Onlysharding:
    
    #auditLog:
    
    #snmp:
    
    ------------------------------------------------------------------------------
    #stand by
    [mongo] vi replSet2_repl2.cnf
    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /home/mongo/log/replSet2/repl2/mongodb2.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/replSet2/repl2
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/replSet2/repl2/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27029
      bindIp: 0.0.0.0
    
    # security:
    security:
      authorization: enabled
      keyFile: /home/mongo/engine/shard/keyfile/shard_keyfile
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet2" 
    
    sharding:
       clusterRole: shardsvr
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:

     

    • 각 cnf 파일에서 설정한 포트 접속 허용 
    #root에서 진행
    [root] ufw allow 27017/tcp
    [root] ufw allow 27018/tcp
    [root] ufw allow 27019/tcp
    
    [root] ufw allow 27027/tcp
    [root] ufw allow 27028/tcp
    [root] ufw allow 27029/tcp

     

    • 실행한다. (한번에 기동하기 위해 start.sh 파일에 실행 명령어를 추가함)
    [mongo] vi start.sh
    mongod -config /home/mongo/config/replSet1_pri.cnf
    mongod -config /home/mongo/config/replSet1_repl1.cnf
    mongod -config /home/mongo/config/replSet1_repl2.cnf
    
    mongod -config /home/mongo/config/replSet2_pri.cnf
    mongod -config /home/mongo/config/replSet2_repl1.cnf
    mongod -config /home/mongo/config/replSet2_repl2.cnf
    
    #실행권한 부여
    [mongo] chmod +x start.sh
    [mongo] . start.sh
    about to fork child process, waiting until server is ready for connections.
    forked process: 25988
    child process started successfully, parent exiting
    about to fork child process, waiting until server is ready for connections.
    forked process: 26068
    child process started successfully, parent exiting
    about to fork child process, waiting until server is ready for connections.
    forked process: 26145
    child process started successfully, parent exiting
    about to fork child process, waiting until server is ready for connections.
    forked process: 26241
    child process started successfully, parent exiting
    about to fork child process, waiting until server is ready for connections.
    forked process: 26300
    child process started successfully, parent exiting
    about to fork child process, waiting until server is ready for connections.
    forked process: 26348
    child process started successfully, parent exiting
    
    #db 실행
    [mongo] mongo 
    > ERROR: child process failed, exited with 48 에러
    원인 : 이미 기동이 되어 PID를 사용중이기 때문에 같은 ID를 사용할 수 없다는 의미
    해결 : 죽이고 다시 시작해준다
    [mongo] kill -9 [PID] [PID] [PID]

     

    • 종료한다. (한번에 종료하기 위해 stop.sh 파일에 실행 명령어를 추가함)
    #파일 생성
    [mongo] vi start.sh
    # stop mongos
    mongo admin -u admin -p 'mongo' --port 26000 -eval 'db.shutdownServer();'
    
    mongod --shutdown -config /home/mongo/config/replSet1_pri.cnf
    mongod --shutdown -config /home/mongo/config/replSet1_repl1.cnf
    mongod --shutdown -config /home/mongo/config/replSet1_repl2.cnf
    
    mongod --shutdown -config /home/mongo/config/replSet2_pri.cnf
    mongod --shutdown -config /home/mongo/config/replSet2_repl1.cnf
    mongod --shutdown -config /home/mongo/config/replSet2_repl2.cnf
    
    # stop config
    mongod --shutdown -f /home/mongo/config/config.cnf
    
    

    종료 명령어가 잘 돌아가는지 확인하고, . start.sh 로 몽고db를 재기동 해준다. 


    Replica Set1 구성

    • Replica 구성
    >rs.initiate(
                {
           _id: "replSet1",
           version: 1,
           members: [
              { _id: 0, host : "[replSet1의 primary IP]:27017" },
              { _id: 1, host : "[replSet1의 secondary IP]:27018" },
              { _id: 2, host : "[replSet1의 secondary IP]:27019" }
    ] }
    )
    
    #결과
    {
    	"ok" : 1,
    	"$clusterTime" : {
    		"clusterTime" : Timestamp(1620375642, 1),
    		"signature" : {
    			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    			"keyId" : NumberLong(0)
    		}
    	},
    	"operationTime" : Timestamp(1620375642, 1)
    }

     

    • 재접속
    [mongo] mongo -port 27017 #primary 접속
    > use admin
    > db.createUser(
       {
         user: 'admin',
         pwd: 'mongo',
         roles: ['root']
    } )
    
    #결과
    Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
    [mongo] mongo -u admin -p mongo -port 27018 #secondary 접속
    SECONDARY> rs.secondaryOk() 
    SECONDARY> use test
    SECONDARY> db.testcol.find()
    
    #결과
    { "_id" : ObjectId("6094fa47016f4b5945f35d20"), "seq" : 1 }

     

    • Priority 설정
    #ReplSet 설정 확인
    PRIMARY> rs.conf()
    
    
    #설정을 변수로 복사
    PRIMARY> cfg = rs.conf()
    
    
    #항상 PRIMARY로 설정한 서버의 priority를 올려줌
    PRIMARY> cfg.members[0].priority = 2
    
    #재설정
    PRIMARY> rs.reconfig(cfg)
    
    #확인
    PRIMARY> rs.conf()

    Replica Set2 구성

    • Replica 구성
    >rs.initiate(
                {
           _id: "replSet2",
           version: 1,
           members: [
              { _id: 0, host : "[replSet2의 primary IP]:27027" },
              { _id: 1, host : "[replSet2의 secondary IP]:27028" },
              { _id: 2, host : "[replSet2의 secondary IP]:27029" }
    ] }
    )
    
    #결과
    {
    	"ok" : 1,
    	"$clusterTime" : {
    		"clusterTime" : Timestamp(1620709032, 1),
    		"signature" : {
    			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    			"keyId" : NumberLong(0)
    		}
    	},
    	"operationTime" : Timestamp(1620709032, 1)
    }

     

    • 재접속
    [mongo] mongo -port 27027 #primary 접속
    > use admin
    > db.createUser(
       {
         user: 'admin',
         pwd: 'mongo',
         roles: ['root']
    } )
    
    #결과
    Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
    [mongo] mongo -u admin -p mongo -port 27028 #secondary 접속
    SECONDARY> rs.secondaryOk() 
    SECONDARY> use test
    SECONDARY> db.testcol.find()
    
    #결과
    { "_id" : ObjectId("6094fa47016f4b5945f35d20"), "seq" : 1 }

     

    • Priority 설정
    #ReplSet 설정 확인
    PRIMARY> rs.conf()
    
    
    #설정을 변수로 복사
    PRIMARY> cfg = rs.conf()
    
    
    #항상 PRIMARY로 설정한 서버의 priority를 올려줌
    PRIMARY> cfg.members[0].priority = 2
    
    #재설정
    PRIMARY> rs.reconfig(cfg)
    
    #확인
    PRIMARY> rs.conf()

    config 와 mongos 구성

    • cnf 파일 구성
    #config 구성
    [mongo@data] mkdir config
    
    [mongo@config] vi config.cnf
    sharding:
      clusterRole: configsvr
    replication:
      replSetName: csShard
    security:
      keyFile: /home/mongo/engine/shard/keyfile/shard_keyfile
    net:
      bindIp: 0.0.0.0
      port: 26001
    systemLog:
      destination: file
      path: /home/mongo/log/config.log
      logAppend: true
    processManagement:
      fork: true
    storage:
      dbPath: /home/mongo/data/config
    
    
    #mongos 구성
    [mongo@config] vi mongos.cnf
    sharding:
      configDB: csShard/sy:26001
    security:
      keyFile: /home/mongo/engine/shard/keyfile/shard_keyfile
    net:
      bindIp: 0.0.0.0
      port: 26000
    systemLog:
      destination: file
      path: /home/mongo/log/mongos.log
      logAppend: true
    processManagement:
      fork: true

     

    • 실행
    # start config server
    mongod -config /home/mongo/config/config.cnf
    
    # start mongos
    mongos -config /home/mongo/config/mongos.cnf &

     

    • config 접속
    [mongo] mongo --port=26001
    >use admin
    
    
    >rs.initiate(
               {
          _id: "csShard",
          version: 1,
          members: [
             { _id: 0, host : "[client서버의 IP]:26001" },
          ]
       }
    )
    
    #결과
    {
    	"ok" : 1,
    	"$gleStats" : {
    		"lastOpTime" : Timestamp(1620709901, 1),
    		"electionId" : ObjectId("000000000000000000000000")
    	},
    	"lastCommittedOpTime" : Timestamp(0, 0)
    }
    
    > use admin
    
    > db.createUser(
      {
        user: 'admin',
        pwd: 'mongo',
        roles: ['root']
      }
    )
    
    #결과
    Successfully added user: { "user" : "admin", "roles" : [ "root" ] }

     

    • mongos 접속
    [mongo] mongo -u admin -p mongo --port=26000

     

    • shard 구성

    !주의! Replica Set이 모두 running인 상태에서 진행해야 한다. 

    #replSet1의 샤드
    sh.addShard("replSet1/[replSet1의 primary IP]:27017,[replSet1의 secondary IP]:27018,[replSet1의 secondary IP]:27019")
    
    #결과
    {
    	"shardAdded" : "replSet1",
    	"ok" : 1,
    	"operationTime" : Timestamp(1620712180, 9),
    	"$clusterTime" : {
    		"clusterTime" : Timestamp(1620712180, 10),
    		"signature" : {
    			"hash" : BinData(0,"ynfisqZ56035CBxB6YR6O/K2lqo="),
    			"keyId" : NumberLong("6960896021098397718")
    		}
    	}
    }
    
    
    
    #replSet2의 샤드
    sh.addShard("replSet2/[replSet2의 primary IP]:27027,[replSet2의 secondary IP]:27028,[replSet2의 secondary IP]:27029")
    
    #결과
    {
    	"shardAdded" : "replSet2",
    	"ok" : 1,
    	"operationTime" : Timestamp(1620712290, 5),
    	"$clusterTime" : {
    		"clusterTime" : Timestamp(1620712290, 5),
    		"signature" : {
    			"hash" : BinData(0,"J2qYZlgRGdx1o7eS+8HnQKn+Plo="),
    			"keyId" : NumberLong("6960896021098397718")
    		}
    	}
    }
    > Could not find host matching read preference { mode: \"primary\" } for set replSet1 에러
    원인 : mongodb가 구동되지 않아서 생기는 오류
    해결 : mongodb를 구동한다.
    [mongo] . start.sh

     


    + 0518 open ssl 관련 내용 추가

    • open ssl 생성하지 않아, 보안 key가 없는 경우
    ##각각 노드의 설정파일에 키를 disabled 처리한다.
    vi /etc/[각 노드의 컨피그 파일명].cnf
    
    # security:
    security:
      authorization: disabled
    ##config 서버와 mongos 서버의 구성파일 설정을 변경한다. 
    [mongo] vi config/config.cnf
    security:
    	authorization: "disabled" ##need ""
    
    [mongo] vi config/mongos.cnf
    #security:
    728x90

    '데이터베이스 > MongoDB' 카테고리의 다른 글

    Ubuntu18.04_MongoDB Replica 구성  (0) 2021.05.09
    Ubuntu18.04_MongoDB 4.4 tar설치  (0) 2021.05.07

    댓글

Designed by Tistory.