# lagou_mongo **Repository Path**: dingjunjun_codeSpace/lagou_mongo ## Basic Information - **Project Name**: lagou_mongo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-10-24 - **Last Updated**: 2022-10-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #### 服务器端口分配 视频演示地址: http://nas.ders.top:5000/sharing/iwufZhWqF | 端口 | 目录名称 | 用途 | | ----- | ----------------------------------------------- | ------------------- | | | /usr/local/mongo-shard | 主目录 | | 17011 | /usr/local/mongo-shard/config/config-17011.conf | 配置节点-17011 | | 17013 | /usr/local/mongo-shard/config/config-17013.conf | 配置节点-17013 | | 17015 | /usr/local/mongo-shard/config/config-17015.conf | 配置节点-17015 | | | /usr/local/mongo-shard/config/logs | 配置节点日志 | | 27017 | /usr/local/mongo-shard/route/route-27017.conf | 路由节点-27017 | | | /usr/local/mongo-shard/route/logs | 路由日志 | | 37011 | /usr/local/mongo-shard/shard1/mongo-37011.conf | 分片1节点1-37011 | | 37013 | /usr/local/mongo-shard/shard1/mongo-37013.conf | 分片1节点2-37013 | | 37015 | /usr/local/mongo-shard/shard1/mongo-37015.conf | 分片1节点3-37015 | | 37017 | /usr/local/mongo-shard/shard1/mongo-37017.conf | 分片1仲裁节点-37017 | | | /usr/local/mongo-shard/shard1/logs | 分片1日志 | | 47011 | /usr/local/mongo-shard/shard2/mongo-47011.conf | 分片2节点1-47011 | | 47013 | /usr/local/mongo-shard/shard2/mongo-47013.conf | 分片2节点2-47013 | | 47015 | /usr/local/mongo-shard/shard2/mongo-47015.conf | 分片2节点3-47015 | | 47017 | /usr/local/mongo-shard/shard2/mongo-47017.conf | 分片2仲裁节点-47017 | | | /usr/local/mongo-shard/shard2/logs | 分片2日志 | | 57011 | /usr/local/mongo-shard/shard3/mongo-57011.conf | 分片3节点1-57011 | | 57013 | /usr/local/mongo-shard/shard3/mongo-57013.conf | 分片3节点2-57013 | | 57015 | /usr/local/mongo-shard/shard3/mongo-57015.conf | 分片3节点3-57015 | | 57017 | /usr/local/mongo-shard/shard3/mongo-57017.conf | 分片3仲裁节点-57017 | | | /usr/local/mongo-shard/shard3/logs | 分片3日志 | | 58011 | /usr/local/mongo-shard/shard4/mongo-58011.conf | 分片4节点1-58011 | | 58013 | /usr/local/mongo-shard/shard4/mongo-58013.conf | 分片4节点2-58013 | | 58015 | /usr/local/mongo-shard/shard4/mongo-58015.conf | 分片4节点3-58015 | | 58017 | /usr/local/mongo-shard/shard4/mongo-58017.conf | 分片4仲裁节点-58017 | | | /usr/local/mongo-shard/shard4/logs | 分片4日志 | #### 安装MongoDB ```shell wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-4.1.3.tgz tar -xvzf mongodb-linux-x86_64-4.1.3.tgz ``` #### 配置节点设置 ```shell # 创建 配置节点数据文件夹 mkdir -p config/config1 mkdir -p config/config2 mkdir -p config/config3 # 创建 配置节点日志文件夹 mkdir -p config/logs # 配置节点 config-17011.conf dbpath=config/config1 logpath=config/logs/config1.log logappend=true fork=true bind_ip=0.0.0.0 port=17011 configsvr=true replSet=configsvr # 配置节点 config-17013.conf dbpath=config/config2 logpath=config/logs/config2.log logappend=true fork=true bind_ip=0.0.0.0 port=17013 configsvr=true replSet=configsvr # 配置节点 config-17015.conf dbpath=config/config3 logpath=config/logs/config3.log logappend=true fork=true bind_ip=0.0.0.0 port=17015 configsvr=true replSet=configsvr # 启动配置节点 ./bin/mongod -f config/config-17011.conf ./bin/mongod -f config/config-17013.conf ./bin/mongod -f config/config-17015.conf ./bin/mongo --port 17011 use admin var cfg = {"_id":"configsvr", "members":[ {"_id":1,"host":"10.10.10.205:17011"}, {"_id":2,"host":"10.10.10.205:17013"}, {"_id":3,"host":"10.10.10.205:17015"}, ] }; rs.initiate(cfg) ``` #### 分片1节点设置 ```shell # 创建 分片1节点数据文件夹 mkdir -p shard1/shard1-37011 mkdir -p shard1/shard1-37013 mkdir -p shard1/shard1-37015 mkdir -p shard1/shard1-37017 # 创建 分片1节点日志文件夹 mkdir -p shard1/logs # 分片1节点1 mongo-37011.conf dbpath=shard1/shard1-37011 bind_ip=0.0.0.0 port=37011 fork=true logpath=shard1/logs/shard1-37011.log replSet=shard1 shardsvr=true # 分片1节点2 mongo-37013.conf dbpath=shard1/shard1-37013 bind_ip=0.0.0.0 port=37013 fork=true logpath=shard1/logs/shard1-37013.log replSet=shard1 shardsvr=true # 分片1节点3 mongo-37015.conf dbpath=shard1/shard1-37015 bind_ip=0.0.0.0 port=37015 fork=true logpath=shard1/logs/shard1-37015.log replSet=shard1 shardsvr=true # 分片1节点4(仲裁节点) mongo-37017.conf dbpath=shard1/shard1-37017 bind_ip=0.0.0.0 port=37011 fork=true logpath=shard1/logs/shard1-37017.log replSet=shard1 shardsvr=true # 启动分片1节点 ./bin/mongod -f shard1/mongo-37011.conf ./bin/mongod -f shard1/mongo-37013.conf ./bin/mongod -f shard1/mongo-37015.conf ./bin/mongod -f shard1/mongo-37017.conf ./bin/mongo --port 37011 use admin var cfg = {"_id":"shard1", "members":[ {"_id":1,"host":"10.10.10.205:37011"}, {"_id":2,"host":"10.10.10.205:37013"}, {"_id":3,"host":"10.10.10.205:37015"}, {"_id":3,"host":"10.10.10.205:37017, "arbiterOnly":true"}, ] }; rs.initiate(cfg) ``` #### 分片2节点设置 ```shell # 创建 分片2节点数据文件夹 mkdir -p shard2/shard2-47011 mkdir -p shard2/shard2-47013 mkdir -p shard2/shard2-47015 mkdir -p shard2/shard2-47017 # 创建 分片2节点日志文件夹 mkdir -p shard2/logs # 分片2节点1 mongo-47011.conf dbpath=shard2/shard2-47011 bind_ip=0.0.0.0 port=47011 fork=true logpath=shard2/logs/shard2-47011.log replSet=shard2 shardsvr=true # 分片2节点2 mongo-47013.conf dbpath=shard2/shard2-47013 bind_ip=0.0.0.0 port=47013 fork=true logpath=shard2/logs/shard2-47013.log replSet=shard2 shardsvr=true # 分片2节点3 mongo-47015.conf dbpath=shard2/shard2-47015 bind_ip=0.0.0.0 port=47015 fork=true logpath=shard2/logs/shard2-47015.log replSet=shard2 shardsvr=true # 分片2节点4(仲裁节点) mongo-47017.conf dbpath=shard2/shard2-47017 bind_ip=0.0.0.0 port=47011 fork=true logpath=shard2/logs/shard2-47017.log replSet=shard2 shardsvr=true # 启动分片2节点 ./bin/mongod -f shard2/mongo-47011.conf ./bin/mongod -f shard2/mongo-47013.conf ./bin/mongod -f shard2/mongo-47015.conf ./bin/mongod -f shard2/mongo-47017.conf ./bin/mongo --port 47011 use admin var cfg = {"_id":"shard2", "members":[ {"_id":1,"host":"10.10.10.205:47011"}, {"_id":2,"host":"10.10.10.205:47013"}, {"_id":3,"host":"10.10.10.205:47015"}, {"_id":3,"host":"10.10.10.205:47017", "arbiterOnly":true"}, ] }; rs.initiate(cfg) ``` #### 分片3节点设置 ```shell # 创建 分片3节点数据文件夹 mkdir -p shard3/shard3-57011 mkdir -p shard3/shard3-57013 mkdir -p shard3/shard3-57015 mkdir -p shard3/shard3-57017 # 创建 分片3节点日志文件夹 mkdir -p shard3/logs # 分片3节点1 mongo-57011.conf dbpath=shard3/shard3-57011 bind_ip=0.0.0.0 port=57011 fork=true logpath=shard3/logs/shard3-57011.log replSet=shard3 shardsvr=true # 分片3节点2 mongo-57013.conf dbpath=shard3/shard3-57013 bind_ip=0.0.0.0 port=57013 fork=true logpath=shard3/logs/shard3-57013.log replSet=shard3 shardsvr=true # 分片3节点3 mongo-57015.conf dbpath=shard3/shard3-57015 bind_ip=0.0.0.0 port=57015 fork=true logpath=shard3/logs/shard3-57015.log replSet=shard3 shardsvr=true # 分片3节点4(仲裁节点) mongo-57017.conf dbpath=shard3/shard3-57017 bind_ip=0.0.0.0 port=57011 fork=true logpath=shard3/logs/shard3-57017.log replSet=shard3 shardsvr=true # 启动分片3节点 ./bin/mongod -f shard3/mongo-57011.conf ./bin/mongod -f shard3/mongo-57013.conf ./bin/mongod -f shard3/mongo-57015.conf ./bin/mongod -f shard3/mongo-57017.conf ./bin/mongo --port 57011 use admin var cfg = {"_id":"shard3", "members":[ {"_id":1,"host":"10.10.10.205:57011"}, {"_id":2,"host":"10.10.10.205:57013"}, {"_id":3,"host":"10.10.10.205:57015"}, {"_id":3,"host":"10.10.10.205:57017", "arbiterOnly":true"}, ] }; rs.initiate(cfg) ``` #### 分片4节点设置 ```shell # 创建 分片4节点数据文件夹 mkdir -p shard4/shard4-58011 mkdir -p shard4/shard4-58013 mkdir -p shard4/shard4-58015 mkdir -p shard4/shard4-58017 # 创建 分片4节点日志文件夹 mkdir -p shard4/logs # 分片4节点1 mongo-58011.conf dbpath=shard4/shard4-58011 bind_ip=0.0.0.0 port=58011 fork=true logpath=shard4/logs/shard4-58011.log replSet=shard4 shardsvr=true # 分片4节点2 mongo-58013.conf dbpath=shard4/shard4-58013 bind_ip=0.0.0.0 port=58013 fork=true logpath=shard4/logs/shard4-58013.log replSet=shard4 shardsvr=true # 分片4节点3 mongo-58015.conf dbpath=shard4/shard4-58015 bind_ip=0.0.0.0 port=58015 fork=true logpath=shard4/logs/shard4-58015.log replSet=shard4 shardsvr=true # 分片4节点4(仲裁节点) mongo-58017.conf dbpath=shard4/shard4-58017 bind_ip=0.0.0.0 port=58011 fork=true logpath=shard4/logs/shard4-58017.log replSet=shard4 shardsvr=true # 启动分片4节点 ./bin/mongod -f shard4/mongo-58011.conf ./bin/mongod -f shard4/mongo-58013.conf ./bin/mongod -f shard4/mongo-58015.conf ./bin/mongod -f shard4/mongo-58017.conf ./bin/mongo --port 58011 use admin var cfg = {"_id":"shard4", "members":[ {"_id":1,"host":"10.10.10.205:58011"}, {"_id":2,"host":"10.10.10.205:58013"}, {"_id":3,"host":"10.10.10.205:58015"}, {"_id":3,"host":"10.10.10.205:58017", "arbiterOnly":true"}, ] }; rs.initiate(cfg) ``` #### 路由节点设置 ```shell # 创建 路由节点日志文件夹 mkdir -p route/logs # 路由节点 27017 port=27017 bind_ip=0.0.0.0 fork=true logpath=route/logs/route.log configdb=configsvr/10.10.10.205:17011,10.10.10.205:17013,10.10.10.205:17015 ``` #### 路由节点添加分片 ```shell ./bin/mongo --port 27017 # 分片一 sh.addShard("shard1/10.10.10.205:37011,10.10.10.205:37013,10.10.10.205:37015"); # 分片二 sh.addShard("shard1/10.10.10.205:47011,10.10.10.205:47013,10.10.10.205:47015"); # 分片三 sh.addShard("shard1/10.10.10.205:57011,10.10.10.205:57013,10.10.10.205:57015"); # 分片四 sh.addShard("shard1/10.10.10.205:58011,10.10.10.205:58013,10.10.10.205:58015"); # 创建数据库 use lg_resume; # 创建集合 db.createCollection("lagou_resume_datas") # 为数据库开启分片功能 sh.enableSharding("lg_resume") # 为指定集合开启分片功能 sh.shardCollection("lg_resume.lagou_resume_datas",{"id": hashed}) ``` #### 安全认证配置 ```shell ./bin/mongo --port 27017 # 创建管理员账号 use admin db.createUser( {user:"root", pwd:"root", roles:[{role:"root",db:"admin"}] }) # 创建 lagou_gx 账号 use lg_resume db.createUser( {user:"lagou_gx", pwd:"abc321", roles:[{role:"readWrite",db:"lg_resume"}] }) # 关闭mongo #安装psmisc yum install psmisc #安装完之后可以使用killall 命令 快速关闭多个进程 killall mongod killall mongos # 生成密钥文件 openssl rand -base64 756 > data/mongodb/mongoKeyFile.file # 修改权限 chmod 600 data/mongodb/mongoKeyFile.file # 配置节点集群和分片节点集群 conf文件追加 auth=true keyFile=data/mongodb/mongoKeyFile.file # 路由配置节点 conf文件追加 keyFile=data/mongodb/mongoKeyFile.file ``` #### 启动分片 ```shell ./bin/mongod -f config/config-17011.conf ./bin/mongod -f config/config-17013.conf ./bin/mongod -f config/config-17015.conf ./bin/mongod -f shard1/mongo-37011.conf ./bin/mongod -f shard1/mongo-37013.conf ./bin/mongod -f shard1/mongo-37015.conf ./bin/mongod -f shard1/mongo-37017.conf ./bin/mongod -f shard2/mongo-47011.conf ./bin/mongod -f shard2/mongo-47013.conf ./bin/mongod -f shard2/mongo-47015.conf ./bin/mongod -f shard2/mongo-47017.conf ./bin/mongod -f shard3/mongo-57011.conf ./bin/mongod -f shard3/mongo-57013.conf ./bin/mongod -f shard3/mongo-57015.conf ./bin/mongod -f shard3/mongo-57017.conf ./bin/mongod -f shard4/mongo-58011.conf ./bin/mongod -f shard4/mongo-58013.conf ./bin/mongod -f shard4/mongo-58015.conf ./bin/mongod -f shard4/mongo-58017.conf ./bin/mongos -f route/route-27017.conf ```