Mongodb 删除添加分片与非分片表维护

 更新时间:2016年01月15日 09:10:01   作者:aqszhuaihuai  
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。通过本文给大家介绍Mongodb 删除添加分片与非分片表维护的相关知识,对此文感兴趣的朋友一起学习吧

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

一、如何移除分片

1、确认balancer已经开启

mongos> sh.getBalancerState()
true

2、移除分片

注:在admin db下执行命令。

mongos> use admin
switched to db admin
mongos> db.runCommand( { removeShard: "shard3" } )
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard3",
"ok" : 1
}

3、检查迁移的状态

同样执行

mongos> use admin
switched to db admin
mongos> db.runCommand( { removeShard: "shard3" } )
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(3),
"dbs" : NumberLong(0)
},
"ok" : 1
}

remaining中的chunks表示还有多少数据块未迁移。

4、移除未分片数据

In a cluster, a database with unsharded collections stores those collections only on a single shard.
That shard becomes the primary shard for that database. (Different databases in a cluster can have different primary shards.)
WARNING
Do not perform this procedure until you have finished draining the shard.
1)To determine if the shard you are removing is the primary shard for any of the cluster's databases, issue one of the following methods:
sh.status()
db.printShardingStatus()
In the resulting document, the databases field lists each database and its primary shard.
For example, the following database field shows that the products database uses mongodb0 as the primary shard:
{ "_id" : "products", "partitioned" : true, "primary" : "mongodb0" }
2)To move a database to another shard, use the movePrimary command. For example, to migrate all remaining unsharded data from mongodb0 to mongodb1,
issue the following command:
use admin
db.runCommand( { movePrimary: "products", to: "mongodb1" }) --products为db name
This command does not return until MongoDB completes moving all data, which may take a long time.
The response from this command will resemble the following:
{ "primary" : "mongodb1", "ok" : 1 }
If you use the movePrimary command to move un-sharded collections, you must either restart all mongos instances,
or use the flushRouterConfig command on all mongos instances before writing any data to the cluster.
This action notifies the mongos of the new shard for the database.
If you do not update the mongos instances' metadata cache after using movePrimary, the mongos may not write data to the correct shard.
To recover, you must manually intervene.

根据上面所说,迁移非分片表 时 最好停机,在运行db.runCommand( { movePrimary: "products", to: "mongodb1" }) 命令完成之后,刷新所有mongos后(所有mongos上运行db.runCommand("flushRouterConfig")),再对外提供服务。当然也可以重新启动所有mongos实例 。

5、完成迁移

mongos> use admin
switched to db admin
mongos> db.runCommand( { removeShard: "shard3" } )
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shard3",
"ok" : 1
}

如果state为 completed,表示已完成迁移。

二、添加分片

1、首先确认balancer已经开启

mongos> sh.getBalancerState()
true

2、执行添加分片的命令

如果出现以下错误,删除目标shard3上的test1数据库,再次执行命令

mongos> sh.addShard("shard3/192.168.137.138:27019")
{
"ok" : 0,
"errmsg" : "can't add shard shard3/192.168.137.138:27019 because a local database 'test1' exists in another shard1:shard1/192.168.137.111:27017,192.168.137.75:27017"
}
mongos> sh.addShard("shard3/192.168.137.138:27019")
{ "shardAdded" : "shard3", "ok" : 1 }

最后运行sh.status()命令确认迁移是否成功,可能会花比较长的时间。

以上内容是给大家介绍了Mongodb 删除添加分片与非分片表维护的全部叙述,希望对大家有所帮助。

相关文章

  • MongoDB快速入门笔记(二)之MongoDB的概念及简单操作

    MongoDB快速入门笔记(二)之MongoDB的概念及简单操作

    MongoDB是面向集合的文档式数据库,不像关系数据库那样,有表,列、行,mongoDB数据库则是由一系列的文档组成。接下来通过本文给大家介绍MongoDB的概念及简单操作,一起看看吧
    2016-06-06
  • MongoDB学习笔记之MapReduce使用示例

    MongoDB学习笔记之MapReduce使用示例

    这篇文章主要介绍了MongoDB学习笔记之MapReduce使用示例,本文直接给出实例代码,需要的朋友可以参考下
    2015-07-07
  • MongoDB快速入门及其SpringBoot实战教程

    MongoDB快速入门及其SpringBoot实战教程

    MongoDB是一个开源、高性能、无模式的文档型数据库,当初的设计就是用于简化开发和方便扩展,是NoSQL数据库产品中的一种,它支持的数据结构非常松散,是一种类似于JSON的格式叫BSON,本文介绍MongoDB快速入门及其SpringBoot实战,感兴趣的朋友一起看看吧
    2023-12-12
  • 使用命令方式安装MongoDB指南(Windows、Linux)

    使用命令方式安装MongoDB指南(Windows、Linux)

    这篇文章主要介绍了使用命令方式安装MongoDB指南,本文分别介绍Windows、Linux下使用命令的方式安装mongodb,需要的朋友可以参考下
    2015-04-04
  • MongoDB数据库安装部署及警告优化

    MongoDB数据库安装部署及警告优化

    大家好,本篇文章主要讲的是MongoDB数据库安装部署及警告优化,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • NoSQL反模式 - 文档数据库篇

    NoSQL反模式 - 文档数据库篇

    我们设计关系数据库Schema的都有一套完整的方案,而NoSQL却没有这些。半年前笔者读了本《SQL反模式》的书,觉得非常好。就开始留意,对于NoSQL是否也有反模式?好的反模式可以在我们设计Schema告诉哪里是陷阱和悬崖。
    2014-08-08
  • CentOS 7下用yum快速安装MongoDB的方法教程

    CentOS 7下用yum快速安装MongoDB的方法教程

    MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。本文介绍了CentOS 7系统下用yum快速安装MongoDB的方法教程,文中通过一步步的步骤介绍的很详细,有需要的朋友们可以参考借鉴。
    2016-12-12
  • Mongodb读数据操作

    Mongodb读数据操作

    今天小编就为大家分享一篇关于Mongodb读数据操作,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • mongodb replica set 配置高性能多服务器详解

    mongodb replica set 配置高性能多服务器详解

    mongodb的多服务器配置,以前写过一篇文章,是master-slave模式的,master-slave模式,不能自动实现故障转移和恢复。所以推荐大家使用mongodb的replica set,来实现多服务器的高性能。
    2014-07-07
  • MongoDB4.28开启权限认证配置用户密码登录功能

    MongoDB4.28开启权限认证配置用户密码登录功能

    这篇文章主要介绍了MongoDB4.28开启权限认证配置用户名和密码认证登录,本文分步骤给大家介绍开启认证登录的方法,需要的朋友可以参考下
    2022-01-01

最新评论