您的位置:首页 > 数据库 > Mongodb

图解MongoDB原理(二)

2014-12-13 18:44 260 查看
根据上篇文章《图解MongoDB原理》的基本介绍,本文以具体的操作为例继续深入解析mongodb。

演示的所有服务都是在一台机器上,我的目录是F:\J***A\MongoDB\cluster,文件结构如下:



说明:1个配置服务结点,1个路由服务结点,三个分片结点。

启动流程

1、启动config服务

F:\J***A\MongoDB\cluster\mongo\mongod -configsvr -dbpath F:\J***A\MongoDB\cluster\data\config -port 20000 -rest

2、启动mongos服务

F:\J***A\MongoDB\cluster\mongo\mongos -configdb 127.0.0.1:20000 -port 30000 -chunkSize 5

3、启动所有分片服务

F:\J***A\MongoDB\cluster\mongo\mongod -shardsvr -master -port 27001 -dbpath F:\J***A\MongoDB\cluster\data\shard1 -logpath F:\J***A\MongoDB\cluster\data\shard1\log -logappend

F:\J***A\MongoDB\cluster\mongo\mongod -shardsvr -master -port 27002 -dbpath F:\J***A\MongoDB\cluster\data\shard2 -logpath F:\J***A\MongoDB\cluster\data\shard2\log -logappend

F:\J***A\MongoDB\cluster\mongo\mongod -shardsvr -master -port 27003 -dbpath F:\J***A\MongoDB\cluster\data\shard3 -logpath F:\J***A\MongoDB\cluster\data\shard3\log -logappend

4、连接mongos服务

F:\J***A\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

}

常见错误

没有设置分片时创建用户报错:

mongos> db.addUser("vince","vince")

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启动参数

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:\J***A\MongoDB\cluster\shard\data

从数据结点:

mongod -shardsvr -slave -source 127.0.0.1:27001 -port 27002 -dbpath F:\J***A\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:\J***A\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:\J***A\MongoDB\cluster\repa_shard1\data

分片1复本集b:

mongod -shardsvr -replSet shard1/127.0.0.1:27051 -port 27041 -dbpath F:\J***A\MongoDB\cluster\repb_shard1\data

分片1复本集c:

mongod -shardsvr -replSet shard1/127.0.0.1:27041 -port 27051 -dbpath F:\J***A\MongoDB\cluster\repc_shard1\data

分片2:

mongod -shardsvr -replSet shard2/127.0.0.1:27052,127.0.0.1:27042 -port 27032 -dbpath F:\J***A\MongoDB\cluster\repa_shard2\data

分片2复本集b:

mongod -shardsvr -replSet shard2/127.0.0.1:27052 -port 27042 -dbpath F:\J***A\MongoDB\cluster\repb_shard2\data

分片2复本集c:

mongod -shardsvr -replSet shard2/127.0.0.1:27042 -port 27052 -dbpath F:\J***A\MongoDB\cluster\repc_shard2\data

WEB控制台

分片服务与配置服务的mongod加上-rest后会有web控制台,具体的地址在启动输出中会有,如下



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: