huijial 发表于 2018-10-25 12:58:47

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/mongo127.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/mongo127.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/mongo127.0.0.1:3717
  #使用admin数据库
  useradmin
  #串联路由服务器与分配副本集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 表需要分片,根据>
  8、测试分片配置结果。
  #连接mongos服务器
  /opt/mongodb-cluster/bin/mongo127.0.0.1:3717
  #使用testdb
  usetestdb;
  #插入测试数据
  for (var i = 1; i
页: [1]
查看完整版本: Centos6.5搭建mongodb分片