示範的所有服務都是在一台機器上,我的目錄是F:\JAVA\MongoDB\cluster,檔案結構如下:

說明:1個配置服務結點,1個路由服務結點,三個分片結點。
啟動流程
1、啟動config服務
F:\JAVA\MongoDB\cluster\mongo\mongod -configsvr -dbpath F:\JAVA\MongoDB\cluster\data\config -port 20000 -rest
2、啟動mongos服務
F:\JAVA\MongoDB\cluster\mongo\mongos -configdb 127.0.0.1:20000 -port 30000 -chunkSize 5
3、啟動所有分片服務
F:\JAVA\MongoDB\cluster\mongo\mongod -shardsvr -master -port 27001 -dbpath F:\JAVA\MongoDB\cluster\data\shard1 -logpath F:\JAVA\MongoDB\cluster\data\shard1\log -logappend
F:\JAVA\MongoDB\cluster\mongo\mongod -shardsvr -master -port 27002 -dbpath F:\JAVA\MongoDB\cluster\data\shard2 -logpath F:\JAVA\MongoDB\cluster\data\shard2\log -logappend
F:\JAVA\MongoDB\cluster\mongo\mongod -shardsvr -master -port 27003 -dbpath F:\JAVA\MongoDB\cluster\data\shard3 -logpath F:\JAVA\MongoDB\cluster\data\shard3\log -logappend
4、連接配接mongos服務
F:\JAVA\MongoDB\cluster\mongos>mongo.exe 127.0.0.1:30000
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:30000/test
5、切換為admin使用者
mongos> use admin
switched to db admin
6、設定分片:
mongos> db.runCommand({addshard:"127.0.0.1:27001"})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:"127.0.0.1:27002"})
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.runCommand({addshard:"127.0.0.1:27003"})
{ "shardAdded" : "shard0002", "ok" : 1 }
7、建立使用者名和密碼:
mongos> db.addUser("vince","vince")
{
"user" : "vince",
"readOnly" : false,
"pwd" : "507acb958e6a63558641743dadb4014e",
"_id" : ObjectId("548be0b77bee4afe9ca1453b")
}
8、對資料庫和表設定分片:
mongos> db.runCommand({enablesharding:"message"})
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"message.history",key:{_id:1}})
{ "collectionsharded" : "message.history", "ok" : 1 }
9、查詢所有分片:
mongos> db.runCommand({listshards:1})
"shards" : [
{
"_id" : "shard0000",
"host" : "127.0.0.1:27031"
},
"_id" : "shard0001",
"host" : "127.0.0.1:27032"
}
],
"ok" : 1
常見錯誤
沒有設定分片時建立使用者報錯:
Sat Dec 13 14:38:56.116 error: {
"$err" : "error creating initial database config information :: caused b
y :: can't find a shard to put new db on",
"code" : 10185
} at src/mongo/shell/query.js:128
删除分片時上個任務未完成時報錯:
mongos> db.runCommand({removeshard:"127.0.0.1:27032"})
{ "ok" : 0, "errmsg" : "Can't have more than one draining shard at a time" }
MongoVUE連接配接測試
連接配接界面如下:
檢視分片:
每個chunk 的大小(size)(機關為MB):
單個分片存儲資料效果:
增加分片shard2和shard3後的Balancer操作過程:
分片後的資料:
檢視所有的資料庫,其中partitioned為True表示這個資料庫設定了分片。
config資料庫的結構:
檢視服務狀态,可以看到MongoDB支援的最大連接配接數為20000:
mongod啟動參數
當啟動參數報錯時會有提示,還有參數的英文解釋:
General options:
-h [ --help ] show this usage information
--version show version information
-f [ --config ] arg configuration file specifying additional options
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--quiet quieter output
--port arg specify port number - 27017 by default
--bind_ip arg comma separated list of ip addresses to listen on
- all local ips by default
--maxConns arg max number of simultaneous connections - 20000 by
default
--logpath arg log file to send write to instead of stdout - has
to be a file, not directory
--logappend append to logpath instead of over-writing
--pidfilepath arg full path to pidfile (if not set, no pidfile is
created)
--keyFile arg private key for cluster authentication
--setParameter arg Set a configurable parameter
--auth run with security
--cpu periodically show cpu and iowait utilization
--dbpath arg directory for datafiles - defaults to \data\db\
--diaglog arg 0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb each database will be stored in a separate
directory
--ipv6 enable IPv6 support (disabled by default)
--journal enable journaling
--journalCommitInterval arg how often to group/batch commit (ms)
--journalOptions arg journal diagnostic options
--jsonp allow JSONP access via http (has security
implications)
--noauth run without security
--nohttpinterface disable http interface
--nojournal disable journaling (journaling is on by default
for 64 bit)
--noprealloc disable data file preallocation - will often hurt
performance
--noscripting disable scripting engine
--notablescan do not allow table scans
--nssize arg (=16) .ns file size (in MB) for new databases
--profile arg 0=off 1=slow, 2=all
--quota limits each database to a certain number of files
(8 default)
--quotaFiles arg number of files allowed per db, requires --quota
--repair run repair on all dbs
--repairpath arg root directory for repair files - defaults to
dbpath
--rest turn on simple rest api
--slowms arg (=100) value of slow for profile and console log
--smallfiles use a smaller default file size
--syncdelay arg (=60) seconds between disk syncs (0=never, but not
recommended)
--sysinfo print some diagnostic system information
--upgrade upgrade db if needed
Windows Service Control Manager options:
--install install Windows service
--remove remove Windows service
--reinstall reinstall Windows service (equivalent to --remove
followed by --install)
--serviceName arg Windows service name
--serviceDisplayName arg Windows service display name
--serviceDescription arg Windows service description
--serviceUser arg account for service execution
--servicePassword arg password used to authenticate serviceUser
Replication options:
--oplogSize arg size to use (in MB) for replication op log. default is
5% of disk space (i.e. large is good)
Master/slave options (old; use replica sets instead):
--master master mode
--slave slave mode
--source arg when slave: specify master as <server:port>
--only arg when slave: specify a single database to replicate
--slavedelay arg specify delay (in seconds) to be used when applying
master ops to slave
--autoresync automatically resync if slave data is stale
Replica set options:
--replSet arg arg is <setname>[/<optionalseedhostlist>]
--replIndexPrefetch arg specify index prefetching behavior (if secondary)
[none|_id_only|all]
Sharding options:
--configsvr declare this is a config db of a cluster; default port
27019; default dir /data/configdb
--shardsvr declare this is a shard db of a cluster; default port
27018
mongos啟動參數
-h [ --help ] show this usage information
--version show version information
-f [ --config ] arg configuration file specifying additional options
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--quiet quieter output
--port arg specify port number - 27017 by default
--bind_ip arg comma separated list of ip addresses to listen on - all
local ips by default
--maxConns arg max number of simultaneous connections - 20000 by
default
--logpath arg log file to send write to instead of stdout - has to be
a file, not directory
--logappend append to logpath instead of over-writing
--pidfilepath arg full path to pidfile (if not set, no pidfile is
created)
--keyFile arg private key for cluster authentication
--setParameter arg Set a configurable parameter
--nohttpinterface disable http interface
--configdb arg 1 or 3 comma separated config servers
--localThreshold arg ping time (in ms) for a node to be considered local
(default 15ms)
--test just run unit tests
--upgrade upgrade meta data version
--chunkSize arg maximum amount of data per chunk
--ipv6 enable IPv6 support (disabled by default)
--jsonp allow JSONP access via http (has security implications)
--noscripting disable scripting engine
分片+主從安裝指令
主從的部署是不具有故障自動轉移功能的,而複本集的部署支援故障轉移。
主資料結點:
mongod -shardsvr -master -port 27001 -dbpath F:\JAVA\MongoDB\cluster\shard\data
從資料結點:
mongod -shardsvr -slave -source 127.0.0.1:27001 -port 27002 -dbpath F:\JAVA\MongoDB\cluster\shards\data
分片+複本集安裝
複本集安裝要對每個分片進行設定,用mongo直接連接配接分片,執行rs.initiate(cfg)進行設定,設定好後需要分别将每個分片加入config中。相關的函數有:
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates setwith configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([secs]) step down as primary (momentarily) (disconnects)
rs.syncFrom(hostportstr) make a secondary to sync from the givenmember
rs.freeze(secs) make a node ineligible to become primary for the time specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.slaveOk() shorthand for db.getMongo().setSlaveOk()
啟動指令
路由結點:
mongos -configdb 127.0.0.1:20000 -port 30000 -chunkSize 5
配置結點:
mongod -configsvr -dbpath F:\JAVA\MongoDB\cluster\config\data -port 20000 -rest
分片1:
mongod -shardsvr -replSet shard1/127.0.0.1:27051,127.0.0.1:27041 -port 27031 -dbpath F:\JAVA\MongoDB\cluster\repa_shard1\data
分片1複本集b:
mongod -shardsvr -replSet shard1/127.0.0.1:27051 -port 27041 -dbpath F:\JAVA\MongoDB\cluster\repb_shard1\data
分片1複本集c:
mongod -shardsvr -replSet shard1/127.0.0.1:27041 -port 27051 -dbpath F:\JAVA\MongoDB\cluster\repc_shard1\data
分片2:
mongod -shardsvr -replSet shard2/127.0.0.1:27052,127.0.0.1:27042 -port 27032 -dbpath F:\JAVA\MongoDB\cluster\repa_shard2\data
分片2複本集b:
mongod -shardsvr -replSet shard2/127.0.0.1:27052 -port 27042 -dbpath F:\JAVA\MongoDB\cluster\repb_shard2\data
分片2複本集c:
mongod -shardsvr -replSet shard2/127.0.0.1:27042 -port 27052 -dbpath F:\JAVA\MongoDB\cluster\repc_shard2\data
WEB控制台
分片服務與配置服務的mongod加上-rest後會有web控制台,具體的位址在啟動輸出中會有,如下
<a href="http://blog.csdn.net/zhaowen25/article/details/41871383"></a>