ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Ubuntu18.04_MongoDB Replica 구성
    데이터베이스/MongoDB 2021. 5. 9. 16:16
    728x90
    • config 파일 위치 : /home/mongo/mongodb_pri.conf. /home/mongo/mongodb_repl1.conf /home/mongo/mongodb_repl2.conf
    • data 파일 위치 : /home/mongo/data/pri /home/mongo/data/repl1 /home/mongo/data/repl2
    • /home/mongo/log/pri /home/mongo/log/repl1 /home/mongo/log/repl2

     

    같은 서버에서 test했기 때문에 디렉토리의 명칭이 모두 달라야 합니다.

    다른 서버에서 test하는 경우 디렉토리를 한 개만 만들면 됩니다.


    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/pri data/repl1 data/repl2 log/pri log/repl1 log/repl2
    
    #권한을 바꿔준다
    [root@/home] chown -R mongo:mongod engine data log

     

    MongoDB 설치

    • mongo db 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@mongodb] mv 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에서 진행
    [root] apt-get install curl libcurl4

     

    • mongo db 구성 파일 생성
    #primary
    [mongo] vi mongodb_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/pri/mongodb.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/pri
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/pri/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27017 
      bindIp: 0.0.0.0
    
    #security:
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet1" 
    
    #sharding:
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
    
    ------------------------------------------------------------------------------
    #standby
    [mongo] vi mongodb_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/repl1/mongodb1.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/repl1
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/repl1/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27018 
      bindIp: 0.0.0.0
    
    #security:
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet1" 
    
    #sharding:
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
    
    ------------------------------------------------------------------------------
    #arbitor
    [mongo] vi mongodb_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/repl2/mongodb2.log
    
    # Where and how to store data.
    storage:
      dbPath: /home/mongo/data/repl2
      journal:
        enabled: true
    #  engine:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /home/mongo/data/repl2/mongod.pid  # location of pidfile
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27019
      bindIp: 0.0.0.0
    
    #security:
    
    #operationProfiling:
    
    replication:
      replSetName: "replSet1" 
    
    #sharding:
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
    

     

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

     

    • 실행한다. (한번에 기동하기 위해 start.sh 파일에 실행 명령어를 추가)
    #파일 생성
    [mongo] vi start.sh
    mongod -config /home/mongo/mongodb_pri.cnf
    mongod -config /home/mongo/mongodb_repl1.cnf
    mongod -config /home/mongo/mongodb_repl2.cnf
    
    #실행권한 부여
    [mongo] chmod +x start.sh
    [mongo] . start.sh
    about to fork child process, waiting until server is ready for connections.
    forked process: 8945
    child process started successfully, parent exiting
    about to fork child process, waiting until server is ready for connections.
    forked process: 8981
    child process started successfully, parent exiting
    about to fork child process, waiting until server is ready for connections.
    forked process: 9017
    child process started successfully, parent exiting
    
    #db 실행
    [mongo] mongo
    > ERROR: child process failed, exited with 48 에러
    원인 : 이미 기동이 되어 SID를 사용중이기 때문에 같은 ID를 사용할 수 없다는 의미
    해결 : 죽이고 다시 시작해준다

    #root에서 죽이기
    [root] kill -9 [SID] [SID] [SID]

     

    Replica 구성

    • 몽고에 접속하여 replica를 구성한다. 
    >rs.initiate(
                {
           _id: "replSet1",
           version: 1,
           members: [
              { _id: 0, host : "172.40.40.33:27017" },
              { _id: 1, host : "172.40.40.33:27018" },
              { _id: 2, host : "172.40.40.33: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 }
    728x90

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

    Ubuntu18.04_Mongo DB Replica Shard 구성  (0) 2021.05.11
    Ubuntu18.04_MongoDB 4.4 tar설치  (0) 2021.05.07

    댓글

Designed by Tistory.