且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mongodb分片集群配置

更新时间:2022-09-15 16:44:23

mkdir /usr/local/mongodb/etc

mkdir /usr/local/mongodb/data

mkdir /usr/local/mongodb/logs

mkdir /usr/local/mongodb/pid



1、安装软件

tar zxvf mongodb-linux-x86_64-rhel62-3.0.2.tgz && mv mongodb-linux-x86_64-rhel62-3.0.2/* /usr/local/mongodb/

2、创建mongodb数据实例配置文件,以端口区分配置文件

dbpath=/usr/local/mongodb/data/21000

logpath=/usr/local/mongodb/logs/21000.log

pidfilepath=/usr/local/mongodb/pid/21000.pid

directoryperdb=true

logappend=true

replSet=peiwo

port=21000

oplogSize=100

fork=true

noprealloc=true

启动:

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/data21000.conf




3、创建mongodb配置服务器,存储所有数据库元信息(路由、分片)的配置,以端口区分配置文件

dbpath=/usr/local/mongodb/data/22000

logpath=/usr/local/mongodb/logs/22000.log

pidfilepath=/usr/local/mongodb/pid/22000.pid

directoryperdb=true

logappend=true

port=22000

oplogSize=100

fork=true

noprealloc=true

configsvr=true


启动:

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/configsvr22000.conf

/usr/local/mongodb/bin/mongod --configsvr --dbpath /usr/local/mongodb/data/22002 --port 22002 --fork --logpath /usr/local/mongodb/logs/22002.log


4、启动路由节点

/usr/local/mongodb/bin/mongos --configdb 10.144.8.86:22000,10.144.8.86:22001,10.144.8.86:22002 --port 20000 --fork --logpath /usr/local/mongodb/logs/configdb.log


5、配置Replica Set副本集

/usr/local/mongodb/bin/mongo 10.163.111.142:7000   #ip和port是某个节点的地址


use admin


replcfg={ _id:"peiwo", members:[ {_id:0,host:'10.163.111.142:21000',priority:2}, {_id:1,host:'10.163.111.142:21001',priority:1}, {_id:2,host:'10.163.111.142:21002',arbiterOnly:true}] };


rs.initiate(replcfg)             #使配置生效


rs.status()  #查看副本集状态


db.getMongo().setSlaveOk(); #mongodb默认是从主节点读写数据的,副本节点上不允许读,需要设置副本节点可以读。


6、配置Sharding

/usr/local/mongodb/bin/mongo 10.144.8.86:20000   #这里必须连接路由节点

use admin

>sh.addShard("peiwo/10.163.111.142:21000") #test表示replica set的名字 当把主节点添加到shard以后,会自动找到set里的主,备,决策节点  

>db.runCommand({enableSharding:"peiwotestdb"})    #diameter_test is database name  

>db.runCommand( { shardCollection: "peiwotestdb.peiwotable",key:{"_id":1}})  


第一个命令很容易理解,第二个命令是对需要进行Sharding的数据库进行配置,第三个命令是对需要进行Sharding的Collection进行配置,这里的dcca_dccr_test即为Collection的名字。另外还有个key,这个是比较关键的东西,对于查询效率会有很大的影响,具体可以查看Shard Key Overview

到这里Sharding也已经搭建完成了,以上只是最简单的搭建方式,其中某些配置仍然使用的是默认配置。如果设置不当,会导致效率异常低下,所以建议大家多看看官方文档再进行默认配置的修改。







     本文转自aaron428 51CTO博客,原文链接:http://blog.51cto.com/aaronsa/1741513,如需转载请自行联系原作者