不正狼 发表于 2015-7-7 09:01:26

[原创]MongoDB综合实例一

CentOS-6.5单机实现mongoDB分片
环境:1)CentOS 6.5系统      2)IP:本机3)MongoDB:MongoDB-linux-x86_64-2.6.1
实现:两个副本集shard(shard-a和shard-b),每个副本集shard配置三个实例(主,从和仲裁),三个config server实例和一个mongos路由。最终通过mongos写入的数据均匀分布在两个shard中。

1.安装好CentOS 6.5并配置好IP地址,然后配置/etc/hosts文件,添加本机IP地址和域名(自定义,如:IP       master)映射。

2.下载安装MongoDB:
       1)建立活动目录:#mkdir /data
       1)官网下载MongoDB-linux-x86_64-2.6.1.tgz到/data目录
       3)解压并进入解压目录的bin目录中:
   #tar –zxvf /data/mongodb-linux-x86_64-2.6.1.tgz
   #cd /data/mongodb-linux-x86_64-2.6.1/bin
3.准备好部署sharding环境的目录:
       #mkdir /data/rs-a-1
       #mkdir /data/rs-a-2
       #mkdir /data/rs-a-3
       #mkdir /data/rs-b-1
       #mkdir /data/rs-b-2
       #mkdir /data/rs-b-3
       #mkdir /data/config-1
       #mkdir /data/config-2
       #mkdir /data/config-3
4.启动副本集
       1)启动第一个副本集
       #mongod --shardsvr --replSet shard-a --dbpath /data/rs-a-1 --logpath /data/rs-a-1.log --port 30000 --nojournal --fork &
       #mongod --shardsvr --replSet shard-a --dbpath /data/rs-a-2 --logpath /data/rs-a-2.log --port 30001 --nojournal --fork &
       #mongod --shardsvr --replSet shard-a --dbpath / data/rs-a-3 --logpath /data/rs-a-3.log --port 30002 --nojournal --fork &
       2)启动第二个副本集
       #mongod --shardsvr --replSet shard-b --dbpath /data/rs-b-1 --logpath /data/rs-b-1.log --port 30100 --nojournal --fork &
       #mongod --shardsvr --replSet shard-b --dbpath /data/rs-b-2 --logpath /data/rs-b-2.log --port 30101 --nojournal --fork &
       #mongod --shardsvr --replSet shard-b --dbpath /data/rs-b-3 --logpath /data/rs-b-3.log --port 30102 --nojournal --fork &
5.配置副本集
       1)第一个副本集上:
     #mongo --port 30000
            >rs.initiate()
            >rs.add(“master:30001”)
            >rs.add(“master:30002”,{arbiterOnly:true})
       2)第二个副本集上:
            #mongo –port 30100
            > rs.initiate()
            >rs.add(“master:30101”)
            >rs.add(“master:30102”,{arbiterOnly:true})
       3)最后在每个副本集上运行>rs.status()命令,验证两个副本集是否正常运行。如果一切顺利就可以准备启动配置服务器了。
6.启动配置服务器
       1)启动三个配置服务器实例
            #mongod --configsvr --dbpath /data/config-1 --logpath / data/config-1.log --port 27019 --nojournal --fork &
            #mongod --configsvr --dbpath /data/config-2 --logpath / data/config-2.log --port 27020 --nojournal --fork &
     #mongod --configsvr --dbpath /data/config-3 --logpath / data/config-3.log --port 27021 --nojournal --fork &

       2)用shell连接或者查看日志文件,确保每台配置服务器都已启动并已正常运行,并验证每个进程都在监听的端口。如果配置服务器都在运行了,就可以启动mongos了。
7.启动mongos路由
       1)mongos --configdb master:27019,master:27020,master:27021 --logpath /data/mongos.log --fork --port 40000
       2)用shell登录验证是否正常运行。(mongos启动时可以指定chunkSize的大小,此值为指定数据大小到达时分片)
8.配置集群
       1)指定两个之前创建的副本集,用的是每个副本集中非仲裁节点的地址:
            #mongos –port 40000
            >sh.addShard(“shard-a/master:30000,master:30001”)
            >sh.addShard(“shard-b/master:30100,master:30101”)
       如果命令执行成功,命令的响应中会包含刚添加的分片名称。
       2)使用getSiblingDB()方法切换数据库来查看效果:
            >db.getSiblingDB(“config”).shards.find()
9.开启一个数据库上的分片,这里以gizwits为例:
       1)命令:      >sh.enableSharding(“gizwits”)
       2)检查config里的数据查看所做的变更:
            >db.getSiblingDB(“config”).databases.find()
       3)给test集合做分片,需要定义一个分片键,这里使用组合分片键{username:1,_id:1}。
            >sh.shardCollection(“gizwits.test”,{username:1,_id:1})
            >sh.getSiblingDB(“config”).collections.findOne() //验证分片集合的配置
       4)到分片实例上运行getIndexes()方法进行验证。
            #mongo –port 30000
            >use gizwits
            >db.test.getIndexes()
         完成集合的分片,分片集群就准备就绪了。
10:在mongos路由中插入数据测试分片集群
       #mongo –port 40000
       >for(i=1;idb.runCommand({addshard:”shardIP:port”,maxSize:20480})
第二种:
>use config
>db.shards.update({_id:””},{$set:{maxSize:20480}})

附:[原创]MongoDB综合实例二 链接:http://www.iyunv.com/wsjhk/p/4469317.html
Mongodb启动命令mongod参数说明

mongod的主要参数有:

基本配置

--------------------------------------------------------------------------------


--quiet
# 安静输出
--port arg
# 指定服务端口号,默认端口27017
--bind_iparg
# 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
--logpatharg
# 指定MongoDB日志文件,注意是指定文件不是目录
--logappend
# 使用追加的方式写日志
--pidfilepatharg
# PID File的完整路径,如果没有设置,则没有PID文件
--keyFilearg
# 集群的私钥的完整路径,只对于ReplicaSet 架构有效
--unixSocketPrefixarg
# UNIX域套接字替代目录,(默认为/tmp)
--fork
# 以守护进程的方式运行MongoDB,创建服务器进程
--auth
# 启用验证
--cpu
# 定期显示CPU的CPU利用率和iowait
--dbpatharg
# 指定数据库路径
--diaglogarg
# diaglog选项0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb
# 设置每个数据库将被保存在一个单独的目录
--journal
# 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里
--journalOptionsarg
# 启用日志诊断选项
--ipv6
# 启用IPv6选项
--jsonp
# 允许JSONP形式通过HTTP访问(有安全影响)
--maxConnsarg
# 最大同时连接数 默认2000
--noauth
# 不启用验证
--nohttpinterface
# 关闭http接口,默认关闭27018端口访问
--noprealloc
# 禁用数据文件预分配(往往影响性能)
--noscripting
# 禁用脚本引擎
--notablescan
# 不允许表扫描
--nounixsocket
# 禁用Unix套接字监听
--nssizearg (=16)
# 设置信数据库.ns文件大小(MB)
--objcheck
# 在收到客户数据,检查的有效性,
--profilearg
# 档案参数0=off 1=slow, 2=all
--quota
# 限制每个数据库的文件数,设置默认为8
--quotaFilesarg
# numberof files allower per db, requires --quota
--rest
# 开启简单的restAPI
--repair
# 修复所有数据库runrepair on all dbs
--repairpatharg
# 修复库生成的文件的目录,默认为目录名称dbpath
--slowmsarg (=100)
# value ofslow for profile and console log
--smallfiles
# 使用较小的默认文件
--syncdelayarg (=60)
# 数据写入磁盘的时间秒数(0=never,不推荐)
--sysinfo
# 打印一些诊断系统信息
--upgrade
# 如果需要升级数据库
* Replicaton 参数

--------------------------------------------------------------------------------


--fastsync
# 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步
--autoresync
# 如果从库与主库同步数据差得多,自动重新同步,
--oplogSizearg
# 设置oplog的大小(MB)
* 主/从参数

--------------------------------------------------------------------------------


--master
# 主库模式
--slave
# 从库模式
--sourcearg
# 从库 端口号
--only arg
# 指定单一的数据库复制
--slavedelayarg
# 设置从库同步主库的延迟时间
* Replica set(副本集)选项:

--------------------------------------------------------------------------------


--replSetarg
# 设置副本集名称
* Sharding(分片)选项

--------------------------------------------------------------------------------
--configsvr
# 声明这是一个集群的config服务,默认端口27019,默认目录/data/configdb
--shardsvr
# 声明这是一个集群的分片,默认端口27018
--noMoveParanoia
# 关闭偏执为moveChunk数据保存
页: [1]
查看完整版本: [原创]MongoDB综合实例一