去年的笔记,接着发

一、如何移除分片

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()命令确认迁移是否成功,可能会花比较长的时间。

转载于:https://blog.51cto.com/raugher/1735153

Mongodb 添加删除分片与非分片表维护相关推荐

  1. 用SQL语句添加删除修改字段及一些表与字段的基本操作 .

    用SQL语句添加删除修改字段及一些表与字段的基本操作 分类: MS SQL 2009-07-02 14:41 222人阅读 评论(0) 收藏 举报 用SQL语句添加删除修改字段 1.增加字段      ...

  2. 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等

    用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...

  3. MyCat分片规则(全局表,ER分片表,多对多关联,主键分片VS非主键分片),MyCat常用的分片规则(15中分片规则),自定义MyCat分片规则,其它术语

    1 MyCat分片规则 数据切分中重要的几条原则,其中有几条数据冗余,表分组(Table Group). 1.1全局表 如果你的业务中有些数据类似于数据字典,比如配置文件的配置,常用业务的配置或数据量 ...

  4. MongoDB 学习笔记八 复制、分片、备份与恢复、监控

    MongoDB 学习笔记八 复制.分片.备份与恢复.监控 MongoDB复制(副本集) 什么是复制? MongoDB 复制原理 MongoDB 副本集设置 副本集添加成员 MongoDB 分片 分片 ...

  5. mycat分表之ER表分片、范围分片、取模分片、日期分片、全局表等

    概述 数据库进行水平分表时,需要按照一定规则对数据进行分片. Mycat的分片策略可以分为两种: 连续分片和离散分片. 连续分片常用的有分片规则有: 范围分片. 日期分片. 优点:如果进行范围查询,比 ...

  6. mongodb 集群shard_【mongoDB运维篇④】Shard 分片集群

    简述 为何要分片 减少单机请求数,降低单机负载,提高总负载 减少单机的存储空间,提高总存空间. 常见的mongodb sharding 服务器架构 要构建一个 MongoDB Sharding Clu ...

  7. mongodb 集群shard_【mongoDB运维篇④】Shard分片集群

    ## 简述 ### 为何要分片 1. 减少单机请求数,降低单机负载,提高总负载 2. 减少单机的存储空间,提高总存空间. ![此处输入图片的描述][1] ### **常见的mongodb shardi ...

  8. 学习MongoDB 二:MongoDB添加、删除、修改

    原文:http://blog.csdn.net/congcong68/article/details/46781777 一.简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSQ ...

  9. 搭建mongodb集群(副本集+分片)

    完整的搭建mongodb集群(副本集+分片)的样例... 准备四台机器,各自是bluejoe1,bluejoe2,bluejoe3,以及bluejoe0 副本集及分片策略确定例如以下: 将创建3个副本 ...

最新文章

  1. mysql远程主机强迫关闭了一个现有连接_asp.net连接mysql出现了远程主机强迫关闭了一个现有的连接。!!!...
  2. PHP 给图片制作水印的方法
  3. 实习推荐 | 腾讯AI Lab虚拟人中心招聘算法工程师实习生
  4. nginx如何开启debug日志及相关配置
  5. [NOIP1999] 提高组 洛谷P1014 Cantor表
  6. 家用计算机历史记录,教您如何查看电脑使用记录
  7. P2678 [NOIP2015 提高组] 跳石头
  8. php通过数组存取mysql查询语句的返回值
  9. python百分号字符串_python--003--百分号字符串拼接、format
  10. android裁剪部分放大动画,【Android】图片放大被截了一部分之认识clipChildren属性的用法...
  11. 蒙特卡洛估值几种不同的计算方式(Python)
  12. VFP开眼看世界的第一眼,就是学会真正的BS开发,走错一步费三年
  13. 数学原理(The Principles of Mathmatics)
  14. BOCHS 模拟器和我的启动代码
  15. 分类--ROC 和曲线下面积
  16. scum服务器里找不到车,人渣SCUM车辆机制介绍 人渣SCUM车辆为什么消失
  17. 毁掉一个孩子的几个方法 有多少家长正在这么做?
  18. HDMI 收发器简化家庭影院系统设计
  19. 〖Python接口自动化测试实战篇②〗- 摒弃 ‘捉虫师’ 称号 - 你需要重新认识软件测试
  20. adb连接夜神模拟器提示:adb unable to connect to 127.0.0.162001 cannot connect to 127.0.0.16200 由于目标 计算机积极拒绝

热门文章

  1. JavaScript 高级技巧 Memoization
  2. 在Flash中利用PCRE正则式漏洞CVE-2015-0318的方法
  3. CUDA学习(十五)
  4. vmstat参数解释
  5. python 3 最佳python中文书籍下载
  6. 自然语言处理中的N-Gram模型
  7. Visual C++ 时尚编程百例019(串行化)
  8. Swift 必备开发库 (高级篇)
  9. 【iCore3 双核心板】例程十七:USB_MSC实验——读/写U盘(大容量存储器)
  10. VMware View 5.0从菜鸟到高手系列 10 –远程图形工作站配置篇