In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article brings you a detailed introduction to the sharding function of Mongodb. Most of the Mongodb knowledge points are often used by everyone, so we can share them for your reference. Let's follow the editor and have a look.
Overview of MongoDB sharding
There is another kind of cluster in Mongodb, that is, sharding technology, which can meet the needs of the massive growth of MongoDB data.
When MongoDB stores large amounts of data, one machine may not be enough to store data, or it may not be enough to provide acceptable read and write throughput. At this time, we can split the data on multiple machines, so that the database system can store and process more data.
Why do you use fragmentation?
Copy all writes to the primary node
Delay-sensitive data will be queried at the primary node
A single replica set is limited to 12 nodes
Running out of memory occurs when the number of requests is large.
Insufficient local disk
Vertical expansion is expensive
MongoDB fragmentation
The following figure shows the distribution using the sharding cluster structure in MongoDB:
There are three main components in the above figure as follows:
Shard:
It is used to store actual data blocks. In the actual production environment, a shard server role can be assumed by several machines grouped into a replica set to prevent a single point of failure of the host.
Config Server:
Mongod instance, which stores the entire ClusterMetadata, including chunk information.
Query Routers:
The front-end routing allows the client to access, and makes the whole cluster look like a single database, and the front-end applications can be used transparently.
Sharding instance
192.168.1.100: mongos
192.168.1.110: config server
192.168.1.101: Shard1
192.168.1.102: Shard2
The mongodb database is installed on all four hosts.
one。 Configure config server
Edit the configuration file mongod.conf:
Vim / etc/mongod.conf
Add the following and enable the config server service:
Configsvr=true
Restart the mongodb service:
Service mongod restart
Note: by default, port 27019 is listening. You can also start the mongod process using the following command.
# mongod-configsvr-dbpath-port
At this point, the default port changes from 27017 to 27019:
two。 Configure mongos instance
Install mongos:
Yum install mongodb-org-mongos-2.6.1-1.x86_64.rpm
Start mongos:
Mongos-configdb=192.168.1.110-fork-logpath=/var/log/mongodb/mongos.log
Note: mongos is a lightweight application and can run on the same node as other services. When starting, you need to specify the access address of each config server for the mongos instance.
By default, mongos listens on port 27017; you can start the mongos instance using the following command.
# mongos-configdb
You can also edit the configuration file directly:
1. Comment the dbpath instruction
2. Add the configdb directive and specify the address of the config server
Then use the following command to start the mongos instance:
# mongos-f / etc/mongod.conf
Log in to mongos and configure the shard node:
Mongo-- host 192.168.1.100mongos > sh.addShard ("192.168.1.101") {"shardAdded": "shard0000", "ok": 1} mongos > sh.addShard ("192.168.1.102") {"shardAdded": "shard0001", "ok": 1}
View shard status:
Mongos > sh.status ()-Sharding Status-sharding version: {"_ id": 1, "version": 4, "minCompatibleVersion": 4, "currentVersion": 5, "clusterId": ObjectId ("58d4bd8a102ad4bdad74aa1d")} shards: {"_ id": "shard0000", "host": "192.168.1.101 Suzhou 27017"} {"_ id": "shard0001" "host": "192.168.1.102 id 27017"} databases: {"_ id": "admin", "partitioned": false, "primary": "config"}
Start the sharding function:
Mongos > sh.enableSharding ("testdb"); {"ok": 1}
Note: the shard function of mongodb is implemented at the collection level, but to start shard on collection, you need to enable it in advance on its associated database. When the shard feature is enabled on the database, MongoDB assigns it a primary shard.
The enabling process needs to be implemented on the mongos instance, either using the sh.enableSharding () method or the "enableSharding command" of db.runCommand (), in the following format:
Sh.enableSharding (") db.runCommand ({enableSharding:})
At this point, check the shard status again as follows:
Mongos > sh.status ()-Sharding Status-sharding version: {"_ id": 1, "version": 4, "minCompatibleVersion": 4, "currentVersion": 5, "clusterId": ObjectId ("58d4bd8a102ad4bdad74aa1d")} shards: {"_ id": "shard0000", "host": "192.168.1.101 Suzhou 27017"} {"_ id": "shard0001" "host": "192.168.1.102 id 27017"} databases: {"_ id": "testdb", "partitioned": true, "primary": "shard0000"}
Test:
Sharding on collection:
Mongos > sh.shardCollection ("testdb.student", {"age": 1}) {"collectionsharded": "testdb.student", "ok": 1}
Insert data:
Mongos > for
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.