温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Mongodb cluster

发布时间:2020-07-17 15:44:01 来源:网络 阅读:311 作者:Robert杨 栏目:MongoDB数据库

MongoDB分片(Sharding)技术

  分片(sharding)是MongoDB用来将大型集合分割到不同服务器(或者说一个集群)上所采用的方法。尽管分片起源于关系型数据库分区,但MongoDB分片完全又是另一回事。

  和MySQL分区方案相比,MongoDB的最大区别在于它几乎能自动完成所有事情,只要告诉MongoDB要分配数据,它就能自动维护数据在不同服务器之间的均衡。

1 分片的目的
  高数据量和吞吐量的数据库应用会对单机的性能造成较大压力,大的查询量会将单机的CPU耗尽,大的数据量对单机的存储压力较大,最终会耗尽系统的内存而将压力转移到磁盘IO上。

  为了解决这些问题,有两个基本的方法: 垂直扩展和水平扩展。

    垂直扩展:增加更多的CPU和存储资源来扩展容量。

    水平扩展:将数据集分布在多个服务器上。水平扩展即分片。

2 分片设计思想
  分片为应对高吞吐量与大数据量提供了方法。使用分片减少了每个分片需要处理的请求数,因此,通过水平扩展,集群可以提高自己的存储容量和吞吐量。举例来说,当插入一条数据时,应用只需要访问存储这条数据的分片.

  使用分片减少了每个分片存储的数据。

  例如,如果数据库1tb的数据集,并有4个分片,然后每个分片可能仅持有256 GB的数据。如果有40个分片,那么每个切分可能只有25GB的数据。

Docker部署
架构设计:

三台主机启动相同的进程,mongos 、config server、shard server

Rancher 部署
每个主机按照计划添加标签:

mongos=true

shard2=true

shard3=true

shard1=true

config=true

host网络模式,端口务必不能被占用

新建服务:

粘贴以下内容:

compose
version: '2'
services:
  shard2:
    image: mongo:4.0.1-xenial
    stdin_open: true
    network_mode: host
    volumes:
    - /data/shard2:/data/db
    tty: true
    command:
    - /usr/bin/mongod
    - --shardsvr
    - --replSet
    - shard2
    - --port
    - '27002'
    - --bind_ip_all
    - --dbpath
    - /data/db
    - --logpath
    - /data/db/shard2.log
    - --oplogSize
    - '10'
    labels:
      io.rancher.scheduler.affinity:host_label: shard2=true
      io.rancher.container.pull_image: always
      io.rancher.scheduler.global: 'true'
  mongos:
    image: mongo:4.0.1-xenial
    stdin_open: true
    network_mode: host
    volumes:
    - /data/mongos:/data/db
    tty: true
    command:
    - mongos
    - --configdb
    - myset/172.20.101.132:27000,172.20.101.133:27000,172.20.101.134:27000
    - --port
    - '27017'
    - --bind_ip_all
    labels:
      io.rancher.scheduler.affinity:host_label: mongos=true
      io.rancher.container.pull_image: always
      io.rancher.scheduler.global: 'true'
  shard3:
    image: mongo:4.0.1-xenial
    stdin_open: true
    network_mode: host
    volumes:
    - /data/shard3:/data/db
    tty: true
    command:
    - /usr/bin/mongod
    - --shardsvr
    - --replSet
    - shard3
    - --port
    - '27003'
    - --bind_ip_all
    - --dbpath
    - /data/db
    - --logpath
    - /data/db/shard3.log
    - --oplogSize
    - '10'
    labels:
      io.rancher.scheduler.affinity:host_label: shard3=true
      io.rancher.container.pull_image: always
      io.rancher.scheduler.global: 'true'
  shard1:
    image: mongo:4.0.1-xenial
    stdin_open: true
    network_mode: host
    volumes:
    - /data/shard1:/data/db
    tty: true
    command:
    - /usr/bin/mongod
    - --shardsvr
    - --replSet
    - shard1
    - --port
    - '27001'
    - --bind_ip_all
    - --dbpath
    - /data/db
    - --logpath
    - /data/db/shard1.log
    - --oplogSize
    - '10'
    labels:
      io.rancher.scheduler.affinity:host_label: shard1=true
      io.rancher.container.pull_image: always
      io.rancher.scheduler.global: 'true'
  config:
    image: mongo:4.0.1-xenial
    stdin_open: true
    network_mode: host
    volumes:
    - /data/config:/data/db
    tty: true
    command:
    - /usr/bin/mongod
    - --configsvr
    - --replSet
    - myset
    - --bind_ip_all
    - --dbpath
    - /data/db
    - --port
    - '27000'
    - --logpath
    - /data/db/config.log
    labels:
      io.rancher.scheduler.affinity:host_label: config=true
      io.rancher.container.pull_image: always
      io.rancher.scheduler.global: 'true'
rancher-compose:

rancher-compose
version: '2'
services:
  mongos:
    start_on_create: true
  shard2:
    start_on_create: true
  shard3:
    start_on_create: true
  shard1:
    start_on_create: true
  config:
    start_on_create: true

1、登录某一个宿主机链接shard1:

127.0.0.1:27001/admin

config = { _id:"shard1", members:[

                     {_id:0,host:"172.20.101.132:27001"},

                     {_id:1,host:"172.20.101.133:27001"},

                     {_id:2,host:"172.20.101.134:27001"}

                ]

         }

rs.initiate(config)

2、链接shard2

127.0.0.1:27002/admin


config = { _id:"shard2", members:[

                     {_id:0,host:"172.20.101.132:27002"},

                     {_id:1,host:"172.20.101.133:27002"},

                     {_id:2,host:"172.20.101.134:27002"}

                ]

         }

rs.initiate(config)

3、链接shard3

127.0.0.1:27003/admin

config = { _id:"shard3", members:[
                     {_id:0,host:"172.20.101.132:27003"},
                     {_id:1,host:"172.20.101.133:27003"},
                     {_id:2,host:"172.20.101.134:27003"}
                ]
         }

rs.initiate(config)
4、链接config服务:

config = {_id: 'myset', members: [
                          {_id: 0, host: '172.20.101.132:27000'},
                          {_id: 1, host: '172.20.101.133:27000'},
                          {_id: 2, host: '172.20.101.134:27000'}]
           }

rs.initiate(config)
5、登录mongos:

mongo  127.0.0.1:27017/admin #登录mongos,增加节点
db.runCommand( { addshard : "shard1/172.20.101.132:27001,172.20.101.133:27001,172.20.101.134:27001",name:"shard1"});
db.runCommand( { addshard : "shard2/172.20.101.132:27002,172.20.101.133:27002,172.20.101.134:27002",name:"shard2"});
db.runCommand( { addshard : "shard3/172.20.101.132:27003,172.20.101.133:27003,172.20.101.134:27003",name:"shard3"});

6、查看集群状态
sh.status()
主机安装
一、安装

1、

cat >/etc/yum.repos.d/mongodb-org-4.0.repo<<EOF
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOF

yum install -y mongodb-org-4.0.1 mongodb-org-server-4.0.1 mongodb-org-shell-4.0.1 mongodb-org-mongos-4.0.1 mongodb-org-tools-4.0.1

2、

建立目录 mongos目录、config server目录、share目录

mkdir -p /data/mongodb/{shard1,shard2,shard3,mongos,config,shard1/data,shard1/log,shard2/data,shard2/log,shard3/data,shard3/log,mongos/log,config/log,config/data}

chown mongod.mongod /data/mongodb/ -R

3、

需要启动5个进程所以要规划5个组件对应的端口号,由于一个机器需要同时部署 mongos、config server 、shard1、shard2、shard3

mongos:27017

config server:27000

shard1、shard2、shard3 : 27001 27002 27003

4、启动各进程

#mongos
mongos --configdb myset/172.20.101.132:27000,172.20.101.133:27000,172.20.101.134:27000 --port 27017 --logpath /data/mongodb/mongos/mongos.log --fork

#config
mongod --configsvr --replSet myset --bind_ip_all --dbpath /data/mongodb/config/data --port 27000 --logpath /data/mongodb/config/log/config.log --fork

#shard1 #shard2 #shard3

mongod --shardsvr --replSet shard1 --bind_ip_all --port 27001 --dbpath /data/mongodb/shard1/data --logpath /data/mongodb/shard1/log/shard1.log --fork --oplogSize 10
mongod --shardsvr --replSet shard2 --bind_ip_all --port 27002 --dbpath /data/mongodb/shard2/data --logpath /data/mongodb/shard2/log/shard2.log --fork --oplogSize 10
mongod --shardsvr --replSet shard3 --bind_ip_all --port 27003 --dbpath /data/mongodb/shard3/data --logpath /data/mongodb/shard3/log/shard3.log --fork --oplogSize 10

5、设置副本集

连接 shard1:mongo 127.0.0.1:27001/admin/

config = { _id:"shard1", members:[
{_id:0,host:"172.20.101.132:27001"},
{_id:1,host:"172.20.101.133:27001"},
{_id:2,host:"172.20.101.134:27001",arbiterOnly:true}
]
}
rs.initiate(config)

连接 shard2:mongo 127.0.0.1:27001/admin

config = { _id:"shard2", members:[
{_id:0,host:"172.20.101.132:27002"},
{_id:1,host:"172.20.101.133:27002"},
{_id:2,host:"172.20.101.134:27002",arbiterOnly:true}
]
}
rs.initiate(config)

连接 shard3:mongo 127.0.0.1:27001/admin

config = { _id:"shard3", members:[
{_id:0,host:"172.20.101.132:27003"},
{_id:1,host:"172.20.101.133:27003"},
{_id:2,host:"172.20.101.134:27003",arbiterOnly:true}
]
}

config server 副本

config = {_id: 'myset', members: [
{_id: 0, host: '172.20.101.132:27000'},
{_id: 1, host: '172.20.101.133:27000'},
{_id: 2, host: '172.20.101.134:27000'}]
}
rs.initiate(config)

6、mongo 127.0.0.1:27017/admin #登录mongos,增加节点

db.runCommand( { addshard : "shard1/172.20.101.132:27001,172.20.101.133:27001,172.20.101.134:27001",name:"shard1"});
db.runCommand( { addshard : "shard2/172.20.101.132:27002,172.20.101.133:27002,172.20.101.134:27002",name:"shard2"});
db.runCommand( { addshard : "shard3/172.20.101.132:27003,172.20.101.133:27003,172.20.101.134:27003",name:"shard3"});

致辞,部署完成。

测试:
1、杀掉第二个节点所有进程
可读写,没有任何影响
2、继续杀掉第一个主节点
只剩第三个节点,第三个节点依然是SECONDARY,节点不可写。重启节点2后,节点3变为主节点,功能恢复,继续启动第一个节点,第几节点变为SECONDARY

3、如果节点太长时间没有建立连接,启动后会变成recovery状态,操作:关掉进程,删除数据文件,重启即可。

4、添加删除shard 节点:

rs.remove("172.20.101.134:27002");
rs.add("172.20.101.134:27002");

5、添加删除shard分片,需要在主节点操作,查找主节点,链接mongos:mongo ip:27017/admin,sh.status()即可找到主节点

db.runCommand( { addshard : "shard4/172.20.101.125:27004,172.20.101.133:27004",name:"shard4"});
db.runCommand( { removeshard : "shard4/172.20.101.125:27004,172.20.101.133:27004",name:"shard4"});

常用操作:
1、用户相关

db.auth('admin','ptmind') #登录认证

创建用户附权限
db.createUser({user:'datadeck', pwd:'ptmind', roles:['userAdminAnyDatabase']});
db.createUser({user:'datadeck', pwd:'ptmind', roles:['root']});

2、增删改查:

新建并选择数据库
use nettest

新增并写入数据

db.testtable.insert({'k':'3','c':'4'})
查看表
show collections
查看表内数据
db.testtable.find().pretty()

添加删除shard 节点:

rs.remove("172.20.101.134:27002");
rs.add("172.20.101.134:27002");

添加删除shard分片,需要在主节点操作,查找主节点,链接mongos:mongo ip:27017/admin,sh.status()即可找到主节点

db.runCommand( { addshard : "shard4/172.20.101.125:27004,172.20.101.133:27004",name:"shard4"});
db.runCommand( { removeshard : "shard4/172.20.101.125:27004,172.20.101.133:27004",name:"shard4"});

3、集群状态

查看副本集命令:

db.runCommand( { listshards : 1 } )

查看状态:

sh.status();

激活数据库分片功能

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

mongos> db.runCommand( { enablesharding : "test" } )
指定分片建对集合分片,范围片键–创建索引

查看shard状态,登录某个shard节点

rs.status()

4、备份恢复

a、mongodump 导出为二进制bson文件

mongodump -h dbhost -d dbname -o dbdirectory(目录)

           备份单个collection
           mongodump --db test --collection collection

           备份单个库

mongodump -h 127.0.0.1:27017/admin -d db_distribution_test_YYJOTVSUQI -o /var/tmp/db_distribution_test_YYJOTVSUQI.mongodb

备份整个库去掉表明即可

mongorestore恢复数据默认是追加,如打算先删除后导入,可以加上--drop参数,不过添加--drop参数后,会将数据库数据清空后再导入,如果数据库备份后又新加入了数据,也会将新加的数据删除,它不像mysql有一个存在的判断。
mongorestore -h <hostname><:port> -d dbname <path>

b、mongoexport

-h,--host :代表远程连接的数据库地址,默认连接本地Mongo数据库;
--port:代表远程连接的数据库的端口,默认连接的远程端口27017;
-u,--username:代表连接远程数据库的账号,如果设置数据库的认证,需要指定用户账号;
-p,--password:代表连接数据库的账号对应的密码;
-d,--db:代表连接的数据库;
-c,--collection:代表连接数据库中的集合;
-f, --fields:代表集合中的字段,可以根据设置选择导出的字段;
--type:代表导出输出的文件类型,包括csv和json文件;
-o, --out:代表导出的文件名;
-q, --query:代表查询条件;
--skip:跳过指定数量的数据;
--limit:读取指定数量的数据记录;
--sort:对数据进行排序,可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而-1是用于降序排列,如sort({KEY:1})。
mongoimport 简介,通过帮助先了解下mongoimport的功能参数

关键参数说明:

h,--host :代表远程连接的数据库地址,默认连接本地Mongo数据库;
--port:代表远程连接的数据库的端口,默认连接的远程端口27017;
-u,--username:代表连接远程数据库的账号,如果设置数据库的认证,需要指定用户账号;
-p,--password:代表连接数据库的账号对应的密码;
-d,--db:代表连接的数据库;
-c,--collection:代表连接数据库中的集合;
-f, --fields:代表导入集合中的字段;
--type:代表导入的文件类型,包括csv和json,tsv文件,默认json格式;
--file:导入的文件名称
--headerline:导入csv文件时,指明第一行是列名,不需要导入;
实例演示:

#首先查看集合中的数据 > db.bike_bak.find() { "_id" : ObjectId("59e8c27804390e04a063159d"), "lat" : 39.9571954199, "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "source" : "ofo", "lng" : 116.3926501736 } #删除集合中的数据 > db.bike_bak.remove({"bikeId" : "pgdAVg"}) WriteResult({ "nRemoved" : 1 }) #查看集合是否包含数据 > db.bike_bak.find() > #导入数据 [root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoimport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike_bak --type=json --file bike.csv 2017-10-25T11:59:51.020+0800 connected to: localhost:27030
2017-10-25T11:59:51.030+0800 imported 1 document #检查数据是否导入成功 > db.bike_bak.find() { "_id" : ObjectId("59e8c27804390e04a063159d"), "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "lat" : 39.9571954199, "lng" : 116.3926501736, "source" : "ofo" }
[root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoimport --help Usage: mongoimport <options> <file>

注意:

  当查询时同时使用sort,skip,limit,无论位置先后,最先执行顺序 sort再skip再limit。

实例:

  首先查看下数据库中的数据一共多少条,通过的命令的方式查看下我们要导出的数据信息

db.bike.find().count() 16878865
db.bike.find({"source":"ofo"}).limit(1) { "_id" : ObjectId("59e8c27804390e04a063159d"), "lat" : 39.9571954199, "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "source" : "ofo", "lng" : 116.3926501736 } >
如何通过mongoexport导出"bikeId" : "pgdAVg"的数据,我导出了json和csv两种类型的数据;

#导出类型为json,数据库:mapdb,集合:bike 字段:bikeId,lat,lng,current_time,source ,条件为source字段为ofo第一条数据 mongoexport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike -f bikeId,lat,lng,current_time,source --type=json -o bike.csv --query='{"source":"ofo"}' --limit=1 #导出类型为csv,数据库:mapdb,集合:bike 字段:bikeId,lat,lng,current_time,source ,条件为source字段为ofo第一条数据 mongoexport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike -f bikeId,lat,lng,current_time,source --type=csv -o bike.csv --query='{"source":"ofo"}' --limit=1

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI