根据上篇文章《图解mongodb原理》的基本介绍,本文以具体的操作为例继续深入解析mongodb。 演示的所有服务都是在一台机器上,我的目录是f:\java\mongodb\cluster,文件结构如下: 说明:1个配置服务结点,1个路由服务结点,三个分片结点。 vcd4kpha+phn0cm9u
根据上篇文章《图解mongodb原理》的基本介绍,本文以具体的操作为例继续深入解析mongodb。
演示的所有服务都是在一台机器上,我的目录是f:\java\mongodb\cluster,文件结构如下:
说明:1个配置服务结点,1个路由服务结点,三个分片结点。
喎?http://www.2cto.com/kf/ware/vc/ target=_blank class=keylink>vcd4kpha+phn0cm9uzz7g9lavwfezzdwvc3ryb25npjwvcd4kpha+magixvs2r2nvbmzpz7f+zve8l3a+cjxwpky6xepbvkfctw9uz29eqlxjbhvzdgvyxg1vbmdvxg1vbmdvzcaty29uzmlnc3zyic1kynbhdgggrjpcskfwqvxnb25nb0rcxgnsdxn0zxjczgf0yvxjb25mawcglxbvcnqgmjawmdaglxjlc3q8l3a+cjxwpjkhosb0tq9tb25nb3o3/s7xpc9wpgo8cd5golxkqvzbxe1vbmdvrejcy2x1c3rlclxtb25nb1xtb25nb3mglwnvbmzpz2riideyny4wljaumtoymdawmcatcg9ydcazmdawmcaty2h1bmttaxplidu8l3a+cjxwpjohosb0tq/l+dpqt9bgrlf+zve8l3a+cjxwpky6xepbvkfctw9uz29eqlxjbhvzdgvyxg1vbmdvxg1vbmdvzcatc2hhcmrzdniglw1hc3rlciatcg9ydcaynzawmsatzgjwyxroiey6xepbvkfctw9uz29eqlxjbhvzdgvyxgrhdgfcc2hhcmqxic1sb2dwyxroiey6xepbvkfctw9uz29eqlxjbhvzdgvyxgrhdgfcc2hhcmqxxgxvzyatbg9nyxbwzw5kpgjypgo8yni+cky6xepbvkfctw9uz29eqlxjbhvzdgvyxg1vbmdvxg1vbmdvzcatc2hhcmrzdniglw1hc3rlciatcg9ydcaynzawmiatzgjwyxroiey6xepbvkfctw9uz29eqlxjbhvzdgvyxgrhdgfcc2hhcmqyic1sb2dwyxroiey6xepbvkfctw9uz29eqlxjbhvzdgvyxgrhdgfcc2hhcmqyxgxvzyatbg9nyxbwzw5kpgjypgo8yni+cky6xepbvkfctw9uz29eqlxjbhvzdgvyxg1vbmdvxg1vbmdvzcatc2hhcmrzdniglw1hc3rlciatcg9ydcaynzawmyatzgjwyxroiey6xepbvkfctw9uz29eqlxjbhvzdgvyxgrhdgfcc2hhcmqzic1sb2dwyxroiey6xepbvkfctw9uz29eqlxjbhvzdgvyxgrhdgfcc2hhcmqzxgxvzyatbg9nyxbwzw5kpc9wpgo8cd40oalbrl3tbw9uz29zt/7o8twvcd4kpha+rjpcskfwqvxnb25nb0rcxgnsdxn0zxjcbw9uz29zjmd0o21vbmdvlmv4zsaxmjcumc4wlje6mzawmda8yni+ck1vbmdvreigc2hlbgwgdmvyc2lvbjogmi40ljg8yni+cmnvbm5ly3rpbmcgdg86ideyny4wljaumtozmdawmc90zxn0pc9wpgo8cd41oalh0lu7zqphzg1pbtpdu6c8l3a+cjxwpm1vbmdvcyznddsgdxnligfkbwlupgjypgpzd2l0y2hlzcb0bybkyibhzg1pbjwvcd4kpha+nqgiyejww7fwxqyjujwvcd4kpha+bw9uz29zjmd0oybkyi5ydw5db21tyw5kkhthzgrzagfyzdomcxvvddsxmjcumc4wlje6mjcwmdemcxvvddt9ktxicj4keyamcxvvddtzagfyzefkzgvkjnf1b3q7idogjnf1b3q7c2hhcmqwmdawjnf1b3q7lcamcxvvddtvayzxdw90oya6idegftxicj4kbw9uz29zjmd0oybkyi5ydw5db21tyw5kkhthzgrzagfyzdomcxvvddsxmjcumc4wlje6mjcwmdimcxvvddt9ktxicj4keyamcxvvddtzagfyzefkzgvkjnf1b3q7idogjnf1b3q7c2hhcmqwmdaxjnf1b3q7lcamcxvvddtvayzxdw90oya6idegftxicj4kbw9uz29zjmd0oybkyi5ydw5db21tyw5kkhthzgrzagfyzdomcxvvddsxmjcumc4wlje6mjcwmdmmcxvvddt9ktxicj4keyamcxvvddtzagfyzefkzgvkjnf1b3q7idogjnf1b3q7c2hhcmqwmdayjnf1b3q7lcamcxvvddtvayzxdw90oya6idegftwvcd4kpha+n6gitls9qnpdu6fd+7rnw9zc66o6pc9wpgo8cd5tb25nb3mmz3q7igrilmfkzfvzzxiojnf1b3q7dmluy2umcxvvddssjnf1b3q7dmluy2umcxvvddsppgjypgp7pgjypgogicagicagiczxdw90o3vzzximcxvvddsgoiamcxvvddt2aw5jzszxdw90oyw8yni+ciagicagicagjnf1b3q7cmvhze9ubhkmcxvvddsgoibmywxzzsw8yni+ciagicagicagjnf1b3q7chdkjnf1b3q7idogjnf1b3q7nta3ywniotu4ztzhnjm1ntg2nde3ndnkywrindaxngumcxvvddsspgjypgogicagicagiczxdw90o19pzczxdw90oya6ie9iamvjdelkkczxdw90ozu0ogjlmgi3n2jlztrhzmu5y2exnduzyizxdw90oyk8yni+cn08l3a+cjxwpjihorbuyv2+3b/ius2x7cno1so31saso7o8l3a+cjxwpm1vbmdvcyznddsgzgiucnvuq29tbwfuzch7zw5hymxlc2hhcmrpbmc6jnf1b3q7bwvzc2fnzszxdw90o30ppgjypgp7iczxdw90o29rjnf1b3q7idogmsb9pgjypgptb25nb3mmz3q7igrilnj1bknvbw1hbmqoe3noyxjky29sbgvjdglvbjomcxvvddttzxnzywdllmhpc3rvcnkmcxvvddssa2v5ontfawq6mx19ktxicj4keyamcxvvddtjb2xszwn0aw9uc2hhcmrlzczxdw90oya6iczxdw90o21lc3nhz2uuaglzdg9yeszxdw90oywgjnf1b3q7b2smcxvvddsgoiaxih08l3a+cjxwpjmhorlp0a/l+dpqt9bgrko6pc9wpgo8cd5tb25nb3mmz3q7igrilnj1bknvbw1hbmqoe2xpc3rzagfyzhm6mx0ppgjypgp7pgjypgogicagicagiczxdw90o3noyxjkcyzxdw90oya6ifs8yni+ciagicagicagicagicagicb7pgjypgogicagicagicagicagicagicagicagicamcxvvddtfawqmcxvvddsgoiamcxvvddtzagfyzdawmdamcxvvddsspgjypgogicagicagicagicagicagicagicagicamcxvvddtob3n0jnf1b3q7idogjnf1b3q7mti3ljaumc4xoji3mdmxjnf1b3q7pgjypgogicagicagicagicagicagfsw8yni+ciagicagicagicagicagicb7pgjypgogicagicagicagicagicagicagicagicamcxvvddtfawqmcxvvddsgoiamcxvvddtzagfyzdawmdemcxvvddsspgjypgogicagicagicagicagicagicagicagicamcxvvddtob3n0jnf1b3q7idogjnf1b3q7mti3ljaumc4xoji3mdmyjnf1b3q7pgjypgogicagicagicagicagicagftxicj4kicagicagicbdldxicj4kicagicagicamcxvvddtvayzxdw90oya6ide8yni+cn08yni+cjwvcd4kpha+phn0cm9uzz6zo7z7to3o8zwvc3ryb25npjwvcd4kpha+w7vt0mno1so31sasyrg0tl2o08o7p7goto2jujwvcd4kpha+bw9uz29zjmd0oybkyi5hzgrvc2vykczxdw90o3zpbmnljnf1b3q7lczxdw90o3zpbmnljnf1b3q7ktxicj4ku2f0ierlyyaxmyaxndozodo1ni4xmtygzxjyb3i6ihs8yni+ciagicagicagjnf1b3q7jgvycizxdw90oya6iczxdw90o2vycm9yignyzwf0aw5nigluaxrpywwgzgf0ywjhc2ugy29uzmlnigluzm9ybwf0aw9uido6ignhdxnlzcbipgjypgp5ido6ignhbg==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
--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 [/]
--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启动参数
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
--nohttpinterface disable http interface
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
sharding options:
--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控制台,具体的地址在启动输出中会有,如下