Home Database Mysql Tutorial mongodb sharding原理学习与试用(六)之chunk手动切割

mongodb sharding原理学习与试用(六)之chunk手动切割

Jun 07, 2016 pm 05:56 PM
mongodb sharding principle study try out

1. 手动切割chunk主要是两个函数splitAt(fullname,middle)与splitFind(fullname,find). fullname指定哪个库的哪个集合。middle与find都是条件,代表你想手动切割哪个chunk. 需要注意的是条件必须包含片键,不然报错,如下图。 这两个函数不同的是: 1.1 spli

1. 手动切割chunk主要是两个函数splitAt(fullname,middle)与splitFind(fullname,find). fullname指定哪个库的哪个集合。middle与find都是条件,代表你想手动切割哪个chunk. 需要注意的是条件必须包含片键,不然报错,如下图。

  

这两个函数不同的是:

 1.1 splitAt利用middle这个条件找到对应的chunk,并以这个条件所查询到的第一条结果为分隔点,把原先的chunk分隔成两部分。

  (一)在手动切隔之前,sar总共有三个块。如下图

  

  (二)执行切割命令

  

  之后chunks的分布如下图

  

  可以看出,执行之后,将第二个chunk,香港虚拟主机,按照{_id:ObjectId("50dc0790525e4314024b79d0")}这个值为分隔点分隔成第2,3两块。

 1.2 函数splitFind,官网解释是将找到的第一个chunk分隔成size大小相等的两个chunk.但是我在测试过程中发现并不是这么回事。版本是2.2.2

  (一)准备数据。向一个新集合插入250W条有规律的数据。如下图。字段name的值末尾是一个自增的数字。

  

  (二)数据插入之后,集合的分块情况如下图。

  

  (三)我们可以看下,第一块最后一条数据是什么。

  

  (四)可以看到,第一块最后一条数据的name值是"habc780335".也就是说第一块有78W条数据之多。现在将第一块分成两块。使用splitFind()函数。

  

  (五)name值为"habc19"肯定是在第一块中。因为按官网所说,应该将第一块分成size大小相等的两块。可实际上呢?

  

  (六)如上图,的确是将原先的第一块分成了两块。第二块最后一条的ID值就是原先第一块最后一条的ID值。但第1,2这两块size大小是相等的吗?如下图。

  

  (七)如上图。第一块的最后一条数据实际上就是第一条数据。这说明第一块实际上只有一条数据。很显然这两块的size是不相等的。真实情况究竟是什么呢 ???

2. 今天在将一个已被移除的shard重新添加进来的时候,出现了问题。特记录下来。

  问题:将一个shard移除后,我没有停掉这个shard。后来为了测试我又把它添加进来。db.runCommand({addshard:"hostname:port"}); 操作提示成功。也开始了迁移数据。等迁移完成之后,我进行查询操作,发现操作失败。错误提示是“gotshardname different than what i had before” 。如下图

  错误提示是说,这个shard之前添加进来的时候name被赋值为shard0001,现在再次添加进来后name被赋值为shard0000.当进行查询操作时发现这前后name不一样。所以报错。我比较困惑的是,它是怎么知道这个shard之前的name值的。我找遍config库下面的所有collection.都没有发现有地方存储shard之前的name值。仅仅只有shards这个集合存储相关数据。但是存储的都是此时此刻各个shard的情况。问了渡娘与google.没有什么收获。后来没办法,我想把这个shard再一次的remove掉后再添加进来。可以不行。

  

  虽然操作提示是成功的,但是过了很久我发现数据根本就没有迁移。查询日志,发现了错误提示。

  

  错误提示仍是name值前后不一样。后来在同事的提示下,我将这个shard重启,重启后发现问题解决了。

  分析:每个添加进去的shard的name值不仅在config库存储,同时各个shard也都存储着这个值。不同的是,config库的数据是落地的,但各个shard是缓存起来的。被移除的shard如果不重启,那么这个name值就一直存在。有一点我不理解的是,既然name值存在,并且再次添加后name不一样却能添加成功,并且数据也都迁移过来了,香港虚拟主机,只是查询操作的时候才报错。难道迁移数据与查询数据使用的是不同地方的name值? 有一点需要注意的是,虽然查询操作失败,但写操作能够成功。

  如何避免:db.runCommand({addshard:"hostname:port",name:"xxx"}) VS db,runCommand({addshard:"hostname:port"})。 在addshard的时候,我没有指定name值,系统就使用默认值从shard0000开始递增。因此,在addshard时一定要手动指定name值。

 

 

 

 

  

  

  

,服务器空间
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

What is the use of net4.0 What is the use of net4.0 May 10, 2024 am 01:09 AM

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

How to encrypt data in Debian MongoDB How to encrypt data in Debian MongoDB Apr 12, 2025 pm 08:03 PM

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

See all articles