MongoDB运维实战

mongoDB(三) mongoDB分片集群

2020-04-08  本文已影响0人  Starlightskm

mongoDB分片集群

介绍

解决数据分片,防止数据丢失
生产环境需要擦用分片+副本集的部署方式

组成部分

依赖关系

集群搭建

集群资源规划

configserver: 28017,28018,28019 三个端口搭建
route: 27017,27018,27019 三个端口搭建
shardserver: 29017,29018,29019,29020四个端口搭建

configsvr的配置

配置文件

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/28017/mongodb.log
storage:
  dbPath: /data/mongodb/28017/
  journal:
    enabled: true
processManagement:
  fork: true
net:
  port: 28017
  bindIp: 0.0.0.0
replication:
  replSetName: copySet
sharding:
  clusterRole: configsvr

启动服务

[root@centos7-node1 ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/28017/mongodb.conf 
[root@centos7-node2 ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/28018/mongodb.conf
[root@centos7-node3 ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/28019/mongodb.conf

配置副本集

[root@centos7-node1 ~]# /usr/local/mongodb/bin/mongo localhost:28017
>config={ _id:"copySet",
  configsvr: true,
   members: [
     {_id:0,host:"192.168.56.11:28017"},
     {_id:1,host:"192.168.56.12:28018"},
     {_id:2,host:"192.168.56.13:28019"}
 ]
}
> rs.initiate(config)
> rs.ststus()

router的配置

router是mongodb的入口,不存储任何数据

配置文件

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/27017/mongodb.log
processManagement:
  fork: true
net:
  port: 27017
  bindIp: 0.0.0.0
sharding:
  configDB: copySet/192.168.56.11:28017,192.168.56.12:28018,192.168.56.13:28019

启动服务

mongos 启动

[root@centos7-node1 ~]# /usr/local/mongodb/bin/mongos -f /data/mongodb/27017/mongodb.conf
[root@centos7-node2 ~]# /usr/local/mongodb/bin/mongos -f /data/mongodb/27018/mongodb.conf 
[root@centos7-node3 ~]# /usr/local/mongodb/bin/mongos -f /data/mongodb/27019/mongodb.conf 
[root@centos7-node2 ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:27018    #测试登录

shardsvr的配置

数据角色

配置文件

29017,29018,29019,29020四个端口搭建即可
数据角色
29017、29018数据角色data1
29019、29020数据角色data2

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/29017/mongodb.log
storage:
  dbPath: /data/mongodb/29017/
  journal:
    enabled: true
processManagement:
  fork: true
net:
  port: 29017
  bindIp: 0.0.0.0
replication:
  replSetName: data1
sharding:
  clusterRole: shardsvr
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/29019/mongodb.log
storage:
  dbPath: /data/mongodb/29019/
  journal:
    enabled: true
processManagement:
  fork: true
net:
  port: 29019
  bindIp: 0.0.0.0
replication:
  replSetName: data2
sharding:
  clusterRole: shardsvr

服务启动

/usr/local/mongodb/bin/mongod -f /data/mongodb/29017/mongodb.conf
/usr/local/mongodb/bin/mongod -f /data/mongodb/29018/mongodb.conf
/usr/local/mongodb/bin/mongod -f /data/mongodb/29019/mongodb.conf
/usr/local/mongodb/bin/mongod -f /data/mongodb/29020/mongodb.conf
29017#### 配置
> config = {
        "_id" : "data1",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.56.11:29017"
                },
                {
                        "_id" : 1,
                        "host" : "192.168.56.12:29018"
                }
        ]
}
> rs.initiate(config) 
29019#### 配置
{
        "_id" : "data2",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.56.13:29019"
                },
                {
                        "_id" : 1,
                        "host" : "192.168.56.14:29020"
                }
        ]
}
> rs.initiate(config)

mongodb分片的使用

添加数据分片到集群中

[root@centos7-node1 ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017
mongos> sh.status()      #状态查询
mongos> sh.addShard("data1/192.168.56.11:29017,192.168.56.12:29018")    #添加数据分片1
mongos> sh.addShard("data2/192.168.56.13:29019,192.168.56.14:29020")   #添加数据分片2
mongos> use test    
mongos> for(i=1;i<1000;i++){
... db.myuser.insert({name:"myuser"+i,age:i})
... }
mongos> sh.status()     #可以查看数据存储到哪个数据分片上了
mongos> db.dropDatabase()    #删除数据
mongos> use admin
mongos> db.runCommand({enablesharding:"test"})
mongos> db.runCommand({shardcollection:"test.myuser",key:{_id:"hashed"}})
mongos> use test    #写入数据
mongos> for(i=1;i<=1000;i++){
... db.myuser.insert({name:"myuser"+i,age:i})
... }
WriteResult({ "nInserted" : 1 })
mongos> db.myuser.count()
1000

注意

上一篇下一篇

猜你喜欢

热点阅读