🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 1.环境信息 | 服务器1 | 服务器2 | 服务器3 | | --- | --- | ---| | 192.168.86.131 | 192.168.86.132 | 192.168.86.133 | | mongos | mongos | mongos | | config server | config server | config server | | shard server1 主节点 | shard server1 副节点 | shard server1 仲裁 | | shard server2 仲裁| shard server2 主节点 | shard server2 副节点 | | shard server3 副节点 | shard server3 仲裁 | shard server3 主节点 | ## 2.端口分配 mongos:20000 config:21000 shard1:27001 shard2:27002 shard3:27003 ## 3.下载安装mongodb ``` wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-4.0.9.tgz tar -xzvf mongodb-linux-x86_64-amazon-4.0.9.tgz  -C /usr/local/ ``` ## 4.建立软连接并配置path ``` cd /usr/local/ ln -s  /usr/local/mongodb-linux-x86_64-amazon-4.0.9 /usr/local/mongodb ``` 编辑/etc/profile ``` export MONGODB_HOME=/usr/local/mongodb export PATH=$MONGODB_HOME/bin:$PATH ``` ``` source /etc/profile ``` ## 5.创建日志和数据目录 ``` mkdir -p /usr/local/mongodb/conf \ mkdir -p /usr/local/mongodb/mongos/log \ mkdir -p /usr/local/mongodb/config/data \ mkdir -p /usr/local/mongodb/config/log \ mkdir -p /usr/local/mongodb/shard1/data \ mkdir -p /usr/local/mongodb/shard1/log \ mkdir -p /usr/local/mongodb/shard2/data \ mkdir -p /usr/local/mongodb/shard2/log \ mkdir -p /usr/local/mongodb/shard3/data \ mkdir -p /usr/local/mongodb/shard3/log ``` ## 6.config server 配置服务器 **mongodb3.4版本之后要求配置服务器也创建副本集,不然集群搭建不成功。** ``` vi /usr/local/mongodb/conf/config.conf ``` ``` ## 配置文件内容 pidfilepath = /usr/local/mongodb/config/log/configsrv.pid dbpath = /usr/local/mongodb/config/data logpath = /usr/local/mongodb/config/log/configsrv.log logappend = true bind_ip = 0.0.0.0 port = 21000 fork = true #declare this is a config db of a cluster; configsvr = true #副本集名称 replSet = configs #设置最大连接数 maxConns = 20000 ``` 启动三台服务器的config server ``` mongod -f /usr/local/mongodb/conf/config.conf ``` 登录任意一台配置服务器,初始化配置副本集 ``` #连接MongoDB mongo --port 21000 ``` 配置config变量 ``` config = { _id : "configs", members : [ { _id : 0, host : "192.168.86.131:21000" }, { _id : 1, host : "192.168.86.132:21000" }, { _id : 2, host : "192.168.86.133:21000" } ] } ``` 初始化副本集 ``` rs.initiate(config) ``` 其中`"_id" : "configs"`应与配置文件中replication.replSetName一致,"members"中的"host"为三个节点的ip和port 响应内容如下 ``` config = { _id : "configs", members: [ { _id : 0, host : "192.168.86.131:21000" }, { _id : 1, host : "192.168.86.132:21000" }, { _id : 2, host : "192.168.86.133:21000" } ] } { "_id" : "configs", "members" : [ { "_id" : 0, "host" : "192.168.86.131:21000" }, { "_id" : 1, "host" : "192.168.86.132:21000" }, { "_id" : 2, "host" : "192.168.86.133:21000" }, ] } > rs.initiate(config); { "ok" : 1, "operationTime" : Timestamp(1517369899, 1), "$gleStats" : { "lastOpTime" : Timestamp(1517369899, 1), "electionId" : ObjectId("000000000000000000000000")     }, "$clusterTime" : { "clusterTime" : Timestamp(1517369899, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0)         }     } } configs:SECONDARY> ``` ## 7.配置分片服务器 ### 7.1.设置第一个分片服务器 三台服务器都需要配置 ``` vi /usr/local/mongodb/conf/shard1.conf ``` ``` #配置内容 pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid dbpath = /usr/local/mongodb/shard1/data logpath = /usr/local/mongodb/shard1/log/shard1.log logappend = true bind_ip = 0.0.0.0 port = 27001 fork = true #副本集名称 replSet = shard1 #declare this is a shard db of a cluster; shardsvr = true #设置最大连接数 maxConns = 20000 ``` 启动三台服务器的shard1 server ``` mongod -f /usr/local/mongodb/conf/shard1.conf ``` 登录任意一台服务器,初始化副本集(除了192.168.86.133),**因为仲裁服务器没有访问权限,会初始化失败。** 连接mongodb ``` mongo --port 27001 ``` ``` # 使用admin数据库,定义副本集配置 > use admin > config = { _id : "shard1", members : [ {_id : 0, host : "192.168.86.131:27001" },          {_id : 1, host : "192.168.86.132:27001" },          {_id : 2, host : "192.168.86.133:27001" , arbiterOnly: true } ] } #初始化副本集配置 rs.initiate(config) ``` 响应内容如下 ``` > use admin switched to db admin > config = { ...     _id : "shard1", ...      members : [ ...          {_id : 0, host : "192.168.86.131:27001" }, ...          {_id : 1, host : "192.168.86.132:27001" }, ...          {_id : 2, host : "192.168.86.133:27001" , arbiterOnly: true } ...      ] ...  } { "_id" : "shard1", "members" : [         { "_id" : 0, "host" : "192.168.86.131:27001"         },         { "_id" : 1, "host" : "192.168.86.132:27001"         },         { "_id" : 2, "host" : "192.168.86.133:27001", "arbiterOnly" : true         }     ] } > rs.initiate(config) { "ok" : 1 } ``` ### 7.2.设置第二和第三个分片服务器 第二、第三个服务器配置过程同上,只需将配置文件中的服务器名称改为shard2、shard3,将端口改为27002,27003即可,仲裁服务器分别选择第二个和第三个。 ## 8.配置路由服务器 mongos ### 8.1.配置并初始化 三台服务器都需要操作 先启动配置服务器和分片服务器,后启动路由实例 ``` vi /usr/local/mongodb/conf/mongos.conf ``` ``` pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid logpath = /usr/local/mongodb/mongos/log/mongos.log logappend = true bind_ip = 0.0.0.0 port = 20000 fork = true #监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字 configdb = configs/192.168.86.131:21000,192.168.86.132:21000,192.168.86.133:21000 #设置最大连接数 maxConns = 20000 ``` 启动三台服务器的mongos server ``` mongos -f /usr/local/mongodb/conf/mongos.conf ``` ### 8.2.串联路由服务器 目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。 登录任意一台mongos ``` mongo --port 20000 # 使用admin数据库 use admin # 串联路由服务器与分配副本集 sh.addShard("shard1/192.168.86.131:27001,192.168.86.132:27001,192.168.86.123:27001"); sh.addShard("shard2/192.168.86.131:27002,192.168.86.132:27002,192.168.86.133:27002"); sh.addShard("shard3/192.168.86.131:27003,192.168.86.132:27003,192.168.86.133:27003"); # 查看集群状态 sh.status() # 响应内容如下 mongos> sh.status() --- Sharding Status ---   sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5a713a37d56e076f3eb47acf")   }   shards:         {  "_id" : "shard1",  "host" : "shard1/192.168.86.131:27001,192.168.86.132:27001",  "state" : 1 }         {  "_id" : "shard2",  "host" : "shard2/192.168.86.132:27002,192.168.86.133:27002",  "state" : 1 }         {  "_id" : "shard3",  "host" : "shard3/192.168.86.131:27003,192.168.86.133:27003",  "state" : 1 }   active mongoses: "4.0.9" : 3   autosplit:         Currently enabled: yes   balancer:         Currently enabled:  yes         Currently running:  no         Failed balancer rounds in last 5 attempts:  0         Migration Results for the last 24 hours:                 No recent migrations   databases:         {  "_id" : "config",  "primary" : "config",  "partitioned" : true } mongos> ``` ## 9.启用集合分片 目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。 登录任意一台mongos ``` mongo --port 20000 # 使用admin数据库 use admin ``` 指定testdb分片生效,如下图: ![](https://img.kancloud.cn/9e/f0/9ef0f96f4680f4f62c064e255b7d427a_382x130.png) 指定数据库里需要分片的集合和片键,哈希id分片(注意:分片的字段数据应该是变化的,不然分片不成功),如下图: ![](https://img.kancloud.cn/2a/0b/2a0b7fe47fd894ee517e0e19d2509bd2_638x152.png) ``` #切换到testdb数据库 use testdb; # 插入测试数据 for(i=1;i<=100000;i++){db.table1.insert({"id":i,"name":"sunlei"+i})}; # 查看总条数 db.table1.aggregate([{$group : {_id : "$name", total : {$sum : 1}}}]) # 查看分片情况 db.table1.stats() ``` 结论:数据基本均匀