图解MongoDB原理(二)

原创
2016-06-07 16:12:22 1453浏览

根据上篇文章《图解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+CkY6XEpBVkFcTW9uZ29EQlx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YnI+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控制台,具体的地址在启动输出中会有,如下


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。