MongoDB Sharding學習操作篇

來源:互聯網
上載者:User

標籤:mongodb   sharding   

本文使用的MongoDB版本是2.4.6,採用如下部署架構用於測試使用,不可用於線上環境。以下測試都是在本機上完成,線上環境需要各個組件分開部署。

650) this.width=650;" src="http://docs.mongodb.org/manual/_images/sharded-cluster-production-architecture.png" alt="Diagram of a sample sharded cluster for production purposes. Contains exactly 3 config servers, 2 or more ``mongos`` query routers, and at least 2 shards. The shards are replica sets." />


1.添加主機列表

vim /etc/hosts 添加如下列表

127.0.0.1  mongo-sharding-router1127.0.0.1  mongo-sharding-router2127.0.0.1  mongo-sharding-config1127.0.0.1  mongo-sharding-config2127.0.0.1  mongo-sharding-config3127.0.0.1  mongo-sharding-replica1127.0.0.1  mongo-sharding-replica2127.0.0.1  mongo-sharding-replica3127.0.0.1  mongo-sharding-replica4127.0.0.1  mongo-sharding-replica5127.0.0.1  mongo-sharding-replica6


2.配置兩個Shard,各自擁有三個成員的Replica Set

相應的主機列表

shard1

127.0.0.1  mongo-sharding-replica1     2811            Primary127.0.0.1  mongo-sharding-replica2     2822            Secondary127.0.0.1  mongo-sharding-replica3     2833            Secondary


shard2

127.0.0.1  mongo-sharding-replica4     3811            Primary127.0.0.1  mongo-sharding-replica5     3822            Secondary127.0.0.1  mongo-sharding-replica6     3833            Secondary


部署架構如下:

650) this.width=650;" src="http://docs.mongodb.org/v2.4/_images/replica-set-primary-with-secondary-and-arbiter.png" alt="Diagram of a replica set that consists of a primary, a secondary, and an arbiter." />

A.分別配置mongo-sharding-replica1,mongo-sharding-replica2和mongo-sharding-replica3的MongoDB的設定檔和啟動指令碼並啟動MongoDB

如mongo-sharding-replica1相關配置

# cat /etc/mongod2811.conf logpath=/data/app_data/mongodb/log2811/mongodb.log logappend=truefork=trueport=2811dbpath=/data/app_data/mongodb/data2811/pidfilepath=/data/app_data/mongodb/data2811/mongod.pidmaxConns=2048nohttpinterface=truedirectoryperdb=truereplSet=test_shard1


/etc/init.d/mongod2811

#!/bin/bash# mongod - Startup script for mongod# chkconfig: 35 85 15# description: Mongo is a scalable, document-oriented database.# processname: mongod# config: /etc/mongod.conf# pidfile: /data/app_data/mongodb/data/mongodb/mongod.pid. /etc/rc.d/init.d/functions# things from mongod.conf get there by mongod reading it# NOTE: if you change any OPTIONS here, you get what you pay for:# this script assumes all options are in the config file.CONFIGFILE="/etc/mongod2811.conf"OPTIONS=" -f $CONFIGFILE"SYSCONFIG="/etc/sysconfig/mongod"# FIXME: 1.9.x has a --shutdown flag that parses the config file and# shuts down the correct running pid, but that‘s unavailable in 1.8# for now.  This can go away when this script stops supporting 1.8.DBPATH=`awk -F= ‘/^dbpath=/{print $2}‘ "$CONFIGFILE"`PIDFILE=`awk -F= ‘/^pidfilepath=/{print $2}‘ "$CONFIGFILE"`mongod=${MONGOD-/data/app_platform/mongodb/bin/mongod}MONGO_USER=mongodMONGO_GROUP=mongodif [ -f "$SYSCONFIG" ]; then    . "$SYSCONFIG"fi# Handle NUMA access to CPUs (SERVER-3574)# This verifies the existence of numactl as well as testing that the command worksNUMACTL_ARGS="--interleave=all"if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/nullthen    NUMACTL="numactl $NUMACTL_ARGS"else    NUMACTL=""fistart(){  echo -n $"Starting mongod: "  daemon --user "$MONGO_USER" $NUMACTL $mongod $OPTIONS  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod}stop(){  echo -n $"Stopping mongod: "  killproc -p "$PIDFILE" -d 300 /data/app_platform/mongodb/bin/mongod  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod}restart () {    stop    start}ulimit -n 12000RETVAL=0case "$1" in  start)    start    ;;  stop)    stop    ;;  restart|reload|force-reload)    restart    ;;  condrestart)    [ -f /var/lock/subsys/mongod ] && restart || :    ;;  status)    status $mongod    RETVAL=$?    ;;  *)    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"    RETVAL=1esacexit $RETVAL


注意/data/app_data/mongodb/這個目錄的使用權限設定成為mongod進程啟動並執行使用者權限。

# service mongod2811 startStarting mongod: about to fork child process, waiting until server is ready for connections.all output going to: /data/app_data/mongodb/log2811/mongodb.logforked process: 4589child process started successfully, parent exiting                                                           [  OK  ]# service mongod2822 startStarting mongod: about to fork child process, waiting until server is ready for connections.all output going to: /data/app_data/mongodb/log2822/mongodb.logforked process: 4656child process started successfully, parent exiting                                                           [  OK  ]# service mongod2833 startStarting mongod: about to fork child process, waiting until server is ready for connections.all output going to: /data/app_data/mongodb/log2833/mongodb.logforked process: 4723child process started successfully, parent exiting                                                           [  OK  ]


B.初始化Replica Set

# /data/app_platform/mongodb/bin/mongo   --port 2811MongoDB shell version: 2.4.6connecting to: 127.0.0.1:2811/test> rs.status();{"startupStatus" : 3,"info" : "run rs.initiate(...) if not yet done for the set","ok" : 0,"errmsg" : "can‘t get local.system.replset config from self or any seed (EMPTYCONFIG)"}> rs.initiate("mongo-sharding-replica1:2811");{"info2" : "no configuration explicitly specified -- making one","me" : "mongo-sharding-replica1:2811","info" : "Config now saved locally.  Should come online in about a minute.","ok" : 1}> test_shard1:PRIMARY> test_shard1:PRIMARY> rs.status();{"set" : "test_shard1","date" : ISODate("2015-01-07T10:16:51Z"),"myState" : 1,"members" : [{"_id" : 0,"name" : "mongo-sharding-replica1:2811","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 569,"optime" : Timestamp(1420625715, 1),"optimeDate" : ISODate("2015-01-07T10:15:15Z"),"self" : true}],"ok" : 1}


C.向Replica Set添加mongo-sharding-replica2和mongo-sharding-replica3

test_shard1:PRIMARY> rs.add("mongo-sharding-replica2:2822");{ "ok" : 1 }test_shard1:PRIMARY> rs.add("mongo-sharding-replica3:2833");{ "ok" : 1 }test_shard1:PRIMARY> rs.config();{"_id" : "test_shard1","version" : 5,"members" : [{"_id" : 0,"host" : "mongo-sharding-replica1:2811"},{"_id" : 1,"host" : "mongo-sharding-replica2:2822"},{"_id" : 2,"host" : "mongo-sharding-replica3:2833"}]}test_shard1:PRIMARY>


D.查看mongo-sharding-replica2:2822和mongo-sharding-replica3:2833的狀態

# /data/app_platform/mongodb/bin/mongo --port 2822MongoDB shell version: 2.4.6connecting to: 127.0.0.1:2822/testtest_shard1:SECONDARY> db.isMaster();{"setName" : "test_shard1","ismaster" : false,"secondary" : true,"hosts" : ["mongo-sharding-replica2:2822","mongo-sharding-replica3:2833","mongo-sharding-replica1:2811"],"primary" : "mongo-sharding-replica1:2811","me" : "mongo-sharding-replica2:2822","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"localTime" : ISODate("2015-01-07T10:21:41.522Z"),"ok" : 1}test_shard1:SECONDARY>
# /data/app_platform/mongodb/bin/mongo --port 2833MongoDB shell version: 2.4.6connecting to: 127.0.0.1:2833/testtest_shard1:SECONDARY> db.isMaster();{"setName" : "test_shard1","ismaster" : false,"secondary" : true,"hosts" : ["mongo-sharding-replica3:2833","mongo-sharding-replica2:2822","mongo-sharding-replica1:2811"],"primary" : "mongo-sharding-replica1:2811","me" : "mongo-sharding-replica3:2833","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"localTime" : ISODate("2015-01-07T10:22:27.125Z"),"ok" : 1}test_shard1:SECONDARY>


同理對shard2的MongoDB執行個體進行相同的配置



3.配置config server  



參考文章:

http://docs.mongodb.org/manual/administration/sharded-cluster-deployment/


本文出自 “Linux SA John” 部落格,請務必保留此出處http://john88wang.blog.51cto.com/2165294/1600189

MongoDB Sharding學習操作篇

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.