聊聊MongoDB(三)MongoDB3.2.9集群搭建

服务器

浏览数:151

2020-6-22

1.    安装步骤:

#解压
tar -zxvf mongodb-linux-x86_64-ubuntu1404-3.2.9.tgz

mv mongodb-linux-x86_64-ubuntu1404-3.2.9.tgz mongodb

cd mongodb

#创建存储数据目录

mkdir data

cd data

#创建存放数据库的目录db和存放日志信息的目录log

mkdir db log

cd log

touch MongoDB.log

启动:

cd /home/nosql/mongodb/bin

./mongod --dbpath="/home/nosql/mongodb/data/db/" --logpath="/home/nosql/mongodb/data/log/MongoDB.log" –httpinterface

进入shell窗口

./mongo

2.    Mongo集群-分片(Replica Set + Sharding)

Mongo支持三种集群方式:Replica Set (副本集)/ Sharding(数据分片/ Master-Slaver(主从)

 

2.1    建立目录

 

分片数据文件存储目录:

cd data

mkdir -p shard1/db1-1 shard1/db1-2 shard1/db1-3 shard2/db2-1 shard2/db2-2 shard2/db2-3 shard3/db3-1 shard3/db3-2 shard3/db3-1

配置

mkdir -p config/configdb1 config/configdb2 config/configdb3

2.2    启动副本集

2.2.1    副本集db1启动:

./mongod --dbpath=../data/shard1/db1-1/ --port 11001 --replSet db1 --logpath=../data/log/db1-1.log --pidfilepath=../data/db1-1.pid &  

./mongod --dbpath=../data/shard1/db1-2/ --port 11002 --replSet db1 --logpath=../data/log/db1-2.log --pidfilepath=../data/db1-2.pid &  

./mongod --dbpath=../data/shard1/db1-3/ --port 11003 --replSet db1 --logpath=../data/log/db1-3.log --pidfilepath=../data/db1-3.pid & 

 

2.2.2    副本集db2启动:

./mongod --dbpath=../data/shard2/db2-1/ --port 21001 --replSet db2 --logpath=../data/log/db2-1.log --pidfilepath=../data/db2-1.pid &  

./mongod --dbpath=../data/shard2/db2-2/ --port 21002 --replSet db2 --logpath=../data/log/db2-2.log --pidfilepath=../data/db2-2.pid &  

./mongod --dbpath=../data/shard2/db2-3/ --port 21003 --replSet db2 --logpath=../data/log/db2-3.log --pidfilepath=../data/db2-3.pid &  

 

2.2.3    副本集db3启动:

./mongod --dbpath=../data/shard3/db3-1/ --port 31001 --replSet db3 --logpath=../data/log/db3-1.log --pidfilepath=../data/db3-1.pid &  

./mongod --dbpath=../data/shard3/db3-2/ --port 31002 --replSet db3 --logpath=../data/log/db3-2.log --pidfilepath=../data/db3-2.pid &  

./mongod --dbpath=../data/shard3/db3-3/ --port 31003 --replSet db3 --logpath=../data/log/db3-3.log --pidfilepath=../data/db3-3.pid &

  

 

2.3    副本集的配置以及初始化

2.3.1    定义副本集配置db1

./mongo --port 11001

config={

... "_id":"db1",

... "members":[

... {"_id":0,"host":"192.168.111.139:11001"},

... {"_id":1,"host":"192.168.111.139:11002"},

... {"_id":2,"host":"192.168.111.139:11003"}

... ]

... }


rs.initiate(config)

exit

 

 

2.3.2    定义副本集配置db2

./mongo --port 21001

config={

... "_id":"db2",

... "members":[

... {"_id":0,"host":"192.168.111.139:21001"},

... {"_id":1,"host":"192.168.111.139:21002"},

... {"_id":2,"host":"192.168.111.139:21003"}

... ]

... }

rs.initiate(config)

exit

 

2.3.3    定义副本集配置db3

./mongo --port 31001

config={

... "_id":"db3",

... "members":[

... {"_id":0,"host":"192.168.111.139:31001"},

... {"_id":1,"host":"192.168.111.139:31002"},

... {"_id":2,"host":"192.168.111.139:31003"}

... ]

... }

rs.initiate(config)

exit

2.4    启动配置库

./mongod --configsvr --dbpath=../data/config/configdb1 --port 20000 --logpath=../data/log/configdb1.log –pidfilepath=../data/config/configdb1.pid &

./mongod --configsvr --dbpath=../data/config/configdb2 --port 20001 --logpath=../data/log/configdb2.log –pidfilepath=../data/config/configdb2.pid &

./mongod --configsvr --dbpath=../data/config/configdb3 --port 20002 --logpath=../data/log/configdb3.log --pidfilepath=../data/config/configdb3.pid &

2.5    启动mongos服务器

./mongos --port 30000 --configdb 192.168.111.139:20000,192.168.111.139:20001,192.168.111.139:20002 --logpath=../data/log/mongos.log &  

2.6    分配副本集

#连接到mongos

./mongo --port 30000

#使用admin数据库

use admin

#串联路由服务器与分配副本集1

sh.addShard("db1/192.168. 111.139:11001")

#串联路由服务器与分配副本集2

sh.addShard("db2/192.168. 111.139:21001")

#串联路由服务器与分配副本集3

sh.addShard("db3/192.168. 111.139:31001")

#查看分片服务器的配置

db.runCommand({listshards:1})  

 

 

2.7    分片生效

同样是在admin数据库下
#指定test分片生效

db.runCommand({"enablesharding":"0929test"})  

 #指定数据库里需要分片的集合和片键

sh.shardCollection("0929test.person",{_id:'hashed'})

 

 

2.8    测试

测试

use 0929test

插入测试数据:

for(var i=0;i<100000;i++){db.person.insert({name:"lzq"+i});}  

查看分片信息:

查看分片情况,省略了部分信息

db.person.stats();
#分片信息:
{

    "sharded": true,

    "capped": false,

    "ns": "0929test.person",

    "count": 100000,

    "size": 4088890,

    "storageSize": 2080768,

    "totalIndexSize": 5459968,

    "indexSizes": {

        "_id_": 958464,

        "_id_hashed": 4501504

    },

    "avgObjSize": 40.8889,

    "nindexes": 2,

    "nchunks": 6,

    "shards": {

        "db1": {

            "ns": "0929test.person",

            "count": 33250,

            "size": 1359581,

            "avgObjSize": 40,

            "storageSize": 692224,

            "capped": false,

            "nindexes": 2,

            "totalIndexSize": 1847296,

            "indexSizes": {

                "_id_": 315392,

                "_id_hashed": 1531904

            },

            "ok": 1

        },

        "db2": {

            "ns": "0929test.person",

            "count": 33372,

            "size": 1364574,

            "avgObjSize": 40,

            "storageSize": 692224,

            "capped": false,

            "nindexes": 2,

            "totalIndexSize": 1785856,

            "indexSizes": {

                "_id_": 323584,

                "_id_hashed": 1462272

            },

            "ok": 1

        },

        "db3": {

            "ns": "0929test.person",

            "count": 33378,

            "size": 1364735,

            "avgObjSize": 40,

            "storageSize": 696320,

            "capped": false,

            "nindexes": 2,

            "totalIndexSize": 1826816,

            "indexSizes": {

                "_id_": 319488,

                "_id_hashed": 1507328

            },

            "ok": 1

        }

    },

    "ok": 1

}

可以看出分片比较均匀

 

参考资料:

http://www.lanceyan.com/tech/arch/mongodb_shard1.html

http://blog.csdn.net/luonanqin/article/details/8497860

http://www.jianshu.com/p/2825a66d6aed

https://my.oschina.net/weiweiblog/blog/657766

http://hunan.iteye.com/blog/2095605

作者:你我他有个梦