mongodb分片集群部署

1.目录准备

mkdir -p /data/mongodb
mkdir -p /data/mongodb/{data,config,logs,apps}
mkdir -p /data/mongodb/data/shard{1}

/usr/sbin/groupadd -g 10001 mongodb
/usr/sbin/useradd -u 10001 -g mongodb mongodb


chown -R mongodb:mongodb /data/mongodb
chmod -R 775 /data/mongodb

2.配置服务器部署 

三个节点配置。
vi /data/mongodb/apps/mongodb/bin/mongodb-config.conf

systemLog:
  destination: file
#注意修改路径
#Note the modification path
  path: "/data/mongodb/logs/mongo_config.log"
  logAppend: true
operationProfiling:  
  mode: slowOp
  slowOpThresholdMs: 200  
  slowOpSampleRate: 1.0
storage:
  journal:
    enabled: true
#注意修改路径
#Note the modification path
  dbPath: "/data/mongodb/config"
  engine: wiredTiger
  wiredTiger:
    engineConfig:
         cacheSizeGB: 13
processManagement:
  fork: false
net:
  bindIp: 0.0.0.0
#注意修改端口
#Notice Modifying the port
  port: 27017
setParameter:
  enableLocalhostAuthBypass: true
replication:
#复制集名称
#Replication set name
  replSetName: "rscnf"
sharding:
#作为配置服务
#As configuration service
  clusterRole: configsvr
#security:  #mongos集群创建好之后再开启。
#  keyFile: "/data/mongodb/config/keyFile"
#  authorization: enabled

  
#生成秘钥文件,741个字符。
#keyFile 文件是手工创建的
openssl rand -base64 741 > /data/mongodb/config/keyFile
chmod 400 /data/mongodb/config/keyFile
#拷贝到三个节点。 
  
chown -R mongodb:mongodb /data/mongodb
#启动 configdb 的实例。
#3台configdb 实例都启动。
su - mongodb 
mongod -f /data/mongodb/apps/mongodb/bin/mongodb-config.conf &

#先注释掉:security: 部分。重启。创建用户。
#再取消注释,通过密码登录。

#免密登录 
mongo 192.168.1.8074:27017
use admin 
config={_id:"rscnf",members:[
{_id:0,host:"192.168.1.8080:27017"},
{_id:1,host:"192.168.1.8088:27017"},
{_id:2,host:"192.168.1.8074:27017"}
]}

//初始化复制集
rs.initiate(config)

登录主节点创建用户:

rscnf:PRIMARY> use admin
switched to db admin
rscnf:PRIMARY> db.createUser({user:"admin",pwd:"admin123",roles:["userAdminAnyDatabase"]})
rscnf:PRIMARY> show databases; 
admin   0.000GB
config  0.000GB
local   0.000GB
#取消注释:security: 重启,启用密码管理配置数据库。

#重启登录 

rscnf:PRIMARY> use admin 
switched to db admin
rscnf:PRIMARY> db.auth('admin','admin123');
1

2.配置分片集群

#分片集群创建
准备参数文件 
#准备三个节点。均为分片1;
[root@localhost ~]# 
cat  /data/mongodb/config/mongo_shard1.yml
systemLog:
  destination: file
  path: "/data/mongodb/logs/mongo_shard1.log"
  logAppend: true
  logRotate: rename
operationProfiling:  
  mode: slowOp
  slowOpThresholdMs: 200  
  slowOpSampleRate: 1.0
storage:
  journal:
    enabled: true
    commitIntervalMs: 162
  dbPath: "/data/mongodb/data/shard1"
  syncPeriodSecs: 67
  engine: wiredTiger
  wiredTiger:
    engineConfig:
         cacheSizeGB: 0.5
processManagement:
  fork: false
net:
  bindIp: 0.0.0.0
  #注意修改端口  # Notice Modifying the port
  port: 40001
setParameter:
  enableLocalhostAuthBypass: true
replication:
  #复制集名称 # Replication set name
  replSetName: "rsshd1"
  oplogSizeMB: 24576
sharding:
  #作为分片服务 # As a shard service
  clusterRole: shardsvr
#security:  #mongos集群创建好之后再开启。
#  keyFile: "/data/mongodb/config/keyFile"
#  authorization: enabled


启动:mongod 分片集群。
#启动三个节点 
sh mongodb_shard1.sh


mongo 192.168.1.8080:40001     
use admin
config={_id:"rsshd1",members:[
{_id:0,host:"192.168.1.8080:40001",priority:2},  
{_id:1,host:"192.168.1.8088:40001",priority:1},    
{_id:2,host:"192.168.1.8074:40001",priority:1}   
]}
rs.initiate(config)
rs.status()

#创建用户:
rsshd1:PRIMARY> use admin
switched to db admin
rsshd1:PRIMARY> db.createUser({user:"admin",pwd:"admin123",roles:["userAdminAnyDatabase"]})
#取消注释:#security: 并重启集群。

4.配置mongos集群 

重点:
mognodb_config; 取消认证参数。。
mongodb_shard 取消认证参数。 
等mongos 登录30000端口,创建好集群之后,再统一开启 认证参数:security:。



#编辑三台 
vi /data/mongodb/apps/mongodb/bin/mongodb-route.conf

[root@host-172-21-228-132 ~]# 
cat /data/mongodb/conf/mongo_route.yml
systemLog:
  destination: file
#  注意修改路径  # Note the modification path
  path: "/data/mongodb/logs/mongo_route.log"
  logAppend: true
operationProfiling:  
  slowOpThresholdMs: 200  
  slowOpSampleRate: 1.0
processManagement:
  fork: false
net:
  bindIp: 0.0.0.0
#  注意修改端口 # Notice Modifying the port
  port: 30000
setParameter:
  enableLocalhostAuthBypass: true
replication:
  localPingThresholdMs: 15
sharding:
#  关联配置服务  # Associated configuration service 
  configDB: rscnf/192.168.1.8080:27017,192.168.1.8088:27017,192.168.1.8074:27017
#security: #mongos集群创建好之后再开启。
#  keyFile: "/data/mongodb/config/keyFile"


fork=true :会创建多个mongos服务。我们只需要一个。所以设置false;

启动mongos;
#三台都启动。
mongos -f  /data/mongodb/apps/mongodb/bin/mongodb-route.conf 


4.将分片加入monggs中 
mongo 192.168.1.8080:30000
use admin
mongos> db.auth('admin','admin123');  #连接到配置集群。
sh.addShard("rsshd1/192.168.1.8080:40001,192.168.1.8088:40001,192.168.1.8074:40001")
sh.status()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值