Mongodb sharding


구성정보 : 


mongo01 : 192.168.0.2

 mongo02 : 192.168.0.3 



mongodb version : 3.6.2


그림 : https://docs.mongodb.com/manual/sharding/




위와같이 구성할꺼다.


 A MongoDB sharded cluster consists of the following components:

  • shard: Each shard contains a subset of the sharded data. Each shard can be deployed as a replica set.
  • mongos: The mongos acts as a query router, providing an interface between client applications and the sharded cluster.
  • config servers: Config servers store metadata and configuration settings for the cluster. As of MongoDB 3.4, config servers must be deployed as a replica set (CSRS).



설치법 : 

별도의 설명이 없다면 양쪽서버에서 진행하시면 됩니다. ^^


먼저 다운받는다.

$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.2.tgz 


압출을 풀어준다.

$ tar -zxvf mongodb-linux-x86_64-rhel70-3.6.2.tgz 


링크걸어준다. ( 편리하게 접근하기 위함 ) 

$ ln -s mongodb-linux-x86_64-rhel70-3.6.2 mongodb


mongodb폴더에서 각각의 dir을 만들어준다. 

$ mkdir pid
$ mkdir shardsvr
$ mkdir configsvr # 이 부분은 mongo01 에서만 진행합니다.

$ mkdir log

$ mkdir conf


먼저 config servers을 설치하고 구동시켜야한다. 

config servers은 1대아님 3대가 권장설정이기 때문에 mongo02서버에서는 생략 ( mongo_install_dir 에는 설치한 mongo dir경로를 넣어줌 된다 ) 

 

 $ vi $mongo_install_dir/conf/configsvr.conf

systemLog:
   verbosity: 5
   destination: file
   path: $mongo_install_dir/log/configsvr.log
   logAppend: true

storage:
   dbPath: $mongo_install_dir/mongodb/configsvr

processManagement:
   fork: true
   pidFilePath: $mongo_install_dir/mongodb/pid/configsvr.pid

net:
   bindIp: 0.0.0.0 # 이 부분이 중요하다 127.0.0.1일 시 local에서만 사용가능 
   port: 27022

#security:
#   keyFile: /home/unisecure/mongodb/key/mongodb.key

sharding:
   clusterRole: configsvr

replication:
   replSetName: configGroup



실행

#mongoldb/bin폴더에서 mongod 을 통해 실행

 $ ./mongod --config $mongo_install_dir/mongodb/conf/configsvr.conf 



config server설정 ( mongo01 에서 진행 )

$ mongo --host localhost --port 27022


rs.initiate({id:"configGroup",configsvr:true,members:[{ _id:0 , host:"192.168.0.2:27022"} ] })

# 이와 같이 설정해준다. config server가 여러대라면 members에 같은 형식으로 추가해주면된다.


rs.initiate({})

#config server가 한대라면 이 와같이 입력해도 무방하다.



# 상태확인

rs.status()



# 아래와 같이 나오면 성공 

configGroup:SECONDARY> rs.status()

{

        "set" : "configGroup",

        "date" : ISODate("2018-01-26T01:41:15.233Z"),

        "myState" : 1,

        "term" : NumberLong(1),

        "configsvr" : true,

        "heartbeatIntervalMillis" : NumberLong(2000),

        "optimes" : {

                "lastCommittedOpTime" : {

                        "ts" : Timestamp(1516930863, 3),

                        "t" : NumberLong(1)

                },

                "readConcernMajorityOpTime" : {

                        "ts" : Timestamp(1516930863, 3),

                        "t" : NumberLong(1)

                },

                "appliedOpTime" : {

                        "ts" : Timestamp(1516930863, 3),

                        "t" : NumberLong(1)

                },

                "durableOpTime" : {

                        "ts" : Timestamp(1516930863, 3),

                        "t" : NumberLong(1)

                }

        },

        "members" : [

                {

                        "_id" : 0,

                        "name" : “mongo01:27022",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 501,

                        "optime" : {

                                "ts" : Timestamp(1516930863, 3),

                                "t" : NumberLong(1)

                        },

                        "optimeDate" : ISODate("2018-01-26T01:41:03Z"),

                        "infoMessage" : "could not find member to sync from",

                        "electionTime" : Timestamp(1516930855, 1),

                        "electionDate" : ISODate("2018-01-26T01:40:55Z"),

                        "configVersion" : 1,

                        "self" : true

                }

        ],

        "ok" : 1,

        "operationTime" : Timestamp(1516930863, 3)

}

 



shard서버 구동 ( mongo01, mongo02서버에서 진행 )

# $mongo_install_dir / conf 디렉토리에서 파일수정

 $ vi shardsvr.conf

systemLog:

   verbosity: 4

   destination: file

   path: $mongo_install_dir/log/shardsvr.log

   quiet: true

   logAppend: true

   logRotate: rename


storage:

   dbPath: $mongo_install_dir/shardsvr

   directoryPerDB: true

   journal:

      enabled: true


processManagement:

   fork: true

   pidFilePath: $mongo_install_dir/pid/shardsvr.pid


net:

   bindIp: 0.0.0.0

   port: 27033


sharding:

   clusterRole: shardsvr


shard서버구동

# $mongo_install_dir / bin 디렉토리에서 mongod 실행

  ./mongod --config $mongo_install_dir/conf/shardsvr.conf 





mongos서버 구동 ( mongo01, mongo02서버에서 진행 )

# $mongo_install_dir / bin 디렉토리에서 mongos 실행 

./mongos --configdb configGroup/mongo01:27022 --bind_ip 0.0.0.0 --port 27011 --logpath $mongo_install_dir/mongodb/log/mongos.log & 


# admin으로 실행

mongos> use admin

switched to db admin


mongos> sh.addShard("mongo01:27033")

{

        "shardAdded" : "shard0000",

        "ok" : 1,

        "$clusterTime" : {

                "clusterTime" : Timestamp(1516934294, 4),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        },

        "operationTime" : Timestamp(1516934294, 4)

}

mongos> sh.addShard("mongo02:27033")

{

        "shardAdded" : "shard0001",

        "ok" : 1,

        "$clusterTime" : {

                "clusterTime" : Timestamp(1516934300, 2),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        },

        "operationTime" : Timestamp(1516934300, 2)

}


# 샤딩 상태확인

mongos> sh.status()

config.locks collection empty or missing. be sure you are connected to a mongos

--- Sharding Status ---

  sharding version: {

        "_id" : 1,

        "minCompatibleVersion" : 5,

        "currentVersion" : 6,

        "clusterId" : ObjectId("5a6a8cf0744d26f58db56f3b")

  }

  shards:

        {  "_id" : "shard0000",  "host" : “mongo01:27033",  "state" : 1 }

        {  "_id" : "shard0001",  "host" : "mongo02:27033",  "state" : 1 }

  active mongoses:

        "3.6.2" : 1

  balancer:

        Currently enabled:  yes

        Currently running:  no

        Failed balancer rounds in last 5 attempts:  0

        Migration Results for the last 24 hours:

                No recent migrations

  databases:


 


mongos 에서 database와 collection을 활성화시켜야 사용가능하다.


샤딩활성화 ( test데이터베이스 활성화 ) 

 mongos> sh.enableSharding("test")

{

        "ok" : 1,

        "$clusterTime" : {

                "clusterTime" : Timestamp(1516934488, 5),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        },

        "operationTime" : Timestamp(1516934488, 5)

}




컬렉션 샤딩설정 ( test db의 t1 컬렉션 활성화 ) 

mongos> sh.shardCollection("test.t1",{regdate:"hashed"})

{

        "collectionsharded" : "test.t1",

        "collectionUUID" : BinData(4,"eM/P3uVVS9OKYMNGmylDwA=="),

        "ok" : 1,

        "$clusterTime" : {

                "clusterTime" : Timestamp(1516934531, 4),

                "signature" : {

                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

                        "keyId" : NumberLong(0)

                }

        },

        "operationTime" : Timestamp(1516934531, 4)

}

 


설정확인


mongos> sh.status()

config.locks collection empty or missing. be sure you are connected to a mongos

--- Sharding Status ---

  sharding version: {

        "_id" : 1,

        "minCompatibleVersion" : 5,

        "currentVersion" : 6,

        "clusterId" : ObjectId("5a6a8cf0744d26f58db56f3b")

  }

  shards:

        {  "_id" : "shard0000",  "host" : “mongo01:27033",  "state" : 1 }

        {  "_id" : "shard0001",  "host" : "mongo02:27033",  "state" : 1 }

  active mongoses:

        "3.6.2" : 1

  balancer:

        Currently enabled:  yes

        Currently running:  no

        Failed balancer rounds in last 5 attempts:  0

        Migration Results for the last 24 hours:

                No recent migrations

  databases:

        {  "_id" : "test",  "primary" : "shard0001",  "partitioned" : true }

                test.t1

                        shard key: { "regdate" : "hashed" }

                        unique: false

                        balancing: true

                        chunks:

                                shard0001       2

                        { "regdate" : { "$minKey" : 1 } } -->> { "regdate" : NumberLong(0) } on : shard0001 Timestamp(1, 0)

                        { "regdate" : NumberLong(0) } -->> { "regdate" : { "$maxKey" : 1 } } on : shard0001 Timestamp(1, 1) 



검증

샤딩서버로 접속

  ./mongo localhost:27033


# mongo01에서 진행

# mongos서버에서 지정했던 sharding 테이블에 데이터를 집어넣는다.


> use test

switched to db test

>

>

> db.t1.insert([ { name: "Abet", age: 19, regdate: ISODate() },{ name: "Betty", age: 20, regdate: ISODate() } ]);

BulkWriteResult({

        "writeErrors" : [ ],

        "writeConcernErrors" : [ ],

        "nInserted" : 2,

        "nUpserted" : 0,

        "nMatched" : 0,

        "nModified" : 0,

        "nRemoved" : 0,

        "upserted" : [ ]

})

>

>

> show collections;

t1

> db.t1.find({name:"Abet"})

{ "_id" : ObjectId("5a6a9bb0e9e058fc826770c5"), "name" : "Abet", "age" : 19, "regdate" : ISODate("2018-01-26T03:08:32.564Z") }



mongo02에서도 체크 

 ./mongo localhost:27033

> use test

switched to db test

> db.t1.find({name:"Abet"})

{ "_id" : ObjectId("5a6a9bb0e9e058fc826770c5"), "name" : "Abet", "age" : 19, "regdate" : ISODate("2018-01-26T03:08:32.564Z") }












 

'apps > mongodb' 카테고리의 다른 글

Mongodb란?  (0) 2018.02.10

+ Recent posts