db

MongoDB分片集群搭建

2021-06-25  本文已影响0人  小屁孩cmq

1,服务器环境准备

进程 IP地址 端口
config server 配置服务器1(primary) 139.196.240.226 28001
config server 配置服务器2(secondary) 8.133.184.34 28001
config server 配置服务器3(secondary) 139.224.244.2 28001
shard server1 主节点 139.196.240.226 28011
shard server1 仲裁 8.133.184.34 28011
shard server1 副节点 139.224.244.2 28011
shard server2 主节点 139.196.240.226 28012
shard server2 仲裁 8.133.184.34 28012
shard server2 副节点 139.224.244.2 28012
shard server3 主节点 139.196.240.226 28013
shard server3 仲裁 8.133.184.34 28013
shard server3 副节点 139.224.244.2 28013
路由服务器 139.196.240.226 28000

2,配置config server服务器

cd /var/lib/mongo
mkdir cert

随便进入一台机器,通过openssl命令生成通信key,key文件名字为:mongokey,如下:

cd /
openssl rand -base64 745 > /var/lib/mongo/cert/mongokey
cd /var/lib/mongo/cert/
ls
# 必须将mongokey文件赋权限
chmod -R 600 mongokey
systemLog:
   destination: file
   path: /usr/local/monogodb/config/log/logs
   logAppend: true

storage:
   dbPath: /usr/local/mongodb/config/data
   journal:
      enabled: true

processManagement:
   fork: true
   pidFilePath: /usr/local/mongodb/config/log/mongod.pid
   timeZoneInfo: /usr/share/zoneinfo

net:
   bindIp: 0.0.0.0
   port: 28001

security:
   #链接认证,也就是账号密码
   authorization: enabled
   #集群内部通信安全证书文件,每台的证书必须一致
   keyFile: /usr/local/mongodb/cert/mongokey   

sharding:
   clusterRole: configsvr

replication:
   replSetName: configs
cd /usr/local/mongodb/config/mongdb-4.4.2/bin
./mongod -f /usr/local/mongodb/config/conf/config.conf --fork
cd /usr/local/mongodb/config/mongdb-4.4.2/bin
./mongo --port 28001
> use admin

> config={_id:'configs', 
 configsvr:true,
    members : [
  {_id : 0, host : '139.224.244.2:28001' },
  {_id : 1, host : '8.133.184.34:28001'},
  {_id : 2, host : '139.196.240.226:28001' }
  ]}
  
> rs.initiate(config)  


image.png

3,配置分片副本集

操作如上。

systemLog:
   destination: file
   path: /usr/local/monogodb/shard1/log/logs
   logAppend: true

storage:
   dbPath: /usr/local/mongodb/shard1/data
   journal:
      enabled: true

processManagement:
   fork: true
   pidFilePath: /usr/local/mongodb/shard1/log/mongod.pid
   timeZoneInfo: /usr/share/zoneinfo

net:
   bindIp: 0.0.0.0
   port: 28011

security:
   #链接认证,也就是账号密码
   authorization: enabled
   #集群内部通信安全证书文件,每台的证书必须一致
   keyFile: /usr/local/mongodb/cert/mongokey   

sharding:
   clusterRole: shardsvr

replication:
   replSetName: shard1
cd /usr/local/mongodb/shard1/mongdb-4.4.2/bin
./mongod -f /usr/local/mongodb/shard1/conf/config.conf --fork
cd /usr/local/mongodb/shard1/mongdb-4.4.2/bin
./mongo --port 28011
> use admin

> config={_id:'shard1',     
    members : [
  {_id : 0, host : '139.224.244.2:28011' },
  {_id : 1, host : '8.133.184.34:28011'},
  {_id : 2, host : '139.196.240.226:28011' }
  ]}
  
> rs.initiate(config)  


4,配置路由服务器mongos

cd /usr/local
mkdir mongodb
cd mongodb
mkdir mongos

安装mongoDB到config目录下,安装步骤参考MongoDB安装手册

操作如上。

systemLog:
   destination: file
   path: /usr/local/mongodb/mongos/log/logs
   logAppend: true

processManagement:
   fork: true
   pidFilePath: /usr/local/mongodb/mongos/log/mongod.pid
   timeZoneInfo: /usr/share/zoneinfo

net:
   bindIp: 0.0.0.0
   port: 28000
   
security:
   #集群内部通信安全证书文件,每台的证书必须一致
   keyFile: /usr/local/mongodb/cert/mongokey     

sharding:
   configDB: configs/139.224.244.2:28001,8.133.184.34:28001,139.19s6.240.22:28001

> use admin

> db.createUser(
{
user:"username",
pwd:"password",
roles:[{role:"root",db:"admin"}]
}
)
cd /usr/local/mongodb/mongos/mongdb-4.4.2/bin
./mongos -f /usr/local/mongodb/mongos/conf/config.conf --fork

登录mongos路由服务器

mongo --port 28000
#使用admin数据库
use admin
#串联路由服务器与分配副本集
sh.addShard("shard1/139.224.244.2:28011,8.133.184.34:28011,139.196.240.226:28011")
sh.addShard("shard2/139.224.244.2:28012,8.133.184.34:28012,139.196.240.226:28012")
sh.addShard("shard3/139.224.244.2:28013,8.133.184.34:28013,139.196.240.226:28013")
image.png

5,测试分片

语法:( { enablesharding : "数据库名称" } )

mongos> db.runCommand( { enablesharding : "test" } )
mongos> use admin
# 根据指定索引分片
mongos> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
or
# 根据hash分片
mongos> db.runCommand( { shardcollection : "test.vast",key : {id: "hashed“} } )
mongos> use test
mongos> for(i=0;i<20000;i++){ db.vast.insert({"id":i,"name":"clsn","age":70,"date":new Date()}); }
mongos> db.vast.stats()

排除掉不重要的信息的测试结果


image.png
上一篇下一篇

猜你喜欢

热点阅读