# Module4Part3 **Repository Path**: john_hi/module4-part3 ## Basic Information - **Project Name**: Module4Part3 - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-02-25 - **Last Updated**: 2021-02-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 工程简介 1.配置节点 配置文件config-17011.conf: ``` # 数据库文件位置 dbpath=/Users/yuan/server/mongo/config/17011 # 日志文件位置 logpath=/Users/yuan/server/mongo/config/logs/17011.log # 追加方式写入日志 logappend=true # fork守护进程 fork=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=17011 # 标志为配置服务器 configsvr=true # 配置服务器副本集名称 replSet=configsvr ``` 配置文件config-17013.conf: ``` # 数据库文件位置 dbpath=/Users/yuan/server/mongo/config/17013 # 日志文件位置 logpath=/Users/yuan/server/mongo/config/logs/17013.log # 追加方式写入日志 logappend=true # fork守护进程 fork=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=17013 # 标志为配置服务器 configsvr=true # 配置服务器副本集名称 replSet=configsvr ``` 配置文件config-17015.conf: ``` # 数据库文件位置 dbpath=/Users/yuan/server/mongo/config/17015 # 日志文件位置 logpath=/Users/yuan/server/mongo/config/logs.log # 追加方式写入日志 logappend=true # fork守护进程 fork=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=17015 # 标志为配置服务器 configsvr=true # 配置服务器副本集名称 replSet=configsvr ``` 启动配置节点 ``` ./mongod -f /Users/yuan/server/mongo/config/config-17011.conf ./mongod -f /Users/yuan/server/mongo/config/config-17013.conf ./mongod -f /Users/yuan/server/mongo/config/config-17015.conf ``` 配置集群 ``` ./mongo --port 17011 use admin var cfg = {"_id":"configsvr", "members":[ {"_id":1,"host":"127.0.0.1:17011"}, {"_id":2,"host":"127.0.0.1:17013"}, {"_id":3,"host":"127.0.0.1:17015"}, ] }; rs.initiate(cfg) rs.status(); ``` 2.配置分片节点 shard1: shard1-37011.conf ``` # 数据库目录 dbpath=/Users/yuan/server/mongo/shard/shard1/37011 # 日志文件存放目录 logpath=/Users/yuan/server/mongo/shard/logs/37011.log # 追加方式写入日志 logappend=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=37011 # 后台守护进程启动 fork=true # 复制集名称 replSet=shard1 # 指定为shardsvr shardsvr=true ``` shard1-37013.conf ``` # 数据库目录 dbpath=/Users/yuan/server/mongo/shard/shard1/37013 # 日志文件存放目录 logpath=/Users/yuan/server/mongo/shard/logs/37013.log # 追加方式写入日志 logappend=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=37013 # 后台守护进程启动 fork=true # 复制集名称 replSet=shard1 # 指定为shardsvr shardsvr=true ``` shard1-37015.conf ``` # 数据库目录 dbpath=/Users/yuan/server/mongo/shard/shard1/37015 # 日志文件存放目录 logpath=/Users/yuan/server/mongo/shard/logs/37015.log # 追加方式写入日志 logappend=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=37015 # 后台守护进程启动 fork=true # 复制集名称 replSet=shard1 # 指定为shardsvr shardsvr=true ``` shard1-37017.conf ``` # 数据库目录 dbpath=/Users/yuan/server/mongo/shard/shard1/37017 # 日志文件存放目录 logpath=/Users/yuan/server/mongo/shard/logs/37017.log # 追加方式写入日志 logappend=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=37017 # 后台守护进程启动 fork=true # 复制集名称 replSet=shard1 # 指定为shardsvr shardsvr=true ``` 启动 ``` ./mongod -f /Users/yuan/server/mongo/shard/shard1/shard1-37011.conf ./mongod -f /Users/yuan/server/mongo/shard/shard1/shard1-37013.conf ./mongod -f /Users/yuan/server/mongo/shard/shard1/shard1-37015.conf ./mongod -f /Users/yuan/server/mongo/shard/shard1/shard1-37017.conf ``` 配置复制集 ``` ./bin/mongo -port 37011 use admin var cfg = {"_id":"shard1", "protocolVersion":1, "members":[ {"_id":1,"host":"127.0.0.1:37011","priority":10}, {"_id":2,"host":"127.0.0.1:37013","priority":5}, {"_id":3,"host":"127.0.0.1:37015","priority":0}, {"_id":4,"host":"127.0.0.1:37017","arbiterOnly":true} ] }; rs.initiate(cfg) rs.status() ``` shard2: shard-47011.conf ``` # 数据库目录 dbpath=/Users/yuan/server/mongo/shard/shard2/47011 # 日志文件存放目录 logpath=/Users/yuan/server/mongo/shard/logs/47011.log # 追加方式写入日志 logappend=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=47011 # 后台守护进程启动 fork=true # 复制集名称 replSet=shard2 # 指定为shardsvr shardsvr=true ``` shard-37013.conf ``` # 数据库目录 dbpath=/Users/yuan/server/mongo/shard/shard2/47013 # 日志文件存放目录 logpath=/Users/yuan/server/mongo/shard/logs/47013.log # 追加方式写入日志 logappend=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=47013 # 后台守护进程启动 fork=true # 复制集名称 replSet=shard2 # 指定为shardsvr shardsvr=true ``` shard-47015.conf ``` # 数据库目录 dbpath=/Users/yuan/server/mongo/shard/shard2/47015 # 日志文件存放目录 logpath=/Users/yuan/server/mongo/shard/logs/47015.log # 追加方式写入日志 logappend=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=47015 # 后台守护进程启动 fork=true # 复制集名称 replSet=shard2 # 指定为shardsvr shardsvr=true ``` shard-47017.conf ``` # 数据库目录 dbpath=/Users/yuan/server/mongo/shard/shard2/47017 # 日志文件存放目录 logpath=/Users/yuan/server/mongo/shard/logs/47017.log # 追加方式写入日志 logappend=true # 绑定IP bind_ip=0.0.0.0 # 端口 port=47017 # 后台守护进程启动 fork=true # 复制集名称 replSet=shard2 # 指定为shardsvr shardsvr=true ``` 启动 ``` ./mongod -f /Users/yuan/server/mongo/shard/shard2/shard-47011.conf ./mongod -f /Users/yuan/server/mongo/shard/shard2/shard-47013.conf ./mongod -f /Users/yuan/server/mongo/shard/shard2/shard-47015.conf ./mongod -f /Users/yuan/server/mongo/shard/shard2/shard-47017.conf ``` 配置复制集 ``` ./bin/mongo -port 47011 use admin var cfg = {"_id":"shard2", "protocolVersion":1, "members":[ {"_id":1,"host":"127.0.0.1:47011","priority":10}, {"_id":2,"host":"127.0.0.1:47013","priority":5}, {"_id":3,"host":"127.0.0.1:47015","priority":0}, {"_id":4,"host":"127.0.0.1:47017","arbiterOnly":true} ] }; rs.initiate(cfg) rs.status() ``` 3.配置路由节点 ``` # 端口 port=27017 # 绑定IP bind_ip=0.0.0.0 # fork fork=true # 日志文件 logpath=/Users/yuan/server/mongo/route/27017.log # 追加方式记录日志 logappend=true # 配置集群 configdb=configsvr/127.0.0.1:17011,127.0.0.1:17013,127.0.0.1:17015 ``` 启动 ``` ./mongos -f /Users/yuan/server/mongo/route/27017.conf ``` 添加分片 ``` ./mongo --port 27017 sh.addShard("shard1/127.0.0.1:37011,127.0.0.1:37013,127.0.0.1:37015,127.0.0.1:37017"); sh.addShard("shard2/127.0.0.1:47011,127.0.0.1:47013,127.0.0.1:47015,127.0.0.1:47017"); ``` 设置分片 进入路由节点,执行下面操作 ``` sh.enableSharding("lg_resume"); #指定集合开启分片功能 sh.shardCollection("lg_resume.lg_resume_datas",{"resumeid":"hashed"}) db.lg_resume_datas.insert({resumeid:1,name:'zhangsan',city:'cs',salary:18000}); db.lg_resume_datas.insert({resumeid:2,name:'lisi',city:'bj',salary:10000}); db.lg_resume_datas.insert({resumeid:3,name:'wanger',city:'sh',salary:22000}); ```