1 环境准备、集群模型
1.1 准备环境
1 2 3 | Mongo01:10.10.73.142 Mongo02:10.10.73.147 Mongo03:10.10.73.148 |
1.2 集群模型
1 2 3 4 5 | Mongos: 20000/tcp Config: 21000/tcp Shard1: 22001/tcp Shard2: 22002/tcp Shard3: 22003/tcp |
1 2 3 4 5 6 7 | node1 node2 node3 ip 10.10.73.142 10.10.73.147 10.10.73.148 mongos 20000 20000 20000 configsvr 21000 21000 21000 shard1 22001[主节点master] 22001[副本slave] 22001[仲裁arbitrate] shard2 22002[仲裁arbitrate] 22002[主节点master] 22002[副本slave] shard3 22003[副本slave] 22003[仲裁arbitrate] 22003[主节点master] |
1.3 下载mongo
mongo 版本(根据服务器选择版本),选择版本mongodb-linux-x86_64-rhel62-3.2.7.tgz
下载地址https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.7.tgz
1.4 创建安装目录
每台服务器执行
1 | mkdir –p /opt/mongo/ |
1.5 创建服务目录
每台服务器执行
1 2 3 | mkdir –p /data/mongodb/mongos/ log mkdir –p /data/mongodb/config/{data, log } mkdir –p /data/mongodb/shard{1,2,3}/{data, log } |
2 安装配置mongo
2.1 10.10.73.142服务器安装mongo
1、上传mongodb-linux-x86_64-rhel62-3.2.7.tgz到/opt/mongo路径下
1 | cd /opt/mongo |
2、解压mongodb-linux-x86_64-rhel62-3.2.7.tgz
1 | tar –zxvf mongodb-linux-x86_64-rhel62-3.2.7.tgz |
2.2 10.10.73.142创建服务器对应配置文件
1、创建conf路径
1 | mkdir –p /opt/mongo/mongodb-linux-x86_64-rhel62-3.2.7/conf |
2、配置config.conf、mongos.conf、shard1.conf、shard2.conf、shard3.conf
#####################config.conf#####################
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | systemLog: destination: file ###日志存储位置 path: /data/mongodb/config/ log /config. log logAppend: true storage: ##journal配置 journal: enabled: true ##数据文件存储位置 dbPath: /data/mongodb/config/data/ ##是否一个库一个文件夹 directoryPerDB: true ##数据引擎 engine: wiredTiger ##WT引擎配置 wiredTiger: engineConfig: ##WT最大使用cache(根据服务器实际情况调节) cacheSizeGB: 8 ##是否将索引也按数据库名单独存储 directoryForIndexes: true ##表压缩配置 collectionConfig: blockCompressor: zlib ##索引配置 indexConfig: prefixCompression: true ##端口配置 net: port: 21000 ##分片配置 sharding: ##分片角色 clusterRole: configsvr |
#####################mongos.conf#####################
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | ##日志配置 systemLog: destination: file ##日志位置 path: /data/mongodb/mongos/ log /mongos. log logAppend: true ##网路配置 net: ##端口配置 port: 20000 ##分片配置 sharding: ##指定config server configDB: 10.10.73.142:21000,10.10.73.147:21000,10.10.73.148:21000 |
#####################shard1.conf#####################
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | systemLog: destination: file ###日志存储位置 path: /data/mongodb/shard1/ log /shard1. log logAppend: true storage: ##journal配置 journal: enabled: true ##数据文件存储位置 dbPath: /data/mongodb/shard1/data/ ##是否一个库一个文件夹 directoryPerDB: true ##数据引擎 engine: wiredTiger ##WT引擎配置 wiredTiger: engineConfig: ##WT最大使用cache(根据服务器实际情况调节) cacheSizeGB: 8 ##是否将索引也按数据库名单独存储 directoryForIndexes: true ##表压缩配置 collectionConfig: blockCompressor: zlib ##索引配置 indexConfig: prefixCompression: true ##端口配置 net: port: 22001 replication: ##oplog大小 oplogSizeMB: 40960 ##复制集名称 replSetName: shard1 ##分片配置 sharding: ##分片角色 clusterRole: shardsvr |
#####################shard2.conf#####################
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | systemLog: destination: file ###日志存储位置 path: /data/mongodb/shard2/ log /shard2. log logAppend: true storage: ##journal配置 journal: enabled: true ##数据文件存储位置 dbPath: /data/mongodb/shard2/data/ ##是否一个库一个文件夹 directoryPerDB: true ##数据引擎 engine: wiredTiger ##WT引擎配置 wiredTiger: engineConfig: ##WT最大使用cache cacheSizeGB: 8 ##是否将索引也按数据库名单独存储 directoryForIndexes: true ##表压缩配置 collectionConfig: blockCompressor: zlib ##索引配置 indexConfig: prefixCompression: true ##端口配置 net: port: 22002 replication: ##oplog大小 oplogSizeMB: 40960 ##复制集名称 replSetName: shard2 ##分片配置 sharding: ##分片角色 clusterRole: shardsvr |
#####################shard3.conf#####################
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | systemLog: destination: file ###日志存储位置 path: /data/mongodb/shard3/ log /shard3. log logAppend: true storage: ##journal配置 journal: enabled: true ##数据文件存储位置 dbPath: /data/mongodb/shard3/data/ ##是否一个库一个文件夹 directoryPerDB: true ##数据引擎 engine: wiredTiger ##WT引擎配置 wiredTiger: engineConfig: ##WT最大使用cache(根据服务器实际情况调节) cacheSizeGB: 8 ##是否将索引也按数据库名单独存储 directoryForIndexes: true ##表压缩配置 collectionConfig: blockCompressor: zlib ##索引配置 indexConfig: prefixCompression: true ##端口配置 net: port: 22003 replication: ##oplog大小 oplogSizeMB: 40960 ##复制集名称 replSetName: shard3 ##分片配置 sharding: ##分片角色 clusterRole: shardsvr |
2.3 10.10.73.147、10.10.73.148服务器安装配置
1、登录10.10.73.142服务器
1 | cd /opt/mongo |
2、scp至10.10.74.147服务器
1 | scp -r mongodb-linux-x86_64-rhel62-3.2.7 root@10.10.73.147:/opt/mongo/ |
3、scp至10.10.74.148服务器
1 | scp -r mongodb-linux-x86_64-rhel62-3.2.7 root@10.10.73.148:/opt/mongo/ |
3 启动相应服务
每台服务器依次执行如下操作
3.1 启动configserver 、mongos、shard
1、切换到/opt/mongo/mongodb-linux-x86_64-rhel62-3.2.7/bin
1 | cd /opt/mongo/mongodb-linux-x86_64-rhel62-3.2.7/bin |
2、启动configserver
1 | ./mongod -f ../conf/config.conf -fork |
3、启动mongos
1 | ./mongos -f ../conf/mongos.conf -fork |
4、启动shard1
1 | ./mongod -f ../conf/shard1.conf -fork |
5、启动shard2
1 | ./mongod -f ../conf/shard2.conf -fork |
6、启动shard3
1 | ./mongod -f ../conf/shard3.conf -fork |
3.2 初始化分片配置
3.2.1 Shard1
1 2 3 4 5 6 | ./mongo --port 22001 >use admin >config = {_id: "shard1" ,members:[{_id:0,host: "10.10.73.142:22001" , "priority" :9}, {_id:1,host: "10.10.73.147:22001" , "priority" :8},{_id:2,host: "10.10.73.148:22001" ,arbiterOnly: true }]} >rs.initiate(config); |
3.2.2 Shard2
1 2 3 4 5 6 | ./mongo --port 22002 >Use admin >config2 = {_id: "shard2" ,members:[{_id:0,host: "10.10.73.147:22002" },{_id:1,host: "10.10.73.148:22002" },{_id:2,host: "10.10.73.142:22002" ,arbiterOnly: true }]} >rs.initiate(config2); |
3.2.3 Shard3
1 2 3 4 5 6 | ./mongo --port 22003 >Use admin >config3 = {_id: "shard3" ,members:[{_id:0,host: "10.10.73.148:22003" },{_id:1,host: "10.10.73.142:22003" },{_id:2,host: "10.10.73.147:22003" ,arbiterOnly: true }]} >rs.initiate(config3); |
3.3 串联路由服务器与分配副本集
1 2 3 4 5 6 7 8 | ./mongo --port 20000 >use admin >db.runCommand({addshard: "shard1/10.10.73.142:22001,10.10.73.147:22001,10.10.73.148:22001" }); >db.runCommand({addshard: "shard2/10.10.73.147:22002,10.10.73.148:22002,10.10.73.142:22002" }); >db.runCommand({addshard: "shard3/10.10.73.148:22003,10.10.73.142:22003,10.10.73.147:22003" }); |
3.4 查看分片服务器的配置
1 2 3 4 | ./mongo --port 20000 >use admin >db.runCommand({listshards:1}); |
3.5 安装完成
4 插入测试
01 02 03 04 05 06 07 08 09 10 11 12 13 | ./mongo --port 20000 >use admin #创建数据库,指定数据库'testdb'进行分片生效 >db.runCommand( { enablesharding : "testdb" }); #指定数据库需要分片键片 >db.runCommand({shardcollection: "testdb.table1" ,key:{_id: 'hashed' }}) #创建Hash片键,指定'testdb'数据库中的'table1'表中的数据进行分片 #插入 > for (var i=1;i<10;i++) db.table1.save({ "a" :i+ "wwe" , "acId" : "45334" +i, "areaCode" : "3346" +i, "deliverd" : true , "destId" : "13346" +i, "doneTime" : "6334" +i, "dup" :0, "gwId" : "2" , "gwMsgId" : "6334" +i, "mobile" : "166673" , "msgType" : "UmsReportReq" +i, "operator" : "ChinaMobile" , "priority" :0, "reqId" : "1" , "revTime" : "245" +i, "smscSeq" :0, "srcReqId" : "06235" +i, "status" : "DELIVRD" , "subTime" : "26230" +i, "submitTime" : "1062348" +i, "traceNo" : "92363" +i}); #查看状态 >db.table1.stats(); #查看数据 > db.table1.find({}); |