微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Centos6.5搭建mongodb分片

1e3136050d3c8e88ff77577967c7beb1

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",host:"10.0.0.13:3712",arbiterOnly:true },host:"10.0.0.18:3712"},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",host:"10.0.0.13:3713"},host:"10.0.0.18:3713",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]#

附件是节点的安装配置文件http://down.51cto.com/data/2226910

原文地址:https://www.jb51.cc/centos/381750.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。