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.97 wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.7.tgz?_ga=1.235705353.941929558.1425604443 tar zxvf mongodb-linux-x86_64-2.6.7.tgz mv mongodb-linux-x86_64-2.6.7 /usr/local/mongodb ln -sf /usr/local/mongodb/bin/* /usr/sbin/ ln -sf /usr/local/mongodb/bin/mongod /usr/sbin/mongoc ln -sf /usr/local/mongodb/bin/mongod /usr/sbin/mongod1 ln -sf /usr/local/mongodb/bin/mongod /usr/sbin/mongod2 ln -sf /usr/local/mongodb/bin/mongod /usr/sbin/mongod3 mkdir -p /data/mongodb/config/{data,log} mkdir -p /data/mongodb/mongos/log mkdir -p /data/mongodb/shard{1..3}/{data,log} mkdir -p /etc/mongod openssl rand -base64 753 >/etc/mongod/keyfile chmod 600 /etc/mongod/keyfile
echo '#建立mongo1 and mongo2,mongo3配置服务' cat >/etc/mongod/mongoc.conf <<EOF #bind_ip=192.168.1.201 port=2000 fork=true logappend=true dbpath=/data/mongodb/config/data pidfilepath=/var/run/mongoc.pid #keyFile=/etc/mongod/keyfile logpath=/data/mongodb/config/log/config.log configsvr=true EOF echo '#create mongos' cat >/etc/mongod/mongos.conf<<EOF port=27017 configdb=172.31.100.97:2000,172.31.100.82:2000,172.31.100.81:2000 fork=true #keyFile=/etc/mongod/keyfile logpath=/data/mongodb/mongos/log/mongos.log EOF echo '#create shard' cat >/etc/mongod/mongod1.conf<<EOF port=3100 shardsvr=true replSet=shard1 dbpath=/data/mongodb/shard1/data logpath=/data/mongodb/shard1/log/shard1.log #keyFile=/etc/mongod/keyfile fork=true nojournal=true oplogSize=10 EOF cat >/etc/mongod/mongod2.conf<<EOF port=3200 shardsvr=true replSet=shard2 dbpath=/data/mongodb/shard2/data logpath=/data/mongodb/shard2/log/shard2.log #keyFile=/etc/mongod/keyfile fork=true nojournal=true oplogSize=10 EOF cat >/etc/mongod/mongod3.conf<<EOF port=3300 shardsvr=true replSet=shard3 dbpath=/data/mongodb/shard3/data logpath=/data/mongodb/shard3/log/shard3.log #keyFile=/etc/mongod/keyfile fork=true nojournal=true oplogSize=10 EOF
touch /etc/init.d/mongoc touch /etc/init.d/mongos touch /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/admin mongos> 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 dbs admin (empty) config 0.016GB mongos> db admin mongos> db.addUser('admin','fffR3q!P97scxxxx') WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead Successfully added user: { "user" : "admin", "roles" : [ "root" ] } sed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongoc.conf sed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongos.conf sed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongod1.conf sed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongod2.conf sed -i 's/^#keyFile/keyFile/g' /etc/mongod/mongod3.conf grep 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/functions prog=mongoc lockfile=/var/lock/subsys/$prog config=/etc/mongod/mongoc.conf start() { # 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 2 esac
/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/functions prog=mongos lockfile=/var/lock/subsys/$prog config=/etc/mongod/mongos.conf start() { # 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 2 esac
/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/functions prog=mongod1 lockfile=/var/lock/subsys/$prog config=/etc/mongod/mongod1.conf start() { # 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 2 esac
/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/functions prog=mongod2 lockfile=/var/lock/subsys/$prog config=/etc/mongod/mongod2.conf start() { # 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 2 esac
/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/functions prog=mongod3 lockfile=/var/lock/subsys/$prog config=/etc/mongod/mongod3.conf start() { # 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 2 esac