你的位置:首页 > 数据库

[数据库]mongodb分片集群安装教程


mongodb 集群包含副本集群,主从集群以及分片集群,分片集群比较复杂,这里测试我采用了三台机器,交差部署

blog地址:http://www.cnblogs.com/caoguo

 

一 .环境:
#mongo1:
#172.31.100.97

#mongo2:
#172.31.100.81

#mongo3:
#172.31.100.82

#mongo1 and mongo2,mongo3

 

二 .基本安装

 

yum install ntp#add#restrict 172.31.100.0/24/etc/init.d/iptables stop && chkconfig iptables off
setenforce 0 && echo "SELINUX=disabled" >/etc/selinux/config#edit /etc/crontab*/10 * * * * root ntpdate 172.31.100.97wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.7.tgz?_ga=1.235705353.941929558.1425604443tar zxvf mongodb-linux-x86_64-2.6.7.tgzmv mongodb-linux-x86_64-2.6.7 /usr/local/mongodbln -sf /usr/local/mongodb/bin/* /usr/sbin/ln -sf /usr/local/mongodb/bin/mongod /usr/sbin/mongocln -sf /usr/local/mongodb/bin/mongod /usr/sbin/mongod1ln -sf /usr/local/mongodb/bin/mongod /usr/sbin/mongod2ln -sf /usr/local/mongodb/bin/mongod /usr/sbin/mongod3mkdir -p /data/mongodb/config/{data,log}mkdir -p /data/mongodb/mongos/logmkdir -p /data/mongodb/shard{1..3}/{data,log}mkdir -p /etc/mongodopenssl rand -base64 753 >/etc/mongod/keyfilechmod 600 /etc/mongod/keyfile

 

echo '#建立mongo1 and mongo2,mongo3配置服务'cat >/etc/mongod/mongoc.conf <<EOF#bind_ip=192.168.1.201port=2000fork=truelogappend=truedbpath=/data/mongodb/config/datapidfilepath=/var/run/mongoc.pid#keyFile=/etc/mongod/keyfilelogpath=/data/mongodb/config/log/config.logconfigsvr=trueEOFecho '#create mongos'cat >/etc/mongod/mongos.conf<<EOFport=27017configdb=172.31.100.97:2000,172.31.100.82:2000,172.31.100.81:2000fork=true#keyFile=/etc/mongod/keyfilelogpath=/data/mongodb/mongos/log/mongos.logEOFecho '#create shard'cat >/etc/mongod/mongod1.conf<<EOFport=3100shardsvr=truereplSet=shard1dbpath=/data/mongodb/shard1/datalogpath=/data/mongodb/shard1/log/shard1.log#keyFile=/etc/mongod/keyfilefork=truenojournal=trueoplogSize=10EOFcat >/etc/mongod/mongod2.conf<<EOFport=3200shardsvr=truereplSet=shard2dbpath=/data/mongodb/shard2/datalogpath=/data/mongodb/shard2/log/shard2.log#keyFile=/etc/mongod/keyfilefork=truenojournal=trueoplogSize=10EOFcat >/etc/mongod/mongod3.conf<<EOFport=3300shardsvr=truereplSet=shard3dbpath=/data/mongodb/shard3/datalogpath=/data/mongodb/shard3/log/shard3.log#keyFile=/etc/mongod/keyfilefork=truenojournal=trueoplogSize=10EOF

 

touch /etc/init.d/mongoctouch /etc/init.d/mongostouch /etc/init.d/mongod{1..3}chmod 755 /etc/init.d/mongo*

 

#/etc/init.d/mongoc stop && \/etc/init.d/mongos stop && \/etc/init.d/mongod1 stop && \/etc/init.d/mongod2 stop && \/etc/init.d/mongod3 stop && \netstat -ntlp|grep mongo#/etc/init.d/mongoc start && \/etc/init.d/mongos start && \/etc/init.d/mongod1 start && \/etc/init.d/mongod2 start && \/etc/init.d/mongod3 start && \netstat -ntlp|grep mongo#/etc/init.d/mongoc restart && \/etc/init.d/mongos restart && \/etc/init.d/mongod1 restart && \/etc/init.d/mongod2 restart && \/etc/init.d/mongod3 restart && \netstat -ntlp|grep mongo

 

三 .配置

 

[root@Centos ~]# mongo localhost:3100/admin#复制集一(连到主节点上去操作)> rs.initiate({"_id":"shard1",   "members":[    {"_id":1, "host":"172.31.100.97:3100",priority:2},    {"_id":2, "host":"172.31.100.82:3100"},    {"_id":3, "host":"172.31.100.81:3100"}  ]})> rs.status()#复制集二[root@Centos ~]# mongo localhost:3200/admin> rs.initiate({"_id":"shard2",   "members":[    {"_id":1, "host":"172.31.100.97:3200"},    {"_id":2, "host":"172.31.100.82:3200",priority:2},    {"_id":3, "host":"172.31.100.81:3200"}  ]})> rs.status()#复制集三[root@Centos ~]# mongo localhost:3300/admin> rs.initiate({"_id":"shard3",   "members":[    {"_id":1, "host":"172.31.100.97:3300"},    {"_id":2, "host":"172.31.100.82:3300"},    {"_id":3, "host":"172.31.100.81:3300", priority:2}  ]})> rs.status()#添加副本shard1:PRIMARY> rs.remove("172.31.100.81:3100")shard1:PRIMARY> rs.add("172.31.100.81:3100")#移除分片db.runCommand( { removeshard: "shard1" } )#mongos加入分片主机[root@Centos ~]# mongo localhost:27017/adminmongos> db.runCommand({ addshard : "shard1/172.31.100.97:3100,172.31.100.82:3100,172.31.100.81:3100"});mongos> db.runCommand({ addshard : "shard2/172.31.100.97:3200,172.31.100.82:3200,172.31.100.81:3200"});mongos> db.runCommand({ addshard : "shard3/172.31.100.97:3300,172.31.100.82:3300,172.31.100.81:3300"});mongos> db.runCommand({ listshards : 1 } );#允许数据库分片mongos> db.runCommand({"enablesharding" : "seller"})#设置集合分片mongos> db.runCommand({"shardcollection" : "seller.person", "key" : {"_id" : 1}})#设置用户验证mongos> show dbsadmin  (empty)config 0.016GBmongos> dbadminmongos> db.addUser('admin','fffR3q!P97scxxxx')WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "admin", "roles" : [ "root" ] }sed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongoc.confsed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongos.confsed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongod1.confsed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongod2.confsed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongod3.confgrep keyFile /etc/mongod/mongo*

 

 

四 .测试

############################# 数据测试        ################################插入数据mongos> use seller> db.person.insert({"name":"jack","age":20})> db.person.insert({"name":"joe","age":25})#批量插入数据#for (var i = 1; i <= 10000000; i++)db.person.save({id:i,name:"username"+i,age:i});#删除所有数据#db.person.remove()#查看数据分布db.person.stats()db.person.getShardDistribution()#查询数据> db.person.find({"name":"joe"}){ "_id" : ObjectId("53150a14d39c963b140c6f6a"), "name" : "joe", "age" : 25 }#更新数据> db.person.update({"name":"joe"},{"name":"joe","age":30})> db.person.find({"name":"joe"}){ "_id" : ObjectId("53150a14d39c963b140c6f6a"), "name" : "joe", "age" : 30 }#移除数据> db.person.remove({"name":"joe"})> db.person.find(){ "_id" : ObjectId("53150a0cd39c963b140c6f69"), "name" : "jack", "age" : 20 }> db.person.remove()> db.person.find()> db.person.count()0#查看块大小db.settings.find()#设置块大小db.settings.save( { _id:"chunksize", value: <sizeInMB> } )#空间压缩db.repairDatabase()#压缩指定collection,这最好停止写数据,且只能在副本节点操作db.person.runCommand("compact");db.runCommand({compact : 'person');

 

 

 

由于启动脚本比较多我放在最后:

/etc/init.d/mongoc 

vi /etc/init.d/mongoc#!/bin/bash## mongodb  This shell script takes care of starting and stopping## chkconfig: - 58 74### END INIT INFO# Source function library.. /etc/init.d/functionsprog=mongoclockfile=/var/lock/subsys/$progconfig=/etc/mongod/mongoc.confstart() {    # Start daemons.    echo -n $"Starting $prog: "    daemon $prog --config $config $OPTIONS  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && touch $lockfile  return $RETVAL}stop() {    echo -n $"Shutting down $prog: "  killproc $prog  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && rm -f $lockfile  return $RETVAL}# See how we were called.case "$1" in start)  start  ;; stop)  stop  ;; status)  status $prog  ;; restart|force-reload)  stop  start  ;; *)  echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"  exit 2esac

 

/etc/init.d/mongos

vi /etc/init.d/mongos#!/bin/bash## mongodb  This shell script takes care of starting and stopping## chkconfig: - 58 74### END INIT INFO# Source function library.. /etc/init.d/functionsprog=mongoslockfile=/var/lock/subsys/$progconfig=/etc/mongod/mongos.confstart() {    # Start daemons.    echo -n $"Starting $prog: "    daemon $prog --config $config $OPTIONS  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && touch $lockfile  return $RETVAL}stop() {    echo -n $"Shutting down $prog: "  killproc $prog  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && rm -f $lockfile  return $RETVAL}# See how we were called.case "$1" in start)  start  ;; stop)  stop  ;; status)  status $prog  ;; restart|force-reload)  stop  start  ;; *)  echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"  exit 2esac

 

/etc/init.d/mongod1

vi /etc/init.d/mongod1#!/bin/bash## mongodb  This shell script takes care of starting and stopping## chkconfig: - 58 74### END INIT INFO# Source function library.. /etc/init.d/functionsprog=mongod1lockfile=/var/lock/subsys/$progconfig=/etc/mongod/mongod1.confstart() {    # Start daemons.    echo -n $"Starting $prog: "    daemon $prog --config $config $OPTIONS  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && touch $lockfile  return $RETVAL}stop() {    echo -n $"Shutting down $prog: "  killproc $prog  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && rm -f $lockfile  return $RETVAL}# See how we were called.case "$1" in start)  start  ;; stop)  stop  ;; status)  status $prog  ;; restart|force-reload)  stop  start  ;; *)  echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"  exit 2esac

 

/etc/init.d/mongod2

#!/bin/bash## mongodb  This shell script takes care of starting and stopping## chkconfig: - 58 74### END INIT INFO# Source function library.. /etc/init.d/functionsprog=mongod2lockfile=/var/lock/subsys/$progconfig=/etc/mongod/mongod2.confstart() {    # Start daemons.    echo -n $"Starting $prog: "    daemon $prog --config $config $OPTIONS  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && touch $lockfile  return $RETVAL}stop() {    echo -n $"Shutting down $prog: "  killproc $prog  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && rm -f $lockfile  return $RETVAL}# See how we were called.case "$1" in start)  start  ;; stop)  stop  ;; status)  status $prog  ;; restart|force-reload)  stop  start  ;; *)  echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"  exit 2esac

 

/etc/init.d/mongod3

#!/bin/bash## mongodb  This shell script takes care of starting and stopping## chkconfig: - 58 74### END INIT INFO# Source function library.. /etc/init.d/functionsprog=mongod3lockfile=/var/lock/subsys/$progconfig=/etc/mongod/mongod3.confstart() {    # Start daemons.    echo -n $"Starting $prog: "    daemon $prog --config $config $OPTIONS  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && touch $lockfile  return $RETVAL}stop() {    echo -n $"Shutting down $prog: "  killproc $prog  RETVAL=$?    echo  [ $RETVAL -eq 0 ] && rm -f $lockfile  return $RETVAL}# See how we were called.case "$1" in start)  start  ;; stop)  stop  ;; status)  status $prog  ;; restart|force-reload)  stop  start  ;; *)  echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"  exit 2esac