Home Database Mysql Tutorial Mongo服务器集群配置学习三分片

Mongo服务器集群配置学习三分片

Jun 07, 2016 pm 02:58 PM
mongo Fragmentation study Serve server Configuration cluster

Mongo服务器集群配置学习三分片 MongoDB的分片主要是指将集合拆分成小块并分别存在不同服务器上的过程。MongoDB支持自动分片,可摆脱手动分片管理上的困难。 在以下情况下需要运用分片: 1.服务器的磁盘不够用。 2.单个Mongod不能满足写数据的性能需求。 3.

Mongo服务器集群配置学习三——分片

MongoDB的分片主要是指将集合拆分成小块并分别存在不同服务器上的过程。MongoDB支持自动分片,可摆脱手动分片管理上的困难。
在以下情况下需要运用分片:
1.服务器的磁盘不够用。
2.单个Mongod不能满足写数据的性能需求。
3.需要将大数据放入内存中提高性能。
下图为我们要实现的分片结构:
Mongo服务器集群配置学习三分片
从图中我们看到原本一台Mongod节点被分成了A和B两个分片,由路由D读取配置服务器C的分片策略,然后决定数据存储在哪个分片上,而路由隐藏了决策的细节,用户直接访问路由就可以享受分片带来的优点,而不必关心路由读取分片的细节。
配置步骤:
1.创建配置服务器C
创建配置文件如下,端口为10000,并启动mongod -f config.cnf

dbpath=D:\mongodb\test\sharded\C\Data
bind_ip=127.0.0.1
port=10000

2.创建路由服务器D
路由器的配置文件如下:

bind_ip=127.0.0.1
port=20000
configdb=127.0.0.1:10000

其中configdb=127.0.0.1:10000配置的是路由监听的配置服务器的地址
路由器用mongos启动

mongos -f config.cnf

  注意:配置服务器要先启动,因为路由服务器需要监听配置服务器。
3.新建分片服务器A和B
A的配置,然后启动A

dbpath=D:\mongodb\test\sharded\A\Data
bind_ip=127.0.0.1
port=8001  

B的配置,然后启动B

dbpath=D:\mongodb\test\sharded\B\Data
bind_ip=127.0.0.1
port=8002

4.建立集群中分片服务器与路由器的连接
这个操作需要在路由器中配置,打开路由器的shell,执行数据库命令
db.runCommand({addshard:"127.0.0.1:8001",allowLocal:true})
db.runCommand({addshard:"127.0.0.1:8002",allowLocal:true})
Mongo服务器集群配置学习三分片

 可以看到执行添加分片的操作要在admin库中进行
5.为业务数据库添加分片功能
为person添加分片功能,在路由中执行db.runCommand({"enablesharding":"person"})
6.为集合进行分片
  片键:集合中的一个键作为分拆的依据。
  为person库的集合info进行分片,key字段设置了片键
  执行下列命令db.runCommand({"shardcollection":"person.info","key":{"_id":1}})
7.添加一定的大数据量,测试分片的功能
用脚本插入80万条数据
for(i=1;i   db.info.insert({name:i})
}
在分别在分片服务器A和B上查询数据量,如图:看到这800001条数据已经分别存放在2个分片中了
Mongo服务器集群配置学习三分片

正式环境配置
成功地构建分片需要如下条件:
1.多个配置服务器
2.多个mongos服务器
3.每个片都是副本集
1.多个配置服务器
创建配置服务器如上,现在启动mongos的时候应将其连接到这3个配置服务器。假如3个配置文件的端口号是20001~20003
mongos --configdb localhost:20001,localhost:20002,localhost:20003
配置服务器使用的是两步提交机制,不是普通的MongoDB的异步复制,来维护集群配置的不同副本。这样能保证集群状态的一致性。这意味着某台配置服务器down了后,集群配置信息将是只读的。但是客户端还是能够读写的,只有所有配置服务器备份了以后才能重新均衡数据。
2.多个mongos
Mongos的数量不受限制,建议针对一个应用服务器只运行一个mongos进程。这样每个应用服务器就可以与mongos进行本地会话。
3.每个片都是副本集
生产环境中,每个片都应是副本集。这样单个服务器坏了,就不会导致整个片失效。用addshard命令就可以讲副本集作为片添加,添加时只要指定副本集的名字和种子就好了。
实现即达到数据的分片存储也实现备份和故障自动修复功能,可以副本集和分片混合使用,构建如下图的架构,(为了举例简单仅给shardA做了副本集配置)
Mongo服务器集群配置学习三分片
1.修改A的配置如下:

dbpath=D:\mongodb\test\sharded\A\Data
bind_ip=127.0.0.1
port=8001
replSet=replicademo/127.0.0.1:8003

 新添加A1和A2两台节点与A组成副本集
A1的配置:

dbpath=D:\mongodb\test\sharded\A1\Data
bind_ip=127.0.0.1
port=8004
replSet=replicademo/127.0.0.1:8003

A2的配置:

dbpath=D:\mongodb\test\sharded\A2\Data
bind_ip=127.0.0.1
port=8003
replSet=replicademo/127.0.0.1:8001

在A的shell中执行副本集的舒适化

db.runCommand({"replSetInitiate":
  {
  "_id":'replicademo',
  "members":[
  {
  "_id":1,
  "host":"127.0.0.1:8001"
  },
  {
  "_id":2,
  "host":"127.0.0.1:8003"
  },
  {
  "_id":3,
  "host":"127.0.0.1:8004"
  }
  ]
  }
})

这样A,A1,A2的副本集就建立完成,查询配置看到A为活跃节点,如下图:
Mongo服务器集群配置学习三分片
回到路由器上设置分片配置
mongos> db.runCommand({addshard:"replicademo/127.0.0.1:8001"})
这样mongos会知道它所连接的是replicademo副本集,在活跃节点down掉之后就会去寻找新的活跃结点。
执行db.printShardingStatus(),会看到副本集的节点都已经自动的配置进来了,如下图:
Mongo服务器集群配置学习三分片

管理分片
分片的信息主要存放在config数据库上,这样就能被任何连接到mongos的进程访问到了。
配置集合
下面的代码都假设已经在shell中连接了mongos,并且已经运行了use config。
 1.片
可以在shards集合中查到所有的片 >db.shards.find();
2.数据库
databases集合含有已经在片上的数据库列表和一些相关信息。
>db.databases.find() 键的解释如下
"_id" : 表示数据名
"partitioned" : 是否启用分片功能
"primary" :这个值与"_id"对应,表示这个数据的大本营在哪里。也就是开始创建数据库文件的位置
 3.块
块信息保存在chunks集合中,你可以看到数据到底是怎么切分到集群的。
>db.chunks.find()
 分片命令
1.获得概要
>db.printShardingStatus() 给出前面说的那些集合的概要
2.删除片
用removeshard就能从集群中删除片,removeshard会把给定片上的所有块都挪到其他片上。
>db.runCommand({"removeshard":"localhost:10000"})

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

Best Practice Guide for Building IP Proxy Servers with PHP Best Practice Guide for Building IP Proxy Servers with PHP Mar 11, 2024 am 08:36 AM

In network data transmission, IP proxy servers play an important role, helping users hide their real IP addresses, protect privacy, and improve access speeds. In this article, we will introduce the best practice guide on how to build an IP proxy server with PHP and provide specific code examples. What is an IP proxy server? An IP proxy server is an intermediate server located between the user and the target server. It acts as a transfer station between the user and the target server, forwarding the user's requests and responses. By using an IP proxy server

What is the correct way to restart a service in Linux? What is the correct way to restart a service in Linux? Mar 15, 2024 am 09:09 AM

What is the correct way to restart a service in Linux? When using a Linux system, we often encounter situations where we need to restart a certain service, but sometimes we may encounter some problems when restarting the service, such as the service not actually stopping or starting. Therefore, it is very important to master the correct way to restart services. In Linux, you can usually use the systemctl command to manage system services. The systemctl command is part of the systemd system manager

What should I do if I can't enter the game when the epic server is offline? Solution to why Epic cannot enter the game offline What should I do if I can't enter the game when the epic server is offline? Solution to why Epic cannot enter the game offline Mar 13, 2024 pm 04:40 PM

What should I do if I can’t enter the game when the epic server is offline? This problem must have been encountered by many friends. When this prompt appears, the genuine game cannot be started. This problem is usually caused by interference from the network and security software. So how should it be solved? The editor of this issue will explain I would like to share the solution with you, I hope today’s software tutorial can help you solve the problem. What to do if the epic server cannot enter the game when it is offline: 1. It may be interfered by security software. Close the game platform and security software and then restart. 2. The second is that the network fluctuates too much. Try restarting the router to see if it works. If the conditions are OK, you can try to use the 5g mobile network to operate. 3. Then there may be more

How to install PHP FFmpeg extension on server? How to install PHP FFmpeg extension on server? Mar 28, 2024 pm 02:39 PM

How to install PHPFFmpeg extension on server? Installing the PHPFFmpeg extension on the server can help us process audio and video files in PHP projects and implement functions such as encoding, decoding, editing, and processing of audio and video files. This article will introduce how to install the PHPFFmpeg extension on the server, as well as specific code examples. First, we need to ensure that PHP and FFmpeg are installed on the server. If FFmpeg is not installed, you can follow the steps below to install FFmpe

How to configure and install FTPS in Linux system How to configure and install FTPS in Linux system Mar 20, 2024 pm 02:03 PM

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

See all articles