且构网

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

Centos6.5搭建mongodb分片

更新时间:2022-06-03 23:25:16

Centos6.5搭建mongodb分片

 

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.7.tgz

是我使用的版本

 

1、准备3个实例:

10.0.0.13

10.0.0.18

10.0.0.19

2、规划5个组件对应的端口号,由于一个机器需要同时部署 mongos、config server 、shard1、shard2、shard3,所以需要用端口进行区分。

这个端口可以***定义,在本文 mongos为 3717, config server 为 3710, shard1为 3711 , shard2为3712, shard3为3713

3、在每一台服务器分别启动配置服务器。

/opt/mongodb-cluster/bin/mongod --configsvr --dbpath /opt/mongodb-cluster/config/data --port 3710 --logpath /opt/mongodb-cluster/config/var/config.log --fork

4、在每一台服务器分别启动mongos服务器。

/opt/mongodb-cluster/bin/mongos  --configdb 10.0.0.13:3710,10.0.0.18:3710,10.0.0.19:3710  --port 3717 --logpath  /opt/mongodb-cluster/mongos/var/mongos.log --fork

5、配置各个分片的副本集。

#在每个机器里分别设置分片1服务器及副本集shard1

/opt/mongodb-cluster/bin/mongod --shardsvr --replSet shard1 --port 3711 --dbpath /opt/mongodb-cluster/shard1/data  --logpath /opt/mongodb-cluster/shard1/var/shard1.log --fork

#在每个机器里分别设置分片2服务器及副本集shard2

/opt/mongodb-cluster/bin/mongod --shardsvr --replSet shard2 --port 3712 --dbpath /opt/mongodb-cluster/shard2/data  --logpath /opt/mongodb-cluster/shard2/var/shard2.log --fork

#在每个机器里分别设置分片3服务器及副本集shard3

/opt/mongodb-cluster/bin/mongod --shardsvr --replSet shard3 --port 3713 --dbpath /opt/mongodb-cluster/shard3/data  --logpath /opt/mongodb-cluster/shard3/var/shard3.log --fork

#设置第一个分片副本集

/opt/mongodb-cluster/bin/mongo  127.0.0.1:3711

#使用admin数据库

use admin

#定义副本集配置

config = { _id:"shard1", members:[

                     {_id:0,host:"10.0.0.13:3711"},

                     {_id:1,host:"10.0.0.18:3711"},

                     {_id:2,host:"10.0.0.19:3711",arbiterOnly:true }

                ]

         }

#初始化副本集配置

rs.initiate(config);

#设置第二个分片副本集

/opt/mongodb-cluster/bin/mongo  127.0.0.1:3712

#使用admin数据库

use admin

#定义副本集配置

config = { _id:"shard2", members:[

                     {_id:0,host:"10.0.0.13:3712",arbiterOnly:true },

                     {_id:1,host:"10.0.0.18:3712"},

                     {_id:2,host:"10.0.0.19:3712"}

                ]

         }

#初始化副本集配置

rs.initiate(config);

#设置第三个分片副本集

/opt/mongodb-cluster/bin/mongo    127.0.0.1:3713

#使用admin数据库

use admin

#定义副本集配置

config = { _id:"shard3", members:[

                     {_id:0,host:"10.0.0.13:3713"},

                     {_id:1,host:"10.0.0.18:3713",arbiterOnly:true },

                     {_id:2,host:"10.0.0.19:3713"}

                ]

         }

#初始化副本集配置

rs.initiate(config);

6、目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到 mongos 路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。

#连接到mongos

/opt/mongodb-cluster/bin/mongo  127.0.0.1:3717

#使用admin数据库

user  admin

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

db.runCommand( { addshard : "shard1/10.0.0.13:3711,10.0.0.18:3711,10.0.0.19:3711"});

如里shard是单台服务器,用 db.runCommand( { addshard : “[: ]” } )这样的命令加入,如果shard是副本集,用db.runCommand( { addshard : “replicaSetName/[:port][,serverhostname2[:port],…]” });这样的格式表示 。

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

db.runCommand( { addshard : "shard2/10.0.0.13:3712,10.0.0.18:3712,10.0.0.19:3712"});

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

db.runCommand( { addshard : "shard3/10.0.0.13:3713,10.0.0.18:3713,10.0.0.19:3713"});

#查看分片服务器的配置

db.runCommand( { listshards : 1 } );

7、目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片,就差那么一点点,一点点。。。连接在mongos上,准备让指定的数据库、指定的集合分片生效。

#指定testdb分片生效

db.runCommand( { enablesharding :"testdb"});

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

db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )

  • 我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard1 ,shard2,shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表都需要分片!

8、测试分片配置结果。

#连接mongos服务器

/opt/mongodb-cluster/bin/mongo  127.0.0.1:3717

#使用testdb

use  testdb;

#插入测试数据

for (var i = 1; i <= 100000; i++)

db.table1.save({id:i,"test1":"testval1"});

#查看分片情况如下,部分无关信息省掉了

db.table1.stats();

 

下面是我自己为了方便,写的启动管理脚本,附在后面。

[root@tsm-pro1 sbin]# ll 
total 28 
drwxr-xr-x 2 root   root   4096 Jul  4 15:35 bak 
-rwxrwxr-x 1 mongod mongod 3945 May 20 18:51 config 
-rwxrwxr-x 1 mongod mongod 3945 May 20 18:51 mongos 
-rwxrwxr-x 1 mongod mongod 1181 May 20 19:04 node_mongo 
-rwxrwxr-x 1 mongod mongod 3945 May 20 18:51 shard1 
-rwxrwxr-x 1 mongod mongod 3945 May 20 18:51 shard2 
-rwxrwxr-x 1 mongod mongod 3945 May 20 18:51 shard3 
[root@tsm-pro1 sbin]# pwd 
/opt/mongodb-cluster/sbin 
[root@tsm-pro1 sbin]#



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